Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android Recyclerview嵌套 列表裡面套列表

Android Recyclerview嵌套 列表裡面套列表

編輯:關於Android編程

最近,項目中遇到一個問題,就是列表顯示的嵌套,外面是一個RecyclerView列表,而其中一個單項竟然也是列表,產品設計的是否規范暫且不論,先想辦法實現吧,本以為這樣嵌套很難實現,結果寫下來也不過如此,雖然寫的時候內心很刺激。

方法:按照正常實現就可,那個單項布局裡放一個RecyclerView,在holder裡find到它,然後在給它接上一個適配器,也就是在外層列表Adapter中,再給它的單項子控件配一個Adapter,就問你刺激不???廢話少說,上代碼,客官請看…….

public class ContractDeatilAdapter extends
        RecyclerView.Adapter {

    private static final int TYPE_SIMPLE = 1;//普通簡易布局,只有兩個textview
    private static final int TYPE_INSTALMENT = 2;//分期付款的布局

    private Context mContext;
    private List valueList;
    private List keyList;
    private List payStagesList;

    public ContractDeatilAdapter(Context mContext, List valueList, List keyList,
                                 List payStagesList) {
        this.mContext = mContext;
        this.valueList = valueList;
        this.keyList = keyList;
        this.payStagesList = payStagesList;
    }

    @Override
    public ContractDetailHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(mContext).inflate(R.layout.item_contract_detail_instalment, parent, false);
        return new ContractInstalmentHolder(itemView);
    }


    @Override
    public int getItemCount() {
        if (valueList != null && payStagesList != null) {
            return valueList.size() + payStagesList.size();
        }
        return 0;
    }

    @Override
    public void onBindViewHolder(ContractDetailHolder holder, int position) {
            ContractInstalmentHolder instalmentHolder = (ContractInstalmentHolder) holder;

            int stagePosition = position - valueList.size();
            StagePayDetailAdapter mRefundAdapter = new StagePayDetailAdapter(mContext,
                    payStagesList.get(stagePosition).getRentpactpaylists());
            ((ContractInstalmentHolder) holder).rvRefundDetail.setLayoutManager(new LinearLayoutManager(mContext){
                @Override
                public boolean canScrollVertically() {
                    return false;
                }
            });
            ((ContractInstalmentHolder) holder).rvRefundDetail.setAdapter(mRefundAdapter);
        }
    }

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



    /**
     * 分期付款詳情 的holder
     */
    class ContractInstalmentHolder extends ContractDetailHolder {
        TextView tvGrade;
        LinearLayout layoutTitle;
        TextView tvPay, tvBeginDate, tvPayDate, tvOverDate;
        RecyclerView rvRefundDetail;
        TextView tvMemo;

        public ContractInstalmentHolder(View itemView) {
            super(itemView);
            layoutTitle = (LinearLayout) itemView.findViewById(R.id.layout_title);
            tvGrade = (TextView) itemView.findViewById(R.id.tv_grade);
            tvPay = (TextView) itemView.findViewById(R.id.tv_pay);
            tvBeginDate = (TextView) itemView.findViewById(R.id.tv_begindate);
            tvPayDate = (TextView) itemView.findViewById(R.id.tv_paydate);
            tvOverDate = (TextView) itemView.findViewById(R.id.tv_overdate);
            rvRefundDetail = (RecyclerView) itemView.findViewById(R.id.rv_refunddetail);
            tvMemo = (TextView) itemView.findViewById(R.id.tv_memo);
        }

    }
}
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved