RecyclerView 制作瀑布流图片

这是瀑布流的效果图

RecyclerView 制作瀑布流图片

RecyclerView(ListView的升级版)

加载图片用的是第三方图片加载框架

-------------------------------------

在Xml中

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v7.widget.RecyclerView android:id="@+id/gr_id"
android:layout_width="match_parent" android:layout_height="match_parent"/>
</RelativeLayout>

这是Activity 初始化组件

 public class MainActivity extends AppCompatActivity {

     @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); RecyclerView recyclerView = (RecyclerView) findViewById(R.id.gr_id);
//设置布局管理器
recyclerView.setLayoutManager(new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL));
//添加分隔符
recyclerView.addItemDecoration(new DividerGridItemDecoration(this)); MyAdapter adapter = new MyAdapter(this, Arrays.asList(ImageUrlArrays.IMAGE_URL_ARRAYS));
//设置Adapter
recyclerView.setAdapter(adapter); //为子条目设置点击事件
adapter.setOnRecyclerViewItemViewListener(new RecyclerViewItemViewListener() {
@Override
public void onClickListener(RecyclerView.ViewHolder viewHolder, int position) {
Toast.makeText(MainActivity.this, "点击事件:"+position, Toast.LENGTH_SHORT).show();
} @Override
public void onLongClickListener(RecyclerView.ViewHolder viewHolder, int position) {
Toast.makeText(MainActivity.this, "长按事件:"+position, Toast.LENGTH_SHORT).show();
}
});
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main,menu);
return super.onCreateOptionsMenu(menu);
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.clear_disk://清除硬盘缓存
Toast.makeText(this, "清除硬盘缓存!", Toast.LENGTH_SHORT).show();
WXHLImageLoader.getInstance().clearDiskCache();
break;
case R.id.clear_id://清除内存缓存
Toast.makeText(this, "清除内存缓存!", Toast.LENGTH_SHORT).show();
WXHLImageLoader.getInstance().clearMemoryCache();
break;
}
return super.onOptionsItemSelected(item);
}
}

需要设置数据就要newAdapter 这个Adapter是必须继承RecyclerView的Adapter 其中还要传入ViewHolder

这个ViewHolder也是要继承RecyclerView的ViewHolder

 class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>{

     private Context context;
private List<String> list;
private List<Integer> heightList;
private RecyclerViewItemViewListener listener ; public MyAdapter(Context context, List<String> list) {
this.context = context;
this.list = list; heightList = new ArrayList<>();
for (int i = 0; i < list.size() ; i++) {
int height = (int) (Math.random()*400 + 400);
heightList.add(height);
} } /**
* 给itemView监听器赋值
* @param listener
*/
public void setOnRecyclerViewItemViewListener(RecyclerViewItemViewListener listener){
this.listener = listener;
} int i = 0;
/**
* 创建ViewHolder对contentView进行复用
* @param parent
* @param viewType
* @return
*/
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
i++;
L.e("----------------onCreateViewHolder-----------i:"+i);
View contentView = LayoutInflater.from(context).inflate(R.layout.activity_main_item,parent,false);
return new ViewHolder(contentView);
} /**
* 进行数据绑定,itemView设置数据
* @param holder
* @param position
*/
@Override
public void onBindViewHolder(ViewHolder holder, int position) { //改变holder.button的高度
int height = heightList.get(position);
ViewGroup.LayoutParams lp = holder.imageView.getLayoutParams();
lp.height = height;
holder.imageView.setLayoutParams(lp); L.e("----------------onBindViewHolder-----------position:"+position);
WXHLImageLoader.getInstance().displayImage(list.get(position),holder.imageView);
holder.setPosition(position);
} @Override
public int getItemCount() {
return list.size();
} class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener,View.OnLongClickListener{
ImageView imageView;
private int position; public ViewHolder(View itemView) {
super(itemView);
//为ViewHolder中的组件进行赋值
imageView = (ImageView) itemView.findViewById(R.id.id_but); if(listener != null){//判断RecyclerViewItemViewListener是否为空,
// 若为空,不能给子条目设置点击事件,否则会出现空指针
itemView.setOnClickListener(this);
itemView.setOnLongClickListener(this);
}
} public void setPosition(int position) {
this.position = position;
} @Override
public void onClick(View v) {
switch (v.getId()){
case R.id.item_id://itemView的点击事件
listener.onClickListener(this,position);
break;
}
} @Override
public boolean onLongClick(View v) {//itemView的长按事件
listener.onLongClickListener(this,position);
return true;
}
}
}

在用RecyclerView时  是没有监听事件的  这个事件需要我们自己去写

新建一个接口

 public interface RecyclerViewItemViewListener {

     /**
* itemView的点击事件
* @param viewHolder
* @param position
*/
void onClickListener(RecyclerView.ViewHolder viewHolder, int position); /**
* 是itemView的长按事件
* @param viewHolder
* @param position
*/
void onLongClickListener(RecyclerView.ViewHolder viewHolder, int position); }
上一篇:(转)MoMo的Unity3D研究院之Assetbundle的原理(六十一)


下一篇:bash + script