前面两篇文章主要讲了用RecyclerView实现ListView和GridView,这篇文章主要探讨一下RecyclerView 的 瀑布流使用。利用RecyclerView可以很方便地实现瀑布流。 那么如何实现?其实你什么都不用做,只要使用StaggeredGridLayoutManager我们就已经实现了,只是上面的item布局我们使用了固定的高度,下面我们仅仅在适配器的onBindViewHolder方法中为我们的item设置个随机的高度。具体方法如下:
1.主Activity的布局和item的布局如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context="com.example.ahuang.recyclerview.StaggeredViewActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
</pre><pre code_snippet_id="1561535" snippet_file_name="blog_20160121_3_8563476" name="code" class="java"><pre name="code" class="java"><?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:layout_margin="5dp"
android:background="#FFC125"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="100dp"
android:layout_height="100dp"
android:gravity="center"
android:layout_gravity="center"
android:text="1"
/>
</LinearLayout>
2.适配器代码如下:
package com.example.ahuang.recyclerview;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
/**
* Created by ahuang on 2016/1/19.
*/
public class StaggeredHomeAdapter extends RecyclerView.Adapter<StaggeredHomeAdapter.MyViewHolder>{
Context context;
List<String> list;
private List<Integer> mHeight; // 定义了一个集合,存放高度信息
public StaggeredHomeAdapter(Context context,List<String> list){
this.context=context;
this.list=list;
mHeight=new ArrayList<Integer>();
for(int i=0;i<list.size();i++){
mHeight.add((int)(100+Math.random()*300)); //对高度信息随机赋值
}
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
MyViewHolder holder=new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.lv_item_layout,parent,false));
return holder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
ViewGroup.LayoutParams lp=holder.tv.getLayoutParams(); //获取TextView的属性
lp.height=mHeight.get(position); //获得高度属性,并将随机生成的高度属性作用于高度
holder.tv.setLayoutParams(lp); //设置新的属性
holder.tv.setText(list.get(position));
}
@Override
public int getItemCount() {
return list.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder{
TextView tv;
public MyViewHolder(View itemView) {
super(itemView);
tv=(TextView) itemView.findViewById(R.id.textView);
}
}
}
3.主Activity中实现。
mRecyclerView=(RecyclerView) findViewById(R.id.recyclerView);
mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL));
mRecyclerView.setAdapter(new StaggeredHomeAdapter(this, list));
效果图
ItemAnimator
This class defines the animations that take place on items as changes are made to the adapter. Subclasses of ItemAnimator can be used to implement custom animations for actions on ViewHolder items. The RecyclerView will manage retaining these items while they are being animated, but implementors must call the appropriate "Starting" (dispatchRemoveStarting(ViewHolder)
, dispatchMoveStarting(ViewHolder)
, dispatchChangeStarting(ViewHolder, boolean)
, ordispatchAddStarting(ViewHolder)
) and "Finished" (dispatchRemoveFinished(ViewHolder)
, dispatchMoveFinished(ViewHolder)
, dispatchChangeFinished(ViewHolder, boolean)
, or dispatchAddFinished(ViewHolder)
) methods when each item animation is being started and ended.
RecyclerView 可以配置item增加删除的动画。
关于增加,删除的ActionBar的添加,请参考另一篇文章http://blog.csdn.net/baojie0327/article/details/50564180
By default, RecyclerView uses DefaultItemAnimator
默认情况下,RecyclerView使用的是DefaultItemAnimator,所以只要在Activity中加入以下代码:
mRecyclerView.setItemAnimator(new DefaultItemAnimator());