Basic usage of RecyclerView in Android

Recycler is a new circular view layout proposed by Android after version 5.0, and it has been commonly used in our development. Today I will summarize RecyclerView. This article is some basic usage of Recycler.

1. RecyclerView dependency

implementation 'com.android.support:recyclerview-v7:28.0.0'

Dependencies in androidx

implementation 'androidx.recyclerview:recyclerview:1.0.0'

Everyone chooses according to the specific environment

2. Common methods of RecyclerView

  • setAdapter: Set the adapter of the list item. The adapter uses RecyclerView.Adapter.
  • setLayoutManager: Set the layout manager of the list item, including linear layout manager LinearLayoutManager, grid layout manager GridLayoutManager, waterfall grid layout manager StaggeredGridLayoutManager.
  • addItemDecoration: Add the dividing line of the list item.
  • removeItemDecoration: Remove the dividing line of the list item.
  • setItemAnimator: Set the addition and deletion animation of list items. The default animation is the DefaltItemAnimator that comes with the system.
  •  addOnItemTouchListener: Add a touch listener for list items. Because RecyclerView does not implement the click interface of list items, developers can monitor the user's gestures through the touch listener here.
  • removeOnItemTouchListener: Remove the touch listener for the list item.
  • scorllToPosition: scroll to the specified position.

3.RecyclerView.Adapter related methods

<1>Methods that must be rewritten for custom adapters

  • getItemCount: Get the number of list items.
  • onCreateViewHolder: Create the view holder of the entire layout. The input parameters include the view type, and different layouts can be loaded according to the view type, so as to realize the headed list layout.
  • onBindViewHolder: Bind the view holder of each item.

<2> Method that can be rewritten or not

  • getItemViewType: Returns the view type of each item. This view type is used by the onCreateViewHolder method.
  • getItemId: Get the number of each item.

<3>Methods that can be called directly

  • notifyItemInserted: Notify the adapter that a new item has been inserted at the specified position.
  • notifyItemRemoved: Notify the adapter that the original item has been deleted at the specified location.
  • notifyItemChanged: Notify the adapter that the item at the specified position has changed.
  • notifyDataSetChanged: Notify the adapter that the data in the entire list has changed.

4. Example of use

View layout activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="none"/>
</LinearLayout>

List item layout item_recycler.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/iv"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_gravity="center"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"
            android:src="@mipmap/aaa" />

        <TextView
            android:id="@+id/tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="10dp"
            android:text="这是一行文字"
            android:textColor="#000000"
            android:textSize="20sp" />
    </LinearLayout>

    <View
        android:id="@+id/view"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@android:color/darker_gray"
        android:layout_marginTop="5dp"/>
</LinearLayout>

Adapter HomeAdapter.java

public class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.MyViewHolder>{

    private Context mContext;

    public HomeAdapter(Context mContext) {
        this.mContext = mContext;
    }

    /**
     * 引入布局
     * @param viewGroup
     * @param i
     * @return
     */
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        MyViewHolder holder = new MyViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_recycler,viewGroup,false));
        return holder;
    }

    /**
     * 为控件绑定数据
     * @param myViewHolder
     * @param i
     */
    @Override
    public void onBindViewHolder(MyViewHolder myViewHolder, int i) {
        myViewHolder.tv.setText("这是第"+i+"行");
        if (i%2 == 0){
            myViewHolder.iv.setImageResource(R.mipmap.aaa);
        }else{
            myViewHolder.iv.setImageResource(R.mipmap.bbb);
        }
        if (i == 19){
            myViewHolder.view.setVisibility(View.GONE);
        }else{
            myViewHolder.view.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 返回项个数
     * @return
     */
    @Override
    public int getItemCount() {
        return 20;
    }

    /**
     * 定义控件并初始化
     */
    class MyViewHolder extends RecyclerView.ViewHolder{

        TextView tv;
        ImageView iv;
        View view;

        public MyViewHolder(View itemView) {
            super(itemView);
            tv = itemView.findViewById(R.id.tv);
            iv = itemView.findViewById(R.id.iv);
            view = itemView.findViewById(R.id.view);
        }
    }
}

Use Configuration MainActivity.java in Activity

public class MainActivity extends AppCompatActivity {

    private RecyclerView recycler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recycler = findViewById(R.id.recycler);
        //设置布局管理器
        recycler.setLayoutManager(new LinearLayoutManager(this));
        //设置item增加和删除时的动画
        recycler.setItemAnimator(new DefaultItemAnimator());
        HomeAdapter mAdapter = new HomeAdapter(this);
        recycler.setAdapter(mAdapter);
    }
}

This is the easiest one to use RecyclerView to complete. Some other usage methods will be introduced later.

Guess you like

Origin blog.csdn.net/weixin_38322371/article/details/114881282