Android Recyclerview嵌套 列表里面套列表

最近,项目中遇到一个问题,就是列表显示的嵌套,外面是一个RecyclerView列表,而其中一个单项竟然也是列表,产品设计的是否规范暂且不论,先想办法实现吧,本以为这样嵌套很难实现,结果写下来也不过如此,虽然写的时候内心很刺激。

方法:按照正常实现就可,那个单项布局里放一个RecyclerView,在holder里find到它,然后在给它接上一个适配器,也就是在外层列表Adapter中,再给它的单项子控件配一个Adapter,就问你刺激不???废话少说,上代码,客官请看…….

public class ContractDeatilAdapter extends
        RecyclerView.Adapter<ContractDeatilAdapter.ContractDetailHolder> {

    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);
        }

    }
}

你可能感兴趣的:(问题解决)