RecyclerView 制作瀑布流图片
这是瀑布流的效果图
RecyclerView(ListView的升级版)
加载图片用的是第三方图片加载框架
-------------------------------------
在Xml中
1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:id="@+id/activity_main" 4 android:layout_width="match_parent" android:layout_height="match_parent"> 5 6 <android.support.v7.widget.RecyclerView android:id="@+id/gr_id" 7 android:layout_width="match_parent" android:layout_height="match_parent"/> 8 </RelativeLayout>
这是Activity 初始化组件
1 public class MainActivity extends AppCompatActivity { 2 3 @Override 4 protected void onCreate(Bundle savedInstanceState) { 5 super.onCreate(savedInstanceState); 6 setContentView(R.layout.activity_main); 7 8 RecyclerView recyclerView = (RecyclerView) findViewById(R.id.gr_id); 9 //设置布局管理器 10 recyclerView.setLayoutManager(new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL)); 11 //添加分隔符 12 recyclerView.addItemDecoration(new DividerGridItemDecoration(this)); 13 14 MyAdapter adapter = new MyAdapter(this, Arrays.asList(ImageUrlArrays.IMAGE_URL_ARRAYS)); 15 //设置Adapter 16 recyclerView.setAdapter(adapter); 17 18 //为子条目设置点击事件 19 adapter.setOnRecyclerViewItemViewListener(new RecyclerViewItemViewListener() { 20 @Override 21 public void onClickListener(RecyclerView.ViewHolder viewHolder, int position) { 22 Toast.makeText(MainActivity.this, "点击事件:"+position, Toast.LENGTH_SHORT).show(); 23 } 24 25 @Override 26 public void onLongClickListener(RecyclerView.ViewHolder viewHolder, int position) { 27 Toast.makeText(MainActivity.this, "长按事件:"+position, Toast.LENGTH_SHORT).show(); 28 } 29 }); 30 } 31 32 @Override 33 public boolean onCreateOptionsMenu(Menu menu) { 34 getMenuInflater().inflate(R.menu.main,menu); 35 return super.onCreateOptionsMenu(menu); 36 } 37 38 @Override 39 public boolean onOptionsItemSelected(MenuItem item) { 40 switch (item.getItemId()){ 41 case R.id.clear_disk://清除硬盘缓存 42 Toast.makeText(this, "清除硬盘缓存!", Toast.LENGTH_SHORT).show(); 43 WXHLImageLoader.getInstance().clearDiskCache(); 44 break; 45 case R.id.clear_id://清除内存缓存 46 Toast.makeText(this, "清除内存缓存!", Toast.LENGTH_SHORT).show(); 47 WXHLImageLoader.getInstance().clearMemoryCache(); 48 break; 49 } 50 return super.onOptionsItemSelected(item); 51 } 52 }
需要设置数据就要newAdapter 这个Adapter是必须继承RecyclerView的Adapter 其中还要传入ViewHolder
这个ViewHolder也是要继承RecyclerView的ViewHolder
1 class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>{ 2 3 private Context context; 4 private List<String> list; 5 private List<Integer> heightList; 6 private RecyclerViewItemViewListener listener ; 7 8 public MyAdapter(Context context, List<String> list) { 9 this.context = context; 10 this.list = list; 11 12 heightList = new ArrayList<>(); 13 for (int i = 0; i < list.size() ; i++) { 14 int height = (int) (Math.random()*400 + 400); 15 heightList.add(height); 16 } 17 18 } 19 20 /** 21 * 给itemView监听器赋值 22 * @param listener 23 */ 24 public void setOnRecyclerViewItemViewListener(RecyclerViewItemViewListener listener){ 25 this.listener = listener; 26 } 27 28 int i = 0; 29 /** 30 * 创建ViewHolder对contentView进行复用 31 * @param parent 32 * @param viewType 33 * @return 34 */ 35 @Override 36 public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 37 i++; 38 L.e("----------------onCreateViewHolder-----------i:"+i); 39 View contentView = LayoutInflater.from(context).inflate(R.layout.activity_main_item,parent,false); 40 return new ViewHolder(contentView); 41 } 42 43 /** 44 * 进行数据绑定,itemView设置数据 45 * @param holder 46 * @param position 47 */ 48 @Override 49 public void onBindViewHolder(ViewHolder holder, int position) { 50 51 //改变holder.button的高度 52 int height = heightList.get(position); 53 ViewGroup.LayoutParams lp = holder.imageView.getLayoutParams(); 54 lp.height = height; 55 holder.imageView.setLayoutParams(lp); 56 57 L.e("----------------onBindViewHolder-----------position:"+position); 58 WXHLImageLoader.getInstance().displayImage(list.get(position),holder.imageView); 59 holder.setPosition(position); 60 } 61 62 @Override 63 public int getItemCount() { 64 return list.size(); 65 } 66 67 68 class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener,View.OnLongClickListener{ 69 ImageView imageView; 70 private int position; 71 72 public ViewHolder(View itemView) { 73 super(itemView); 74 //为ViewHolder中的组件进行赋值 75 imageView = (ImageView) itemView.findViewById(R.id.id_but); 76 77 if(listener != null){//判断RecyclerViewItemViewListener是否为空, 78 // 若为空,不能给子条目设置点击事件,否则会出现空指针 79 itemView.setOnClickListener(this); 80 itemView.setOnLongClickListener(this); 81 } 82 } 83 84 public void setPosition(int position) { 85 this.position = position; 86 } 87 88 @Override 89 public void onClick(View v) { 90 switch (v.getId()){ 91 case R.id.item_id://itemView的点击事件 92 listener.onClickListener(this,position); 93 break; 94 } 95 } 96 97 @Override 98 public boolean onLongClick(View v) {//itemView的长按事件 99 listener.onLongClickListener(this,position); 100 return true; 101 } 102 } 103 }
在用RecyclerView时 是没有监听事件的 这个事件需要我们自己去写
新建一个接口
1 public interface RecyclerViewItemViewListener { 2 3 /** 4 * itemView的点击事件 5 * @param viewHolder 6 * @param position 7 */ 8 void onClickListener(RecyclerView.ViewHolder viewHolder, int position); 9 10 /** 11 * 是itemView的长按事件 12 * @param viewHolder 13 * @param position 14 */ 15 void onLongClickListener(RecyclerView.ViewHolder viewHolder, int position); 16 17 }