[Android Pro] RecyclerView实现瀑布流效果(二)

referece to : http://blog.csdn.net/u010687392

在上篇中我们知道RecyclerView中默认给我们提供了三种布局管理器,分别是LinearLayoutManager、 GridLayoutManager、StaggeredGridLayoutManager。其中 StaggeredGridLayoutManager可实现交错式网格布局,正好可以用来实现瀑布流。

下面先看看效果图吧,再贴代码:

[Android Pro]   RecyclerView实现瀑布流效果(二)

其中大部分内容实现其实是和上篇文章是一样的,就不多叙述了,就一个地方不同,就是我们在适配器中绑定ViewHolder的方法中需要重新给我们的itemView布局设置height,我这里是生成随机数来设置高度的,代码实现为:

private void getRandomHeight(List<String> lists){//得到随机item的高度
heights = new ArrayList<>();
for (int i = 0; i < lists.size(); i++) {
heights.add((int)(200+Math.random()*400));
}
}

在onBindViewHolder方法中:

ViewGroup.LayoutParams params =  holder.itemView.getLayoutParams();//得到item的LayoutParams布局参数
params.height = heights.get(position);//把随机的高度赋予itemView布局
holder.itemView.setLayoutParams(params);//把params设置给itemView布局

从而实现瀑布流效果。

贴一下代码吧:

MainActivity.java

public class MainActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private List<String> lists;
private MyRecyclerAdapter adapter; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initData();
mRecyclerView = (RecyclerView) this.findViewById(R.id.recyclerView);
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
// mRecyclerView.addItemDecoration();//设置分割线
mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));//设置RecyclerView布局管理器为2列垂直排布
adapter = new MyRecyclerAdapter(this,lists);
mRecyclerView.setAdapter(adapter);
adapter.setOnClickListener(new MyRecyclerAdapter.OnItemClickListener() {
@Override
public void ItemClickListener(View view, int postion) {
Toast.makeText(MainActivity.this,"点击了:"+postion,Toast.LENGTH_SHORT).show();
}
@Override
public void ItemLongClickListener(View view, int postion) {
//长按删除
lists.remove(postion);
adapter.notifyItemRemoved(postion);
}
});
} private void initData() {
lists = new ArrayList();
for (int i = 0; i < 100; i++) {
lists.add("" + i);
}
} }

MyRecyclerAdapter.java

public class MyRecyclerAdapter extends RecyclerView.Adapter<MyViewHolder> {
private List<String> lists;
private Context context;
private List<Integer> heights;
private OnItemClickListener mListener;
public MyRecyclerAdapter(Context context,List<String> lists) {
this.context = context;
this.lists = lists;
getRandomHeight(this.lists);
}
private void getRandomHeight(List<String> lists){//得到随机item的高度
heights = new ArrayList<>();
for (int i = 0; i < lists.size(); i++) {
heights.add((int)(200+Math.random()*400));
}
}
public interface OnItemClickListener{
void ItemClickListener(View view,int postion);
void ItemLongClickListener(View view,int postion);
}
public void setOnClickListener(OnItemClickListener listener){
this.mListener = listener;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.item,parent,false);
MyViewHolder viewHolder = new MyViewHolder(view);
return viewHolder;
} @Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
ViewGroup.LayoutParams params = holder.itemView.getLayoutParams();//得到item的LayoutParams布局参数
params.height = heights.get(position);//把随机的高度赋予item布局
holder.itemView.setLayoutParams(params);//把params设置给item布局 holder.mTv.setText(lists.get(position));//为控件绑定数据
if(mListener!=null){//如果设置了监听那么它就不为空,然后回调相应的方法
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int pos = holder.getLayoutPosition();//得到当前点击item的位置pos
mListener.ItemClickListener(holder.itemView,pos);//把事件交给我们实现的接口那里处理
}
});
holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
int pos = holder.getLayoutPosition();//得到当前点击item的位置pos
mListener.ItemLongClickListener(holder.itemView,pos);//把事件交给我们实现的接口那里处理
return true;
}
});
}
} @Override
public int getItemCount() {
return lists.size();
}
}
class MyViewHolder extends RecyclerView.ViewHolder{
TextView mTv;
public MyViewHolder(View itemView) {
super(itemView);
mTv = (TextView) itemView.findViewById(R.id.textView);
}
}

源码下载地址:http://www.it165.net/uploadfile/files/2015/0705/recyclerView2.rar

转载请注明出处-http://blog.csdn.net/u010687392

上一篇:Topcoder Arena插件配置和训练指南


下一篇:WAMPserver配置(允许外部访问、phpmyadmin设置为输入用户名密码才可登录等)