0

我有一个像 A{a1, a2, a3...aN}, B{B1, BM}, .... O{O1, o2, ... OP} 这样的嵌套列表。该列表具有随机数量的成员。每个成员也是随机的成员列表。这将如下图所示。如您所见,外部单元格的宽度和高度不是固定的。我想如果我使用嵌套的 recyclerview 可能有可能。它有点像 StaggeredGridLayoutManager。但据我了解, StaggeredGridLayoutManager 应该有一个宽度或高度应该是固定的。有什么建议如何实现这种 UI 设计? 这就是我想要展示的

这是我使用 StaggeredGridLayoutManager 时显示的内容

这是外部列表适配器 /********** 类 GroupAdapter 扩展 RecyclerView.Adapter{

    @Override
    public GroupHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.cell_group, null);
        GroupHolder ih = new GroupHolder(v);
        return ih;
    }

    @Override
    public void onBindViewHolder(GroupHolder holder, int position) {
        if(mGroups!=null && mGroups.size()>position) {
            GroupObject group = mGroups.get(position);
            ArrayList<CandidateObject> cans = group.getCandidates();
            CandidateAdapter adapter = new CandidateAdapter(cans);
            CustomLinearLayoutManager mLinearManager = new CustomLinearLayoutManager(MainActivity.this);
            mLinearManager.setAutoMeasureEnabled(true);
            holder.rvCandis.setLayoutManager(mLinearManager);
            holder.tvTitle.setText(group.getName());
            holder.rvCandis.setAdapter(adapter);
        }
    }

    @Override
    public int getItemCount() {
        return mGroups.size();
    }
}

private static class GroupHolder extends RecyclerView.ViewHolder {
    private TextView tvTitle;
    private RecyclerView rvCandis;
    public int index;

    private GroupHolder(View v) {
        super(v);
        this.tvTitle = (TextView) v.findViewById(R.id.tv_partyname);
        this.rvCandis = (RecyclerView) v.findViewById(R.id.rvCandi);
    }
}

这是内部列表适配器类 CandidateAdapter extends RecyclerView.Adapter{ ArrayList candis;

    CandidateAdapter(ArrayList<CandidateObject> candis){
        this.candis = candis;
    }

    @Override
    public CandidateHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.cell_candidate, null);
        CandidateHolder ih = new CandidateHolder(v);
        return ih;
    }

    @Override
    public void onBindViewHolder(CandidateHolder holder, int position) {
        if(candis!=null && candis.size()>position) {
            CandidateObject c = candis.get(position);
            holder.tvName.setText(c.getName());
        }
    }

    @Override
    public int getItemCount() {
        return candis.size();
    }
}

private static class CandidateHolder extends RecyclerView.ViewHolder {
    private ImageView ivThumb;
    private TextView tvName;
    public int index;

    private CandidateHolder(View v) {
        super(v);
        this.tvName = (TextView) v.findViewById(R.id.tv_name);
        this.ivThumb = (ImageView) v.findViewById(R.id.ivNaver);
    }
}

StaggeredGridLayoutManager 是这样制作的。

     setContentView(R.layout.activity_main);
    StaggeredGridLayoutManager mLayManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
    mRvVote = (RecyclerView) findViewById(R.id.rvVote);
    mRvVote.setLayoutManager(mLayManager);
4

0 回答 0