Android 入门笔记(一): RecyclerView

. 2 min read

暑期生产实习选了 Android 开发项目,从未接触过Android开发,对Java也只是略知一二。花了大概一个星期学习 Android 基础知识,完成了Udacity上官方课程的前七周课程,模仿做了几个 Material Design 界面。学习期间做了少许笔记,仅作为自己日后参考。

RecyclerView

Steps

  1. Create the RecyclerView in the layout
  2. Create the list item layout and ViewHolder
  3. Add the RecyclerView adapter
  4. Add the LayoutManager and connect everything togher

Details

1.Add RecyclerView dependency, then create the RecyclerView in the layout

compile 'com.android.support:recyclerview-v7:25.1.0'
<android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerview_forecast"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

2.Create the list item layout and ViewHolder:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TextView
        android:id="@+id/tv_weather_data"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="22sp"
        android:padding="16dp"/>
</LinearLayout>
public class NumberViewHolder
    extends ViewHolder<RecyclerViewHolder>

3.Add the RecyclerView adapter
recyclerview1
methods must to be overrided:
onCreateViewHolder: inflate list item xml into a view, return a new ViewHolder.

@Override
public ForecastViewHolder
       onCreateViewHolder(ViewGroup parent, int viewType) {
       Context context = parent.getContext();
       int layoutIdForListItem = R.layout.forecast_list_item;
       LayoutInflater inflater = LayoutInflater.from(context);
       boolean shouldAttachToParentImmediately = false;

       View view = inflater.inflate(layoutIdForListItem, 
                parent, shouldAttachToParentImmediately);
       ForecastViewHolder viewHolder = new ForecastViewHolder(view);
       return viewHolder;
    }

onBindViewHolder: OnBindViewHolder is called by the RecyclerView to display the data at the specified position.

public void onBindViewHolder(ForecastViewHolder holder, int position)

getItemCount: This method simply returns the number of items to display. It is used behind the scenes

4.Add LayoutManager and connect all things together:
LayoutManagers: LinearLayoutManager, GridLayoutManager, StaggeredLayoutManager

LayoutManager layoutManager = new LinearLayoutManager(this);
myRecycleView.setLayoutManager(layoutManager);
//designate that all items in the list will have the same size
myRecycleView.setHasFixedSize(true);
MyAdapter myAdapter = new MyAdapter(params);
myRecycleView.setAdapter(myAdapter);

Response to the item clicks

1.Add an interface as Listener. Within that interface, define a void method called onListItemClick that takes an int passing in the position as a parameter:

interface ListItemClickListener {
    void onListItemClick(int position);
}

2.Implement ListItemClickListener from the MainActivity; Override ListItemClickListener's onListItemClick method:

@Override
public void onListItemClick(int position) {
  // show clicked position by toast.
    String toastMsg = "Item #" + position + "clicked.";
    mToast = Toast.makeText(this, toastMsg, Toast.LENGTH_LONG);
    mToast.show();
}

3.Implement OnClickListener in the ViewHolder class, override the onClickMethod:

@Override
public void onClick(View view) {
   //passing the clicked position to mClickHandler
    mOnClickListener.onListItemClick(getAdapterPosition());
}

In the constructor for ViewHolder, setOnClickListener(this) to the View inflated in Adapter#onCreateViewHolder

itemView.setOnClickListener(this);

4.In Adapter class, modify the constructor to add a parameter and store it:

public myAdapter(ListItemClickListener listItemClickListener) {
    mOnClickListener = listItemClickListener;
}