最终效果图
1.item_demo5.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="vertical"android:paddingBottom="5dp"android:paddingRight="5dp"><ImageViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:scaleType="fitXY"android:id="@+id/item_demo5_image"/><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/item_demo5_tv"android:layout_margin="5dp"android:background="#ff496b"android:text="12344"/></LinearLayout>
Demo5Adapter
public class Demo5Adapter extends RecyclerView.Adapter<Demo5Adapter.ViewHolder> {/*** 展示数据*/private ArrayList<String> mData;public Demo5Adapter(ArrayList<String> data) {this.mData = data;}public void updateData(ArrayList<String> data) {this.mData = data;notifyDataSetChanged();}@Overridepublic int getItemViewType(int position) {// 瀑布流样式外部设置spanCount为2,在这列设置两个不同的item type,以区分不同的布局return position % 2;}@Overridepublic Demo5Adapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {parent = parent;// 实例化展示的viewView v;
// if(viewType == 1) {v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_demo5, parent, false);
// }
// else {
// v = LayoutInflater.from(parent.getContext()).inflate(R.layout.view_rv_staggered_item_two, parent, false);
// }// 实例化viewholderViewHolder viewHolder = new ViewHolder(v);return viewHolder;}@Overridepublic void onBindViewHolder(Demo5Adapter.ViewHolder holder, int position) {
// 绑定数据holder.mTv.setText(mData.get(position));int type = position % 2;if(type == 0){holder.imageView.setBackgroundResource(R.drawable.a);}else {holder.imageView.setBackgroundResource(R.drawable.ic_launcher);}}@Overridepublic int getItemCount() {return this.mData.size();}public static class ViewHolder extends RecyclerView.ViewHolder {TextView mTv;ImageView imageView;public ViewHolder(View itemView) {super(itemView);mTv = (TextView) itemView.findViewById(R.id.item_demo5_tv);imageView = (ImageView)itemView.findViewById(R.id.item_demo5_image);}}
}
activity_demo5.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context="com.example.mac.mainapplication.Activity.Demo5Activity"><android.support.v7.widget.RecyclerViewandroid:layout_width="match_parent"android:layout_height="match_parent"android:scrollbars="vertical"android:paddingLeft="5dp"android:paddingTop="5dp"android:fadeScrollbars="true"android:id="@+id/demo5_recycler_view"></android.support.v7.widget.RecyclerView></LinearLayout>
主界面代码
public class Demo5Activity extends AppCompatActivity {private StaggeredGridLayoutManager mLayoutManager;private RecyclerView mRecyclerView;private ArrayList<String> data = new ArrayList<>();private Demo5Adapter adapter;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_demo5);mLayoutManager = new StaggeredGridLayoutManager(4, StaggeredGridLayoutManager.VERTICAL);mRecyclerView = (RecyclerView)findViewById(R.id.demo5_recycler_view);mRecyclerView.setLayoutManager(mLayoutManager);adapter = new Demo5Adapter(getData());mRecyclerView.setAdapter(adapter);}private ArrayList<String> getData() {final Random random=new Random();;String []strArr = {"差不多完美的解决了间隔线的问题,有细心的同学可能发现,在RecyclerView滑动的时候上面一直有一条灰色的间隔线,这个可以通过取消xml布局文件中RecyclerView的paddingTop属性去掉顶部灰色的间隔线。","总结","本篇文章主要介绍网格样式和瀑布流样式的RecyclerView,列表样式、网格样式和瀑布流样式在某种程度上是可以转换的。","网格样式的布局管理器的spanCount设置为1,就是列表样式","目前为止关于RecyclerView的基本使用的介绍可以告一段落了,但其实关于RecyclerView深入使用可不止着一些,比如说单个Item横滑,拖动Item之间转换位置等等,官方都有提供,当然这些使用会在后面依次介绍。","我是好人","就是水平方向的列表","如果觉得我的文章对您有用,请随意赞赏。您的支持将鼓励我继续创作!","竖直方向","Item是等宽,水平方向,那就是水平方向的网络样式","1","飞哥陪我过;歌撇根基稳固拼q"};for(int i = 0; i < 100; i++) {data.add(strArr[random.nextInt(strArr.length)]);}return data;}}