Android——RecyclerView和CardView的学习

本文主要通过一个简单小例子来简单的介绍一下RecyclerView和CardView,要是想详细的学习RecyclerView,可以看鸿洋的写的关于RecyclerView的博客。
RecyclerView是对ListView的升级,与ListView相比一个明显的区别就是RecyclerView将ViewHolder的实现封装起来,用户只需要实现自己的ViewHolder,就可以实现回收服用每一个item。

RecyclerView相关的知识点:

通过布局管理器LayoutManager控制显示的方式,有三种:LinearLayoutManager(线性) GridLayoutManager(网格)、StaggeredGridLayoutManager (瀑布流)
通过ItemDecoration控制item之间的间隔
过ItemAnimator控制item增删的动画

RecyclerView需要使用合适的适配器来加载数据,其中与ListView不同之处就是封装了ViewHolder
RecyclerAdapter:

public  class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
    public OnItemClickListener onItemClickListener;
    private List<String> mData;


    public RecyclerAdapter(List<String> mData) {
        this.mData = mData;
    }
    //onCreateViewHolder方法中主要将布局转化为View并传递给RecyclerView封装好的ViewHolder
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.rv_item,parent,false);
        return new ViewHolder(v);
    }
   //建立ViewHolder中视图与数据的关联
    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        holder.textView.setText(mData.get(position));

    }


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

    public class ViewHolder extends RecyclerView.ViewHolder {
        public TextView textView;
        public ViewHolder(View itemView) {
            super(itemView);
            textView= (TextView) itemView.findViewById(R.id.textview);
            textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClickListener.onItemClick(v, getAdapterPosition());
                }
            });

        }
    }

    public void  setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.onItemClickListener=onItemClickListener;
    }

}

注意:需要重写的三个方法中,onCreateViewHolder方法中主要将布局转化为View并传递给RecyclerView封装好的ViewHolder;onBindViewHolder建立ViewHolder中视图与数据的关联

Android并没有给RecyclerView增进点击事件,所以需要自己使用接口回调机制,创建点击事件的接口
OnItemClickListener:

public interface OnItemClickListener {
    void onItemClick(View view,int position);
}
public void  setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.onItemClickListener=onItemClickListener;
    }

MainActivity:

public class MainActivity extends AppCompatActivity {
    private RecyclerView recyclerView;

    private List<String> mData;

    private RecyclerAdapter  adapter;

    private StaggeredGridLayoutManager staggeredGridLayoutManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        mData=new ArrayList<>();

        staggeredGridLayoutManager=new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
        recyclerView= (RecyclerView) findViewById(R.id.rv_main);

        recyclerView.setLayoutManager(staggeredGridLayoutManager);
        for(int i=0;i<200;i++){
            mData.add("A"+i);
        }
        adapter=new RecyclerAdapter(mData);
        recyclerView.setAdapter(adapter);


    }
  }

CardView相关知识点:
在使用CardView之前需要先引入依赖:

compile 'com.android.support:cardview-v7:23.1.1'

在布局文件中使用CardView需要引入一个新的命名空间,这样才能自定义的名字空间引用属性,常用属性:
card_view:cardElevation 阴影的大小
card_view:cardMaxElevation 阴影最大高度
card_view:cardBackgroundColor 卡片的背景色
card_view:cardCornerRadius 卡片的圆角大小
card_view:contentPadding 卡片内容于边距的间隔
card_view:contentPaddingBottom
card_view:contentPaddingTop
card_view:contentPaddingLeft
card_view:contentPaddingRight
card_view:contentPaddingStart
card_view:contentPaddingEnd

xmlns:card_view="http://schemas.android.com/apk/res-auto"

rv_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="2dp">
    <android.support.v7.widget.CardView  xmlns:card_view="http://schemas.android.com/apk/res-auto" android:id="@+id/card_view" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center_vertical" card_view:cardBackgroundColor="#fff0" card_view:cardCornerRadius="6dp" >
        <TextView  android:id="@+id/textview" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:layout_gravity="center" android:textSize="30sp" />
    </android.support.v7.widget.CardView>
</RelativeLayout>

content_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:showIn="@layout/activity_main" tools:context=".MainActivity">
    <android.support.v7.widget.RecyclerView  android:id="@+id/rv_main" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="2dp" android:scrollbars="vertical" android:scrollbarStyle="outsideOverlay" />
</RelativeLayout>

上面的代码整合就是完整的代码
博主还在努力学习Android中,一起努力吧!

你可能感兴趣的:(android,列表,CardView,Recycler,卡片)