美文网首页
RecyclerView基本使用(二)

RecyclerView基本使用(二)

作者: aidlFor | 来源:发表于2018-07-08 12:19 被阅读0次

    现在基于第一个例子,总结常用的效果,GridView效果,瀑布流效果并且实现点击和分割线。
    1,GridView效果
    只要在原基础上修改LayoutManager配置即可,从LinearLayoutManager换成StaggeredGridLayoutManager实现GridView效果,代码如下:

    StaggeredGridLayoutManager staggeredGridLayoutManager = new 
    StaggeredGridLayoutManager(4,StaggeredGridLayoutManager.VERTICAL);
    mRecyclerView.addItemDecoration(new DividerGridItemDecoration(this));
    mRecyclerView.setLayoutManager(staggeredGridLayoutManager);
    

    点击事件:
    1)在Adapter中定义监听回调接口和方法

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

    2)在当前的Adapter中onBindViewHolder回调中调用布局的监听事件

     @Override
    public void onBindViewHolder(@NonNull final MyViewHolder holder, int position) {
    
        if (mOnItemClickListener!=null){
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = holder.getLayoutPosition();
                    mOnItemClickListener.onItemClick(holder.title,position);
                }
            });
    
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    int position = holder.getLayoutPosition();
                    mOnItemClickListener.onItemLongClick(holder.title,position);
    
                    return false;
                }
            });
    
        }
    }
    

    3)在Activity中调用这个监听

    mAdapter.setOnItemClickListener(new RecycleViewAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Toast.makeText(RecycleViewActivity.this,"点击了"+position,Toast.LENGTH_SHORT).show();
            }
    
            @Override
            public void onItemLongClick(View view, int position) {
                mAdapter.removeData(position);
            }
        });
    
    gridview.png

    2,瀑布流效果
    需要在Adapter中写一个随机的高度来控制每一个item就好,通常这个高度是服务器返回的。

     1)在Adapter构造器中初始化随机的瀑布流高度
        mHeights = new ArrayList<Integer>();
    
        for (int i =0; i< mList.size();i++){
            mHeights.add((int) (100+Math.random() *300));
        }
     2)在onBindViewHolder中重新设置子View的大小
    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder holder, int position) {
    
        ViewGroup.LayoutParams layoutParams = holder.title.getLayoutParams();
    
        layoutParams.height = mHeights.get(position);
    
        holder.title.setLayoutParams(layoutParams);
    
        holder.title.setText(mList.get(position));
    
        
    
        }
    }
    
    pbl.png

    相关文章

      网友评论

          本文标题:RecyclerView基本使用(二)

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