天天看点

android控件recyclerView的使用

Android控件RecyclerView与ListView的异同

优点:Android中RecyclerView控件支持线性布局、网格布局、瀑布流布局 三种,还可以而且同时还能够控制横向和纵向滚动。自带添加item和删除item的动画效果

缺点:RecyclerView自身没有点击事件,需要写接口,在onBindViewHolder方法中添加点击事件

实现效果图:

android控件recyclerView的使用

第一步:在build.gradle中添加一行,引用类库

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

第二步:在activity_main.xml文件中,添加控件

<android.support.v7.widget.RecyclerView
    android:id="@+id/recy_list"
    android:dividerHeight="1dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />      

第三步:新建一个item.xml,这个布局主要用于显示RecyclerView列表项的布局,读者可根据实际情况自行添加

第四步:新建一个适配器,用于设置RecyclerView的列表项布局,并且添加点击事件

public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.MyViewHolder> {
    private  OnItemClickListener mOnItemClickListener;
    private List<Bitmap> mDatas;
    private Context mContext;
    private LayoutInflater inflater;

    public MyRecyclerAdapter(Context context, List<Bitmap> datas){
        this. mContext=context;
        this. mDatas=datas;
        inflater=LayoutInflater. from(mContext);
    }

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

    //重写onCreateViewHolder方法,返回一个自定义的ViewHolder
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_home,parent, false);
        MyViewHolder holder= new MyViewHolder(view);
        return holder;
    }

    class MyViewHolder extends RecyclerView.ViewHolder{
        ImageView iv;

        public MyViewHolder(View view) {
            super(view);
            iv=(ImageView) view.findViewById(R.id.iv_item_adapter);
        }
    }

    public interface OnItemClickListener{
        void onClick( int position);
        void onLongClick( int position);
    }
    public void setOnItemClickListener(OnItemClickListener onItemClickListener ){
        this.mOnItemClickListener=onItemClickListener;
    }

    //填充onCreateViewHolder方法返回的holder中的控件
    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
        holder. iv.setImageBitmap(mDatas.get(position));

        if( mOnItemClickListener!= null){
            holder. itemView.setOnClickListener( new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    mOnItemClickListener.onClick(position);
                }
            });

            holder. itemView.setOnLongClickListener( new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    mOnItemClickListener.onLongClick(position);
                    return false;
                }
            });
        }
    }
}      

第五步:在MainActivity文件中,导入布局,设置适配器

public class MainActivity extends Activity {
    //参考网址   https://blog.csdn.net/dmk877/article/details/50816933
    private RecyclerView recyclerView;
    private List<Bitmap> mDatas;
    private MyRecyclerAdapter recycleAdapter;

    @SuppressLint("NewApi") @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout. test);

        recyclerView = (RecyclerView) findViewById(R.id.recy_list);

        initData();
        recycleAdapter= new MyRecyclerAdapter(ImageActivity.this , mDatas );

        /**第一种形式:普通的listview列表效果
         LinearLayoutManager layoutManager = new LinearLayoutManager(this);
         //设置布局管理器
         recyclerView.setLayoutManager(layoutManager);
         //设置为垂直布局,这也是默认的
         layoutManager.setOrientation(OrientationHelper.VERTICAL);
         //设置Adapter
         recyclerView.setAdapter( recycleAdapter);
         //设置增加或删除条目的动画
         recyclerView.setItemAnimator(new DefaultItemAnimator());  */

        /**第二种形式:设置成gridview形式
         LinearLayoutManager layoutManager = new LinearLayoutManager(this);
         //设置布局管理器
         recyclerView .setLayoutManager(new GridLayoutManager( this,4));
         //设置为垂直布局,这也是默认的
         layoutManager.setOrientation(OrientationHelper.VERTICAL);
         //设置Adapter
         recyclerView.setAdapter( recycleAdapter);
         //设置增加或删除条目的动画
         recyclerView.setItemAnimator(new DefaultItemAnimator());
         */

        /**第三种形式:横向左右滑动
         StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(4,StaggeredGridLayoutManager.HORIZONTAL);
         //设置布局管理器
         recyclerView.setLayoutManager(layoutManager);
         layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
         recyclerView.setAdapter( recycleAdapter);
         //设置增加或删除条目的动画
         recyclerView.setItemAnimator(new DefaultItemAnimator());
         */
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        //设置布局管理器
        recyclerView .setLayoutManager(new GridLayoutManager( this,4));
        //设置为垂直布局,这也是默认的
        layoutManager.setOrientation(OrientationHelper.VERTICAL);
        //设置Adapter
        recyclerView.setAdapter( recycleAdapter);
        //设置增加或删除条目的动画
        recyclerView.setItemAnimator(new DefaultItemAnimator());

        recycleAdapter.setOnItemClickListener(new MyRecyclerAdapter.OnItemClickListener() {

            @Override
            public void onLongClick(int position) {
                Toast.makeText(ImageActivity.this,"onLongClick事件您点击了第:"+position+"个Item",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onClick(int position) {
                Toast.makeText(ImageActivity.this,"onClick事件您点击了第:"+position+"个Item",Toast.LENGTH_SHORT).show();
            }
        });

    }

    private void initData() {
        mDatas = new ArrayList<Bitmap>();
        mDatas.add(BitmapFactory.decodeResource(getResources(),R.mipmap.a1));
        mDatas.add(BitmapFactory.decodeResource(getResources(),R.mipmap.a2));
        mDatas.add(BitmapFactory.decodeResource(getResources(),R.mipmap.a3));
        mDatas.add(BitmapFactory.decodeResource(getResources(),R.mipmap.a4));
        mDatas.add(BitmapFactory.decodeResource(getResources(),R.mipmap.a5));
        mDatas.add(BitmapFactory.decodeResource(getResources(),R.mipmap.a6));
    }
}