RecyclerView - 滚动时 CardView 相互重叠(如何将多行添加到单个 CardView?)

RecyclerView - CardViews overlap each other when scrolling (How to add multiple rows to a single CardView?)

整个布局是这样设置的——一个包含多个card view的RecyclerView,每个cardview可以有多行精确类型的LinearLayouts(但数据不同)。

一切正常,除了当我向下或向上滚动时,卡片开始随机重叠在其他卡片上。

代码如下:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> implements View.OnClickListener{

    Context context;

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

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

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        try {

            LinearLayout.LayoutParams contentParams = new LinearLayout.LayoutParams(100, 100);

            LayoutInflater inflater = LayoutInflater.from(context);

            LinearLayout ll_parent = (LinearLayout) inflater.inflate(R.layout.parent_linearlayout, null, false);
            NetworkImageView networkImageView = new NetworkImageView(context);
            String URL = "https://www.google.co.in/images/branding/product/ico/googleg_lodp.png";
            networkImageView.setImageUrl(URL, VolleySingleton.getInstance(context).getImageLoader());
            networkImageView.setLayoutParams(contentParams);
            networkImageView.setPadding(20, 20, 20, 20);

            TextView tv_card_title = new TextView(context);
            tv_card_title.setPadding(20, 20, 20, 20);

            LinearLayout ll_title = new LinearLayout(context);

            ll_title.setOrientation(LinearLayout.HORIZONTAL);

            ll_title.addView(networkImageView);
            ll_title.addView(tv_card_title);
            ll_title.setPadding(20, 20, 20, 20);

            ll_parent.addView(ll_title);

            for (int j = 0; j < getItemCount(); j++)
            {
                LinearLayout ll_children = (LinearLayout) inflater.inflate(R.layout.child_views, null, false);

                Button bt_delete = (Button) ll_children.findViewById(R.id.bt_delete);
                bt_delete.setTag("card no: " + (position+1) + " Iteration: "  + (j+1));
                bt_delete.setOnClickListener(this);
                Button bt_edit = (Button) ll_children.findViewById(R.id.bt_edit);
                bt_edit.setTag("card no: " + (position + 1) + " Iteration: " + (j + 1));
                bt_edit.setOnClickListener(this);

                TextView tv_abc = (TextView)ll_children.findViewById(R.id.tv_abc);
                tv_abc.setText("ABC");
                tv_abc.setTypeface(null, Typeface.BOLD);

                tv_card_title.setText("ABC");
                tv_card_title.setTypeface(null, Typeface.BOLD);

                NetworkImageView iv_Image = (NetworkImageView)ll_children.findViewById(R.id.iv_Image);

                iv_profileImage.setImageUrl(URL, VolleySingleton.getInstance(context).getImageLoader());
                ll_parent.addView(ll_children);
            }
            holder.cardView.addView(ll_parent);

        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View itemView = LayoutInflater.
                from(viewGroup.getContext()).
                inflate(R.layout.grandparent_cardview, viewGroup, false);

        return new ViewHolder(itemView);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.bt_delete:

                break;

            case R.id.bt_edit:

                break;
        }
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        CardView cardView;

        public ViewHolder(View v)
        {
            super(v);
            cardView = (CardView) v.findViewById(R.id.cv);
        }
    }
}

上面的代码有什么问题?有没有更好的方法将多行(相同类型)添加到单个 CardView?

您当然可能想要检查的一件事是您的布局参数。你有一个 3 层视图层次结构,如果你的布局参数没有在适当的地方正确设置为 "match_parent" 和 "wrap_content",事情就会变得混乱。我建议在 XML 中重写您的布局并尝试使用 layout_width 和 layout_height 参数。

在适配器中使用 getType 方法(您可以在您的单元模型中设置类型) 或者使用这个库:https://github.com/yqritc/RecyclerView-MultipleViewTypesAdapter

我只是将背景颜色添加到线性布局(布局在每张卡片中作为单独的行重复)。虽然这解决了问题,但我仍然不知道是什么导致视图在滚动时重叠。