//RecyclerView的适配器
compile 'com.android.support:recyclerview-v7:26.+' 依赖根据版本添加
public class FenleiRecle extends RecyclerView.Adapter<FenleiRecle.MyVIewHolder>{
private Context context;
private ArrayList<Fenleifx> arr;
public FenleiRecle(Context context,ArrayList<Fenleifx> arr){
this.context=context;
this.arr=arr;
Log.e("chen", "MyVIewHolder: 7777777"+"11111111111111" );
}
@Override
public MyVIewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = View.inflate(context, R.layout.fenlei, null);
MyVIewHolder myVIewHolder = new MyVIewHolder(view);
Log.e("chen", "MyVIewHolder: 7777777"+"222222222222" );
return myVIewHolder;
}
@Override
public void onBindViewHolder(MyVIewHolder holder, int position) {
ImageView iv = holder.iv;
ImageLoader.getInstance().displayImage(arr.get(position).getImg(),iv);
holder.tv.setText(arr.get(position).getName());
Log.e("chen", "MyVIewHolder: 7777777"+"33333333333" );
}
@Override
public int getItemCount() {
return arr.size();
}
class MyVIewHolder extends RecyclerView.ViewHolder{
TextView tv;
ImageView iv;
public MyVIewHolder(View itemView) {
super(itemView);
tv = itemView.findViewById(R.id.tv);
iv = itemView.findViewById(R.id.iv);
Log.e("chen", "MyVIewHolder: 44444444444"+tv );
}
}
}
//使用RecyclerView
RecyclerView viewById = getActivity().findViewById(R.id.cxrcv);
GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 2, GridLayoutManager.HORIZONTAL, false);
viewById.setLayoutManager(gridLayoutManager);
viewById.setAdapter(new FenleiRecle(getContext(),arrayList));