Android List的Item显示不一致 RecyclerView的使用
在我做过的客户端里面,还没有一个客户端的LIST的item是不一样的
例如:
如图显示。~~~均为字体圈圈忽略,主要不同的是框框(图片)的位置和张数。
首先,要想到这里面一样的有哪些。
- 标题(title)
- 图片(image)
- 下面三个一样的东东
那么不一样的就是图片的张数,那么在建立JavaBean的时候可以将图片看做List集。即
private List<ImageView> imgs;
//or
private List<BitMap> imgs;
接下来都是一样的,下面才是重点。
我们有两个布局文件,那么就有两个xml(废话)
不急,我们需要根据item的需要设置布局Item的样子1和2
接着RecyclerView.Adapter 有一个方法叫做
@Override
public int getItemViewType(int position) {
return super.getItemViewType(position);
}
在这里怎么用呢?我们发现在
onCreateViewHolder(ViewGroup parent, int viewType)
方法里看到两个参数,父对象和viewType。嘿,这不就是上面传的参数么。对,我们就在这里判断然后给相应的item返回相应的ViewHolder.
贴上ViewHolder的代码.
/**
*
* 有三张图的ViewHolder
*/
public class ThreeViewHolder extends RecyclerView.ViewHolder{
public ThreeViewHolder(View itemView) {
super(itemView);
}
}
/**
*
* 只有一张图的ViewHolder
*/
public class OneViewHolder extends RecyclerView.ViewHolder {
OneViewHolder(View itemView) {
super(itemView);
}
}
接着我们在
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position){
//这里写上bind数据的代码
}
贴上全部代码
public class MyRecycleAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static final int TYPE_1_PIC = ;
private static final int TYPE_3_PIC = ;
private List<Beans> mDates;
private LayoutInflater layoutInflater;
private Context mContex;
public MyRecycleAdapter(Context context, List<Beans> dates) {
this.mDates = dates;
this.mContex = context;
layoutInflater = LayoutInflater.from(context);
}
//定义一个接口,传入两个点击方法
public interface OnItemClickListener {
void onItemClick(View view, int position);
void onItemLongClick(View view, int position);
}
private OnItemClickListener onItemClickListener;//定义
//获取方法类
public void setOnItemClickListener(OnItemClickListener listener) {
this.onItemClickListener = listener;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch (viewType){
case TYPE_1_PIC:
View view1 = layoutInflater.inflate(R.layout.shouye_item2, parent, false);
return new OneViewHolder(view1);
case TYPE_3_PIC:
View view2 = layoutInflater.inflate(R.layout.shouye_item1, parent, false);
return new ThreeViewHolder(view2);
}
return null;
}
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
final Beans bean = mDates.get(position);
if(holder instanceof OneViewHolder){
//一张图
((OneViewHolder) holder).title.setText(bean.getTitle());
((OneViewHolder) holder).imageView.setImageBitmap(bean.getShowPic().get());
((OneViewHolder) holder).category.setText(bean.getCategory());
((OneViewHolder) holder).evaluate.setText(bean.getEvaluate());
((OneViewHolder) holder).explore.setText(bean.getLooknum());
}else {
//三张图
((ThreeViewHolder) holder).title.setText(bean.getTitle());
((ThreeViewHolder) holder).imageView1.setImageBitmap(bean.getShowPic().get());
((ThreeViewHolder) holder).imageView2.setImageBitmap(bean.getShowPic().get());
((ThreeViewHolder) holder).imageView3.setImageBitmap(bean.getShowPic().get());
((ThreeViewHolder) holder).category.setText(bean.getCategory());
((ThreeViewHolder) holder).evaluate.setText(bean.getEvaluate());
((ThreeViewHolder) holder).explore.setText(bean.getLooknum());
}
if (onItemClickListener != null) {
holder.itemView.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void onClick(View v) {
int layoutPos = holder.getLayoutPosition();
onItemClickListener.onItemClick(holder.itemView, layoutPos);
}
});
holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
int layoutPos = holder.getLayoutPosition();
onItemClickListener.onItemLongClick(holder.itemView, layoutPos);
return false;
}
});
}
}
@Override
public int getItemCount() {
return mDates.size();
}
@Override
public int getItemViewType(int position) {
if(mDates.get(position).getShowPic().size()>){
return TYPE_3_PIC;
}else return TYPE_1_PIC;
}
/**
*
* 有三张图的ViewHolder
*/
public class ThreeViewHolder extends RecyclerView.ViewHolder{
private TextView title;
private ImageView imageView1;
private ImageView imageView2;
private ImageView imageView3;
private TextView category;
private TextView explore;
private TextView evaluate;
public ThreeViewHolder(View itemView) {
super(itemView);
title = itemView.findViewById(R.id.item1_title);
imageView1 = itemView.findViewById(R.id.item1_img1);
imageView2 = itemView.findViewById(R.id.item1_img2);
imageView3 = itemView.findViewById(R.id.item1_img3);
category = itemView.findViewById(R.id.item1_category);
explore = itemView.findViewById(R.id.item1_explore);
evaluate = itemView.findViewById(R.id.item1_evaluate);
}
}
/**
*
* 只有一张图的ViewHolder
*/
public class OneViewHolder extends RecyclerView.ViewHolder {
private TextView title;
private ImageView imageView;
private TextView category;
private TextView explore;
private TextView evaluate;
OneViewHolder(View itemView) {
super(itemView);
title = itemView.findViewById(R.id.item2_title);
imageView = itemView.findViewById(R.id.item2_img);
category = itemView.findViewById(R.id.item2_category);
explore = itemView.findViewById(R.id.item2_explore);
evaluate = itemView.findViewById(R.id.item2_evaluate);
}
}
上结果图
欧拉,到这就完了。
PS:现在View实例化不用转型了哈????~~~~