Android RecyclerView的使用(三)——瀑布流的实现
前言: RecyclerView提供了一种插拔式的体验,高度的解耦,异常的灵活,通过设置它提供的不同LayoutManager,ItemDecoration , ItemAnimator实现绚丽的效果。
- 通过布局管理器LayoutManager,可以控制其显示的方式;
- 通过ItemDecoration,可以控制Item间的间隔(可定制);
- 通过ItemAnimator,可以控制Item增删的动画;
- 至于点击、长按事件,RecyclerView并没有给我们提供相应的方法接口,需要我们自己实现。
上一节我们了解了如何为RecyclerView 添加分割线本节我们将学习如何实现瀑布流。
效果图:
1、 布局文件。
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/myRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.constraint.ConstraintLayout>
2、定义item的样式,item_single_imgview.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#442F2F2F"
android:layout_margin="3dp"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/imgView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
3、自定义Adapter适配器
public class StaggeredAdapter extends RecyclerView.Adapter<StaggeredAdapter.MyViewHolder> {
private LayoutInflater mInflater;
private Context mContext;
private int[] imgIds;
private List<Integer> heights;
public StaggeredAdapter(Context context, int[] imgIds) {
this.mContext = context;
this.imgIds = imgIds;
mInflater = LayoutInflater.from(context);
this.heights = new ArrayList<>();
for (int i = 0; i < datas.size(); i++) {
this.heights.add((int) (300 + Math.random()*300));
}
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = mInflater.inflate(R.layout.item_single_imgview, parent, false);
MyViewHolder viewHolder = new MyViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
ViewGroup.LayoutParams lp = holder.itemView.getLayoutParams();
lp.height = heights.get(position);
holder.itemView.setLayoutParams(lp);
holder.imageView.setBackgroundResource(imgIds[ position % imgIds.length]);
}
@Override
public int getItemCount() {
return mDatas.size();
}
class MyViewHolder extends RecyclerView.ViewHolder {
ImageView imageView;
public MyViewHolder(View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.imgView);
}
}
}
4、StaggeredActivity.java
public class StaggeredActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private int[] imgIds;
private StaggeredAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_staggered);
initDatas();
recyclerView = (RecyclerView) findViewById(R.id.myRecyclerView);
mAdapter = new StaggeredAdapter(this, imgIds);
recyclerView.setAdapter(mAdapter);
recyclerView.setLayoutManager(new StaggeredGridLayoutManager(3,
StaggeredGridLayoutManager.VERTICAL));
}
private void initDatas() {
imgIds = new int[]{R.mipmap.img01, R.mipmap.img02, R.mipmap.img03, R.mipmap.img04,
R.mipmap.img05, R.mipmap.img06, R.mipmap.img07};
}
}
就这样,我们实现了瀑布流,图片的高度是随机生成的。