天天看點

RecyclerView 對Item的分類操作Android List的Item顯示不一緻 RecyclerView的使用

Android List的Item顯示不一緻 RecyclerView的使用

在我做過的用戶端裡面,還沒有一個用戶端的LIST的item是不一樣的

例如:

RecyclerView 對Item的分類操作Android List的Item顯示不一緻 RecyclerView的使用

如圖顯示。~~~均為字型圈圈忽略,主要不同的是框框(圖檔)的位置和張數。

首先,要想到這裡面一樣的有哪些。

  • 标題(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);

        }
    }
           

上結果圖

RecyclerView 對Item的分類操作Android List的Item顯示不一緻 RecyclerView的使用

歐拉,到這就完了。

PS:現在View執行個體化不用轉型了哈????~~~~

繼續閱讀