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

时间:2015-10-10 02:18:45

标签: android android-recyclerview android-cardview

整个布局设置如下 - 包含多个卡片视图的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 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

答案 2 :(得分:-1)

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