美文网首页
RecyclerView的使用(1)

RecyclerView的使用(1)

作者: 磨磨唧唧儿童杰 | 来源:发表于2017-02-07 17:49 被阅读0次

RecyclerView需要引入support-v7才能使用

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

RecyclerView的基本使用

//新建布局管理器
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
//设置布局的方向
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
//为RecyclerView添加布局管理器
recyclerView.setLayoutManager(layoutManager);
//设置适配器
adapter = new MyAdapter(mDatas);
recyclerView.setAdapter(adapter);

RecyclerView适配器的适配器需继承RecyclerView.Adapter,代码如下:

private static class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
        List<String> data;
          
       //传入数据
        public MyAdapter(List<String> data) {
            this.data = data;
        }

        private OnItemClickLitener mOnItemClickLitener;

        //创建新的View
        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);
            ViewHolder viewHolder = new ViewHolder(view);

            return viewHolder;
        }

        //为控件绑定数据
        @Override
        public void onBindViewHolder(final ViewHolder holder, int position) {
            holder.textView.setText(data.get(position));
        }

        //返回数据个数
        @Override
        public int getItemCount() {
            return data.size();
        }


        //绑定控件
        class ViewHolder extends RecyclerView.ViewHolder {
            private TextView textView;

            public ViewHolder(View itemView) {
                super(itemView);
                textView = (TextView) itemView.findViewById(R.id.tc_item);
            }
        }
        //删除item
        public void removeData(int position) {
            data.remove(position);
            notifyItemRemoved(position);
        }
    }

LinearLayoutManager线性布局可设置滑动方向

setOrientation(LinearLayoutManager.VERTICAL);//竖直滑动
setOrientation(LinearLayoutManager.HORIZONTAL);//横向滑动

GridLayoutManager网格布局 需要设置列数

GridLayoutManager gridLayoutManager = new GridLayoutManager(this,4);

StaggeredGridLayoutManager瀑布流布局 可设置方向与列数

StaggeredGridLayoutManager gridLayoutManager = new StaggeredGridLayoutManager(4,StaggeredGridLayoutManager.HORIZONTAL);

瀑布流布局还需改变item的高度 在适配的onBindViewHolder中加入

 ViewGroup.LayoutParams params = holder.itemView.getLayoutParams();//得到item的LayoutParams布局参数
params.height = (int) (200 + Math.random() * 400);//把随机的高度赋予itemView布局
holder.itemView.setLayoutParams(params);//把params设置给itemView布局

为item添加点击和长按事件
首先新建回调接口

  //设置点击回调
        public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) {
            this.mOnItemClickLitener = mOnItemClickLitener;
        }
        //点击接口
        public interface OnItemClickLitener {
            void onItemClick(View view, int position);

            void onItemLongClick(View view, int position);
        }

然后为item添加点击事件,最后应用自定义回调事件。

 if (mOnItemClickLitener != null) {
         holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        int pos = holder.getLayoutPosition();
                        mOnItemClickLitener.onItemClick(holder.itemView, pos);//设置点击事件
                    }
                });
                holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View view) {
                        int pos = holder.getLayoutPosition();
                        mOnItemClickLitener.onItemLongClick(holder.itemView, pos);//设置长按事件
                        return false;
                    }
                });
            }

相关文章

网友评论

      本文标题:RecyclerView的使用(1)

      本文链接:https://www.haomeiwen.com/subject/yhrmittx.html