先在gradle产出中加入
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:background="#b120a5"
android:gravity="center"
android:orientation="vertical"
android:padding="20dp">
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/tv_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="3dp"
android:text="1" />
</LinearLayout>
compile 'com.android.support:recyclerview-v7:21.+'
compile 'com.android.support:cardview-v7:21.+'
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycle); //初始化数据 initData(); recycleAdapter = new MyRecyclerAdapter(MainActivity.this, mDatas); LinearLayoutManager layoutManager = new LinearLayoutManager(this); //设置布局管理器 StaggeredGridLayoutManager layout = new StaggeredGridLayoutManager(10, StaggeredGridLayoutManager.HORIZONTAL); recyclerView.setLayoutManager(layout); // recyclerView.setLayoutManager(layoutManager); //设置为垂直布局,这也是默认的 layoutManager.setOrientation(OrientationHelper.HORIZONTAL); //设置Adapter recyclerView.setAdapter(recycleAdapter); //设置增加或删除条目的动画 recyclerView.setItemAnimator(new DefaultItemAnimator()); recycleAdapter.SetRecyclerViewOnClickListener(new MyRecyclerAdapter.ProData() { @Override public void returnData(String str) { Toast.makeText(MainActivity.this, "返回" + str, Toast.LENGTH_SHORT).show(); } });
package cn.beijing.laundry.demo;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.List;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.MyViewHolder> {
private List<String> mDatas;
private Context mContext;
private LayoutInflater inflater;
public MyRecyclerAdapter(Context context, List<String> datas) {
this.mContext = context;
this.mDatas = datas;
inflater = LayoutInflater.from(mContext);
}
@Override
public int getItemCount() {
return mDatas.size();
} //填充onCreateViewHolder方法返回的holder中的控件
@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
String aNull = mDatas.get(position).replace("null", "");
String replace = aNull.replace("+", "/");
holder.tv.setText(replace);
FileInputStream fis = null;
try {
fis = new FileInputStream(replace);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Bitmap bitmap = BitmapFactory.decodeStream(fis);
holder.image.setImageBitmap(bitmap);
holder.image.setTag(position);
} //重写onCreateViewHolder方法,返回一个自定义的ViewHolder
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.item_home, parent, false);
final MyViewHolder holder = new MyViewHolder(view);
holder.image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int tag = (int) holder.image.getTag();
proData.returnData(tag + "");
Toast.makeText(mContext, "点击" + tag, Toast.LENGTH_SHORT).show();
}
});
return holder;
}
class MyViewHolder extends ViewHolder {
TextView tv;
ImageView image;
public MyViewHolder(View view) {
super(view);
tv = (TextView) view.findViewById(R.id.tv_item);
image = (ImageView) view.findViewById(R.id.image);
}
}
ProData proData;
public void SetRecyclerViewOnClickListener(ProData proData) {
this.proData = proData;
}
public interface ProData {
public void returnData(String str);
}
}
这些完成还差一个适配器记住RecyclerView不像ListView的他并没有点击事件所以在适配器中须自己写点击事件
最后贴出XML文件