【Android 基础】RecyclerView 概述以及使用步骤

RecyclerView是什么?

RecyclerView是一种新的视图组,目标是为任何基于适配器的视图提供相似的渲染方式。它被作为ListView和GridView控件的继承者,在最新的support-V7版本中提供支持。
RecyclerView架构,提供了一种插拔式的体验,高度的解耦,异常的灵活,通过设置它提供的不同LayoutManager,ItemDecoration , ItemAnimator实现令人瞠目的效果。
总之ListView和GridView能做的RecyclerView都能做,并且能实现瀑布流效果。

一般使用步骤:

1.在build.gradle配置RecyclerView的库

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

2.在布局文件写定义RecyclerView


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
   .
   .
   .
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
LinearLayout>

3.在代码中
(1)实例化RecyclerView
(2)初始化数据
(3)设置适配器
(4)设置LayoutManager

.
.
private RecyclerView recyclerview;
private ArrayList datas;
private MyRecyclerViewAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
  //1 实例化RecyclerView
    recyclerview = (RecyclerView) findViewById(R.id.recyclerview);  
    initData();

      //3 设置RecyclerView的适配器
        adapter = new MyAdapter(RecyclerViewActivity.this,datas);
        recyclerview.setAdapter(adapter);
           //4.设置LayoutManager
        recyclerview.setLayoutManager(new LinearLayoutManager(RecyclerViewActivity.this, LinearLayoutManager.VERTICAL, false));
}
/**
 * 2 初始化数据
 */
private void initData() {
    datas = new ArrayList<>();
    for (int i = 0; i < 100; i++) {
        datas.add("Content" + i);
    }
}

4.自定义适配器

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

    private final Context context;
    private final ArrayList datas;


    public MyAdapter(Context context,ArrayList datas){
        this.context = context;
        this.datas = datas;
    }
    /**
     * 相当于ListView适配器中的getView的创建holder布局
     *
     * @param parent
     * @param viewType
     * @return
     */
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(context, R.layout.item_hello, null);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.tv_text.setText(datas.get(position));
        holder.iv_icon.setBackgroundResource(R.mipmap.ic_launcher);

    }

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

    class MyViewHolder extends RecyclerView.ViewHolder {

        private TextView tv_text;
        private ImageView iv_icon;

        public MyViewHolder(View itemView) {
            super(itemView);
            tv_text = (TextView) itemView.findViewById(R.id.tv_text);
            iv_icon = (ImageView) itemView.findViewById(R.id.iv_icon);

        }
    }
}

5.设置Item布局:
item_hello.xml



<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#33000000"
    android:padding="2dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ffffff"
        android:gravity="center"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/iv_icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="3dp"
            android:src="@drawable/default" />

        <TextView
            android:id="@+id/tv_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:padding="3dp"
            android:text="内容"
            android:textColor="#000000"
            android:textSize="15sp" />
    LinearLayout>
RelativeLayout>

使用上其实和ListView差不多。

你可能感兴趣的:(Android,之旅)