1. 程式人生 > >recyclerview載入不同的佈局

recyclerview載入不同的佈局



public class BlackLogSixRVAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    Context context;
    int max = 3;
    private static final int TOP = 0;
    private static final int BOTTOM = 2;
    private static final int ITEM = 1;
  

    public BlackLogSixRVAdapter(Context context) {
        this.context = context;
    }


    private RecyclerView.ViewHolder getViewHolderByViewType(int viewType) {

        RecyclerView.ViewHolder holder = null;
        switch (viewType) {
            case TOP:
                holder = new BlackLogSixRVTopViewHolder(LayoutInflater.from(context).inflate(R.layout.rv_blacklogsix_item_top, null));
                break;
            case BOTTOM:
                holder = new BlackLogSixRVBottomViewHolder(LayoutInflater.from(context).inflate(R.layout.rv_blacklogsix_item_bottom, null));
                break;
            case ITEM:
                holder = new BlackLogSixRVItemViewHolder(LayoutInflater.from(context).inflate(R.layout.rv_backlogsix_item, null));
                break;
        }
        return holder;
    }


    @Override
    public int getItemCount() {
        return 4;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        return getViewHolderByViewType(i);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i) {
        switch (viewHolder.getItemViewType()) { //此處獲取條項的介面標示  與條項id不同
            case TOP:


                break;
            case BOTTOM:
                ((BlackLogSixRVBottomViewHolder) viewHolder).btn_personal_bls_3.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        iBlackLogSixPersonalPresenter.toAlertDialog();
                    }
                });

                break;
            case ITEM:

                break;
        }

    }

    @Override
    public int getItemViewType(int position) { //對某一條設定viewtype
        int viewType = 0;
        if (position == 0) {
            viewType = TOP;
        } else if (position == max) {
            viewType = BOTTOM;
        } else {
            viewType = ITEM;
        }
        return viewType;
    }

    class BlackLogSixRVTopViewHolder extends RecyclerView.ViewHolder {
      
        public BlackLogSixRVTopViewHolder(View itemView) {
            super(itemView);
        }
    }

    class BlackLogSixRVItemViewHolder extends RecyclerView.ViewHolder {
      

        public BlackLogSixRVItemViewHolder(View itemView) {
            super(itemView);
        }
    }

    class BlackLogSixRVBottomViewHolder extends RecyclerView.ViewHolder {

        public BlackLogSixRVBottomViewHolder(View itemView) {
            super(itemView);
        }
    }
}