implementation 'com.android.support:recyclerview-v7:25.3.1'

布局代码


<android.support.v7.widget.RecyclerView    android:id="@+id/rv_main"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:scrollbars="vertical" />

recyclerview有专门的布局管理器来实现其横向、纵向、还是瀑布流


1、LinearLayoutManager 以垂直或水平滚动列表方式显示项目 

2、GridLayoutManager 在网格中显示项目 

3、StaggeredGridLayoutManager 在分散对齐网格中显示项目(俗称瀑布流)

我们先来看看和listview 纵向布局的代码:

mRvMain = (RecyclerView) findViewById(R.id.rv_main);// 设置布局LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);mRvMain.setLayoutManager(linearLayoutManager);

适配器代码:

public class MyRVAdapter2 extends RecyclerView.Adapter<MyRVAdapter2.MyTVHolder> {    private final LayoutInflater mLayoutInflater;    private final Context mContext;    private final ArrayList mData;    public MyRVAdapter2(Context context) {        mLayoutInflater = LayoutInflater.from(context);        mContext = context;        mData = new ArrayList<>();        for (int i = 0; i < 40; i++) {            mData.add("hello " + i);        }    }    @Override    public MyRVAdapter2.MyTVHolder onCreateViewHolder(ViewGroup parent, int viewType) {        return new MyRVAdapter2.MyTVHolder(mLayoutInflater.inflate(R.layout.rv_txt_item, parent, false));    }    @Override    public void onBindViewHolder(final MyRVAdapter2.MyTVHolder holder, int pos) {        holder.mTextView.setText(mData.get(pos));    }    @Override    public int getItemCount() {        return mData == null ? 0 : mData.size();    }    class MyTVHolder extends RecyclerView.ViewHolder {        TextView mTextView;        MyTVHolder(View itemView) {            super(itemView);            mTextView = (TextView) itemView.findViewById(R.id.tv_txt);        }    }}

简单的item: rv_txt_item.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:orientation="horizontal">    <TextView        android:id="@+id/tv_txt"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:gravity="center_horizontal"        android:padding="@dimen/eight_dp"        android:text="@string/tmp"        android:textSize="@dimen/thirty_sp" />LinearLayout>

看效果图如下:

iOS 使用UIcollectionView实现瀑布流 并自动计算cell的高度 android recyclerview 瀑布流_android 长按

效果图

网格的实现:

GridLayoutManager gridLayoutManager        = new GridLayoutManager(this, 2);mRvMain.setLayoutManager(gridLayoutManager);

adapter适配器此处省略

效果图如下:

iOS 使用UIcollectionView实现瀑布流 并自动计算cell的高度 android recyclerview 瀑布流_android 长按_02

点击监听事件:

在对RecyclerView的item进行操作的时候会遇到点击事件和长按事件,但是在RecyclerView中没有直接的方法进行监听,需要我们在适配器里面对item进行监听,或者提供一个接口给RecyclerView调用,实现点击和长按的事件监听事件。在适配器里面对item进行监听实际上是对填充到列表项的view进行监听,实现的是setOnClickListener和setOnLongClickListener方法。

@NonNull    @Override    public CreateHolder onCreateViewHolder(@NonNull ViewGroup parent, final int viewType) {       holder.mTextView.setOnLongClickListener (new View.OnLongClickListener () {            @Override            public boolean onLongClick(View view) {                return false;            }        });        holder.mTextView.setOnClickListener (new View.OnClickListener () {            @Override            public void onClick(View view) {            }        });        return holder;    }

滑动监听事件

RecyclerView在滑动的时候可以直接对滑动事件进行监听,可以直接在RecyclerView的对象调用addOnScrollListener接口,然后可以复写两个方法,onScrollStateChanged用来监听滑动状态的改变,
onScrolled用来监听滑动的动作。

rv_main.addOnScrollListener (new RecyclerView.OnScrollListener () {            @Override            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {                super.onScrollStateChanged (recyclerView, newState);            }            @Override            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {                super.onScrolled (recyclerView, dx, dy);            }        });

瀑布流实现请参考:https://github.com/ChunmingWu/StudyProject/tree/scratchDemo