android成神之路Android技术知识Android知识

RecyclerView.Adaptert通过viewType动

2016-10-27  本文已影响765人  风再起时ME

用了RecyclerView一段时间了,最近才知道是可以通过viewType来动态改变每个item的样式的。整理记录一下。

步骤如下:

1.继承RecyclerView.Adapter

public class ZhihuAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
   ····
}

2.重写 getItemViewType(int position)

   ···
@Override
public int getItemViewType(int position) {
    if (position < getDataItemCount()
            && getDataItemCount() > 0) {

        return NOMAL_ITEM;
    }
    return TYPE_LOADING_MORE;
}
   ···

根据item的坐标位置position,来返回不同的viewType。

3.在 onCreateViewHolder(ViewGroup parent, int viewType) 判断使用哪一种布局

   ···
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    switch (viewType) {
        case NOMAL_ITEM:
            return new ZhihuViewHolder(LayoutInflater.from(mContext).inflate(R.layout.zhihu_layout_item, parent, false));

        case TYPE_LOADING_MORE:
            return new LoadingMoreHolder(LayoutInflater.from(mContext).inflate(R.layout.infinite_loading, parent, false));

    }
    return null;
}
   ···

根据不同的viewType,来生成不同的RecyclerView.ViewHolder,使用不同的布局。

4.定义对应的ViewHolder类

   ···
public static class LoadingMoreHolder extends RecyclerView.ViewHolder {
    ProgressBar progressBar;

    public LoadingMoreHolder(View itemView) {
        super(itemView);
        progressBar = (ProgressBar) itemView;
    }
}

class ZhihuViewHolder extends RecyclerView.ViewHolder {
    final TextView textView;
    final LinearLayout linearLayout;
    ImageView imageView;

    ZhihuViewHolder(View itemView) {
        super(itemView);
        imageView = (ImageView) itemView.findViewById(R.id.item_image_id);
        textView = (TextView) itemView.findViewById(R.id.item_text_id);
        linearLayout = (LinearLayout) itemView.findViewById(R.id.zhihu_item_layout);
    }
}
   ···

5.在onBindViewHolder(RecyclerView.ViewHolder holder, int position)绑定数据

   ···
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    int type = getItemViewType(position);
    switch (type) {
        case NOMAL_ITEM:
            bindViewHolderNormal((ZhihuViewHolder) holder, position);
            break;
        case TYPE_LOADING_MORE:
            bindLoadingViewHold((LoadingMoreHolder) holder, position);
            break;
    }
}

private void bindLoadingViewHold(LoadingMoreHolder holder, int position) {
    holder.progressBar.setVisibility(showLoadingMore == true ? View.VISIBLE : View.INVISIBLE);
}

private void bindViewHolderNormal(final ZhihuViewHolder holder, final int position) {

    final ZhihuDailyItem zhihuDailyItem = zhihuDailyItems.get(holder.getAdapterPosition());
    
    if (DBUtils.getDB(mContext).isRead(Config.ZHIHU, zhihuDailyItem.getId(), 1))
        holder.textView.setTextColor(Color.GRAY);
    else
        holder.textView.setTextColor(Color.BLACK);
    
    holder.textView.setText(zhihuDailyItem.getTitle());
    holder.linearLayout.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    goDescribeActivity(holder,zhihuDailyItem);
    //                        Toast.makeText(mContext, "tetttt", Toast.LENGTH_SHORT).show();
                }
            });
    
    Glide.with(mContext)
            .load(zhihuDailyItems.get(position).getImages()[0])
            .diskCacheStrategy(DiskCacheStrategy.SOURCE)
            .centerCrop().override(widthPx, heighPx)
            .into(holder.imageView);
}
   ···

6.最后记录一点

在RecyclerView中,使用notifyItemInserted(int)notifyItemRemoved(int)会有动画效果, 而使用notifyDataSetChanged()则没有。

参考:

http://www.jianshu.com/p/9165249da2fa

上一篇下一篇

猜你喜欢

热点阅读