2015-10-10 17 views
0

整個佈局設置如下 - 一個RecyclerView包含多個卡片視圖,每個CardView可以有多行精確的LinearLayouts鍵入(但具有不同的數據)。RecyclerView - CardViews在滾動時互相重疊(如何將多行添加到單個CardView?)

一切工作正常,除了當我向下或向上滾動時,卡片開始隨機與其他卡片重疊。

下面的代碼:

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?

回答

0

我只是加入了背景顏色的線性佈局(佈局,每個卡作爲單獨的行內重複)。雖然這解決了這個問題,但我仍然不知道是什麼導致視圖在滾動時重疊。

0

你肯定可能要檢查的一件事是你的佈局參數。如果您的佈局參數沒有在適當的位置正確設置爲「match_parent」和「wrap_content」,則您有3層視圖層次結構,並且可能會混亂起來。我會建議用XML重寫你的佈局,並試試你的layout_width和layout_height參數。

相關問題