recyclerView的基础使用

   RecyclerView 简介  

     在Android 5.X中将使用了很久的ListView做了升级,增加了一个使用更方便、效率更高的的控件--RecyclerView,RecyclerView是support-v7包中的新组件 ,是一个强大的滑动组件,与经典的Listview相比,它同样拥有item回收复用的功能,但是RecyclerView可以直接把Viewholder的实现封装起来,用户只要实现自己的ViewHolder就可以了,该组件会自动帮你回收复用每一个item。

      要使用RecyclerView,首先需要在项目中引入com.android.support:recyclerview-v7:21.0.2的依赖。在布局中使用RecyclerView与使用ListView基本类似,同样需要使用一个类似List item的布局,在Material Design中,通常与CardView使用。

      具体代码如下所示:

/**
 * Created by 王亭亭 on 2017/4/25.
 */

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder>{

    private List<String>mData;
    public RecyclerAdapter(List<String>data){
        this.mData=data;
    }
    public AdapterView.OnItemClickListener  itemClickListener;
    public void setOnItemClickListener(AdapterView.OnItemClickListener itemClickListener){
        this.itemClickListener=itemClickListener;
    }
    public interface OnItemClickListener{
        void onItemClic(View view int position);
    }


    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        public TextView textView;
        public ViewHolder(View itemView) {
            super(itemView);
            textView= (TextView) itemView;
            textView.setOnClickListener(this);
        }
         //通过接口回调来实现RecyclerView的点击事件
        @Override
        public void onClick(View view) {
            if(itemClickListener!=null){
                itemClickListener.onItemClick(view,getPosition());
            }
        }
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //将布局转化为View并传递给RecyclerView封装好的ViewHolder
        View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.view_item,parent,true);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
       //建立起ViewHolder中视图与数据的关联
        holder.textView.setText(mData.get(position)+position);
    }



    @Override
    public int getItemCount() {
        return mData.size();
    }
}
 上面就是一个非常简单却典型的RecyclerView,通过onCreateViewHolder将List item的布局转化为View,并传递给RecyclerView封装好的ViewHolder,就可以将数据与视图关联起来。但是有一点需要注意的是,Android并没有给RecyclerView增进点击事件,所以我们需要自己使用接口回调机制,创建一个点击事件的接口,代码如下 

public AdapterView.OnItemClickListener  itemClickListener;
public void setOnItemClickListener(AdapterView.OnItemClickListener itemClickListener){
    this.itemClickListener=itemClickListener;
}
public interface OnItemClickListener{
    void onItemClic(View view int position);
}
  类似ListView的List Item  视图如下所示:

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">
       <TextView
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:textSize="40sp"
           android:gravity="center"
           android:background="#bebebe"
           android:layout_margin="3dp"
           />
LinearLayout>
  当然了,仅仅是优化性能也是不够的,让开发者能够更加方便地使用也是非常重要的。Google在RecyclerView中定义了LayoutManager来帮助开发者更加方便地创建不同烦的布局,接下来我们就演示如何创建简单的水平布局和竖直两种布局方式。当然,我们也可以通过自定义LayoutManager来创建自己的布局,核心代码如下所示。

public class MainActivity extends AppCompatActivity {
    private RecyclerView mRcList;
    private RecyclerAdapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;
    private Spinner mSpinner;
    private List<String>mData=new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.other_layout);
         mRcList= (RecyclerView) findViewById(R.id.rc_list);
         mLayoutManager=new LinearLayoutManager(this);
         mRcList.setLayoutManager(mLayoutManager);
         mRcList.setHasFixedSize(true);
        //设置显示动画
        mRcList.setItemAnimator(new DefaultItemAnimator());
        mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView adapterView, View view, int i, long l) {
                if (i==0){
                    //设置为线性布局
                    mRcList.setLayoutManager(new LinearLayoutManager(MainActivity.this));
                }else if (i==1){
                    //设置为表格布局
                    mRcList.setLayoutManager(new GridLayoutManager(MainActivity.this,3));
                }else if (i==2){
                    
                }
            }

            @Override
            public void onNothingSelected(AdapterView adapterView) {

            }
        });
        //增加测试数据
        mData.add("Recycler");
        mData.add("Recycler");
        mData.add("Recycler");
        mAdapter=new RecyclerAdapter(mData);
        mRcList.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new RecyclerAdapter.OnItemClickListener(){

            @Override
            public void onItemClick(final View view) {
                //设置点击动画
                view.animate().translationZ(15f).setDuration(300).setListener(new Animator.AnimatorListener() {
                    @Override
                    public void onAnimationStart(Animator animator) {
                        
                    }

                    @Override
                     public void onAnimationEnd(Animator animator) {
                               view.animate().translationZ(1F).setDuration(500).start(); 
                    }

                    @Override
                    public void onAnimationCancel(Animator animator) {

                    }

                    @Override
                    public void onAnimationRepeat(Animator animator) {

                    }
                }).start();
            }
        });
    }
    public void addRecycler(View view){
        mData.add("Recycler");
        int position=mData.size();
        if (position>0){
            mAdapter.notifyDataSetChanged();
        }
    }
    public void delRecycler(View view){
        int position=mData.size();
        if (position>0){
            mData.remove(position-1);
            mAdapter.notifyDataSetChanged();
        }
    }
}
   至此,recyclerView的简单使用就介绍完毕了!!!

你可能感兴趣的:(android,recyclerView)