RecylerView的基本用法

在做項目時,使用了RecylerView取代了ListView這個傳統的列表控件,現在總結一下它的基本用法,方便以後回顧。效果圖如下:

先來看一下工程結構:

使用步驟:

  1. 添加依賴,因爲是v7包下的(compile ‘com.android.support:recyclerview-v7:25.0.0’)
  2. 準備數據源,以便填充數據
  3. 佈局
  4. 查找控件
  5. 準備RecylerView的子條目佈局
  6. 複寫適配器
  7. 設置適配器

爲了存儲數據方便在這裏建了一個人類,擁有三個屬性,name,age,picture。

public class PersonBean {

public String name;
public int age;
public int picture;
}

添加多個人:

    List<PersonBean> datas = new ArrayList<>();
    for (int i = 0; i < 20; i++) {
        PersonBean personBean = new PersonBean();

        personBean.name = "蒼老師" + i;
        personBean.age = 20 + i;
        personBean.picture = R.mipmap.gril;

        datas.add(personBean);
    }`

將RecylerView寫入佈局(activity_main.xml)

 <android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerview"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

查找控件,老套路

 //基本用法
    recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
    //添加布局管理器,還有其它幾種
    recyclerView.setLayoutManager(new LinearLayoutManager(this));

準備RecylerView的子條目佈局(item_recylerview)

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

<ImageView
    android:id="@+id/iv_user_pic"
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:layout_margin="20dp"
    android:src="@mipmap/gril" />


<TextView
    android:id="@+id/tv_user_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_margin="20dp"
    android:text="姓名" />

<TextView
    android:id="@+id/tv_user_age"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_margin="20dp"
    android:text="年齡" />

</RelativeLayout>

重寫適配器,同時要先準備好ViewHolder

 private class MyRecylerAdapter extends Adapter<MyRecylerAdapter.MyRecylerViewHolder> {


    @Override
    public MyRecylerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.item_recylerview, null);
        MyRecylerViewHolder holder = new MyRecylerViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(MyRecylerViewHolder holder, int position) {
        PersonBean personBean = datas.get(position);
        holder.tv_user_name.setText(personBean.name);
        holder.tv_user_age.setText(String.valueOf(personBean.age));
        holder.iv_user_pic.setBackgroundResource(personBean.picture);
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    //ViewHolder
    class MyRecylerViewHolder extends ViewHolder {

        private final ImageView iv_user_pic;
        private final TextView tv_user_name, tv_user_age;

        public MyRecylerViewHolder(View itemView) {
            super(itemView);
            iv_user_pic = (ImageView) itemView.findViewById(R.id.iv_user_pic);
            tv_user_name = (TextView) itemView.findViewById(R.id.tv_user_name);
            tv_user_age = (TextView) itemView.findViewById(R.id.tv_user_age);
        }
    }
}

設置適配器

 //設置適配器
    recyclerView.setAdapter(new MyRecylerAdapter());
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章