美文网首页
RecyclerView

RecyclerView

作者: 云烟渐成雨 | 来源:发表于2019-06-15 02:06 被阅读0次

1.RecyclerView的使用

    //设置布局管理器
    mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
    //设置item增加和删除的动画
    mRecyclerView.setItemAnimator(new DefaultItemAnimator());
    mHomeAdapter = new HomeAdapter(this, mList);
    mRecyclerView.setAdapter(mHomeAdapter);


    //将RecyclerView设置成水平排列
//  LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
//  linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
//  mRecyclerView.setLayoutManager(linearLayoutManager);

Adapter最大的改进就是ViewHolder进行了封装定义,我们只需自定义一个ViewHolder继承RecyclerView.ViewHolder就可以了。另外,Adapter继承了RecyclerView.Adapter,在onCreateViewHolder中加载条目布局,在onBindViewHolder中将视图与数据进行绑定。

public class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.MyViewHolder> {
    private Context mContext;
    private List<String> mList;

    public HomeAdapter(Context context, List<String> mList) {
        this.mContext = context;
        this.mList = mList;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_recycler, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        holder.mTextView.setText(mList.get(position));
    }

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

    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView mTextView;

        MyViewHolder(@NonNull View itemView) {
            super(itemView);
            mTextView = itemView.findViewById(R.id.tv_item);
        }
    }
}

2.自定义点击事件

public interface OnItemClickListener {
    void onItemClick(View view,int position);
    void onItemLongClick(View view,int position);
}

在Adapter中添加代码

public void setOnItemClickListener(OnItemClickListener mOnItemClickListener){
        this.mOnItemClickListener = mOnItemClickListener;
    }
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(mContext).inflate(R.layout.item_recycler, parent, false);
    view.setOnClickListener(this);
    view.setOnLongClickListener(this);
    return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
    holder.itemView.setTag(position);
    holder.mTextView.setText(mList.get(position));
    }
@Override
public void onClick(View view) {
    if (mOnItemClickListener != null){
        mOnItemClickListener.onItemClick(view, (int) view.getTag());
    }
}

@Override
public boolean onLongClick(View view) {
    if (mOnItemClickListener != null){
        mOnItemClickListener.onItemLongClick(view, (Integer) view.getTag());
        }
    return true;
}

最后在Activity中监听(继承接口 OnItemClickListener)

    @Override
    public void onItemClick(View view, int position) {
        Toast.makeText(RecyclerViewActivity.this,"点击第"+(position+1),Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onItemLongClick(View view, final int position) {
        new AlertDialog.Builder(RecyclerViewActivity.this)
                .setTitle("确认删除吗?")
                .setNegativeButton("取消",null)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        mHomeAdapter.notifyItemRemoved(position);
                        mList.remove(position);
                        //position数据开始变化的位置,itemCount变化位置的数据数量2.2
                        mHomeAdapter.notifyItemRangeChanged(position,mList.size()-position);
                        Toast.makeText(RecyclerViewActivity.this,(position+1)+"已删除",Toast.LENGTH_SHORT).show();
                    }
                })
                .show();
    }

RecycleView的notifyItemRemoved使用注意

在移除RecyclerView中的数据时发现第一次移除数据时正常,后来每一次的操作的position都不正确

原因是在执行

mHomeAdapter.notifyItemRemoved(position);

后,没有重新bind数据,position没有刷新。以下代码可以解决问题:

public void removeItem(int position){
   data.remove(posiiton);
   notifyItemRemoved(position);
   notifyItemRangeChanged(position, data.size() - position);
}

使用==notifyItemRangeChanged(int positionStart, int itemCount)==

  1. positionStart : 是从界面哪个位置的Item开始变化,比如你点击界面上的第二个ItemView positionStart是1
  2. itemCount : 是已经发生变化的item的个数(包括自己,即正在点击这个)

3.实现GridView

//实现GridView,只需要自定义横向的分割线
    mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(4,StaggeredGridLayoutManager.VERTICAL));
//  mRecyclerView.addItemDecoration(自定义分割线);

4.实现瀑布流

在Adapter写一个随机的高度来控制每个item的高度就可以了。通常这个歌高度是由服务器返回的高度来控制的,在这里我们写一个随机的高度来控制每个item的高度

mHeight = new ArrayList<>();
        for (int i = 0; i < mList.size(); i++) {
            mHeight.add((int) (100 + Math.random() * 300));
        }

接着在Adapter的onBindViewHolder中设置每个item的高度

ViewGroup.LayoutParams layoutParams = holder.mTextView.getLayoutParams();
layoutParams.height = mHeight.get(position);
holder.mTextView.setLayoutParams(layoutParams);

VFUF6x.md.png

相关文章

网友评论

      本文标题:RecyclerView

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