Simple use of recyclerview

Since Android 5.0, Google has launched a recyclerview control, which is a new component in the support-v7 package and a powerful sliding component. Compared with the classic listview, it also has the function of item recycling and reuse. Recyclerview is equivalent to an upgraded version of listview.

Recyclerview encapsulates the recycling and reuse of viewholder, that is, recyclerview standardizes viewholder, writes adapter for viewholder instead of view, and the reuse logic is encapsulated, making it easier to write.

Recyclerview provides a plug-in experience with high decoupling and exceptional flexibility. For the display of an item, recyclerview specially extracts the corresponding classes to control the display of items, making it particularly scalable.

compile 'com.android.support:recyclerview-v7:25.1.0'

/*activity_main.xml*/
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.manu.mrecyclerview.MainActivity">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </android.support.v7.widget.RecyclerView>
</LinearLayout>

/*item.xml*/
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="5dp">
    <TextView
        android:id="@+id/tv_recycle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="data"
        android:background="#cac3c3"
        android:padding="10dp"
        android:textSize="20sp"/>
</LinearLayout>

The recyclerview adapter is a little more complicated than the listview adapter. This is also a reflection of the high decoupling of recyclerview. Although the code is a little more complex, it has good scalability. Here are three methods to implement the recyclerview adapter:

This method mainly loads a view for each item, but it returns a viewholder. This method directly encapsulates the view in the viewholder, and then we target the instance of viewholder. This viewholder is also written by ourselves, but we don't need to call convertview. Settag (VH) and convertview. Gettag () like listview.

This method is mainly used to bind data to the view and directly provide a viewholder instead of convertview.

This method returns the total number of options.

/**
 * Created by jzman on 2017/5/13 0013.
 */
public class RvAdapter extends RecyclerView.Adapter<RvAdapter.DataViewHolder>{
    private Context mContext;
    private ArrayList<String> mList;

    public RvAdapter() {}

    public RvAdapter(Context mContext,ArrayList<String> mList) {
        this.mContext = mContext;
        this.mList = mList;
    }

    //用于创建ViewHolder
    @Override
    public DataViewHolder onCreateViewHolder(ViewGroup parent,int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item,null);
        //使用代码设置宽高(xml布局设置无效时)
        view.setLayoutParams(new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT));
        DataViewHolder holder = new DataViewHolder(view);
        return holder;
    }
    //绑定数据
    @Override
    public void onBindViewHolder(DataViewHolder holder,int position) {
        holder.tv_data.setText(mList.get(position));
    }
    //数据总数
    @Override
    public int getItemCount() {
        return mList.size();
    }

    //创建ViewHolder
    public static class DataViewHolder extends RecyclerView.ViewHolder{
        TextView tv_data;
        public DataViewHolder(View itemView) {
            super(itemView);
            tv_data = (TextView) itemView.findViewById(R.id.tv_recycle);
        }
    }

When using the staggeredgridlayoutmanager manager, the adapter reference is as follows:

/**
 * Created by jzman on 2017/5/13 0013.
 * RecycleView的Adapter
 */
public class RvAdapter extends RecyclerView.Adapter<RvAdapter.DataViewHolder>{
    private Context mContext;
    private RecyclerView recyclerView;
    private ArrayList<String> mList;
    private ArrayList<Integer> mHeight;

    public RvAdapter() {}

    public RvAdapter(Context mContext,ArrayList<String> mList) {
        this.mContext = mContext;
        this.mList = mList;
    }

    /**
     * 初始化每个Item的高度(瀑布流效果)
     * @return
     */
    public ArrayList<Integer> initHeight(){
        mHeight = new ArrayList<>();
        for (int i=0;i<mList.size();i++){
            mHeight.add((int) (Math.random()*300)+200);
        }
        return mHeight;
    }

    /**
     * 用于创建ViewHolder
     * @param parent
     * @param viewType
     * @return
     */
    @Override
    public DataViewHolder onCreateViewHolder(ViewGroup parent,ViewGroup.LayoutParams.WRAP_CONTENT));
        DataViewHolder holder = new DataViewHolder(view);
        return holder;
    }

    /**
     * 绑定数据
     * @param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(DataViewHolder holder,int position) {
        //设置每个Item的高度
        ViewGroup.LayoutParams h = holder.tv_data.getLayoutParams();
        h.height = mHeight.get(position);
        holder.tv_data.setText(mList.get(position));
    }

    /**
     * 选项总数
     * @return
     */
    @Override
    public int getItemCount() {
        return mList.size();
    }

    /**
     * 创建ViewHolder
     */
    public static class DataViewHolder extends RecyclerView.ViewHolder{
        TextView tv_data;
        public DataViewHolder(View itemView) {
            super(itemView);
            tv_data = (TextView) itemView.findViewById(R.id.tv_recycle);
        }
    }
    /**
     *   将RecycleView附加到Adapter上
     */
    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        this.recyclerView= recyclerView;
        //初始化每个Item的高度
        initHeight();
    }
    /**
     *   将RecycleView从Adapter解除
     */
    @Override
    public void onDetachedFromRecyclerView(RecyclerView recyclerView) {
        super.onDetachedFromRecyclerView(recyclerView);
        this.recyclerView = null;
    }
}

/**
 * Created by jzman on 2017/5/13 0013.
 */
public class MainActivity extends AppCompatActivity {
    private RecyclerView rv;
    RvAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        rv = (RecyclerView) findViewById(R.id.rv);
        //设置布局管理器
        rv.setLayoutManager(new linearlayoutmanager(this));//线性
//        rv.setLayoutManager(new GridLayoutManager(this,4));//线性
//        rv.setLayoutManager(new StaggeredGridLayoutManager(4,StaggeredGridLayoutManager.VERTICAL));//线性
        adapter = new RvAdapter(this,initData());
        rv.setAdapter(adapter);
    }
    public static ArrayList<String> initData(){
        ArrayList<String> arrayList = new ArrayList<>();
        for (int i=0;i<50;i++){
            arrayList.add("第"+i+"条数据");
        }
        return arrayList;
    }
}

< end >

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>