2016-07-31 53 views
0

我用CardView製作了一個RecyclerView。 CardView包含一個TextView和ListView。 TextView很好地回收,但ListView的項目與我放在最後的項目完全相同。我怎樣才能解決這個問題?RecyclerView中的ListView不回收

  • 我使用自定義列表視圖來修復問題CardView不會傳播整個listView項目。

這是我的代碼。

recyclerview adapter: 

public class NoticeAdapter extends RecyclerView.Adapter<NoticeAdapter.NoticeViewHolder>{ 
private ArrayList<String> noticeTypes; 
private ArrayList<ArrayList<String>> noticeTitles; 
private ArrayList<ArrayList<String>> noticeDates; 

public NoticeAdapter() { 
    noticeTypes = new ArrayList<>(); 
    noticeTitles = new ArrayList<ArrayList<String>>(); 
    noticeDates = new ArrayList<ArrayList<String>>(); 
} 

public void addNoticeSet(String noticeType, ArrayList<String> noticeTitle, ArrayList<String> noticeDate) { 
    noticeTypes.add(noticeType); 
    noticeTitles.add(noticeTitle); 
    noticeDates.add(noticeDate); 
} 

@Override 
public NoticeViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { 
    View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.notice_row, viewGroup, false); 
    return new NoticeViewHolder(itemView); 
} 

@Override 
public void onBindViewHolder(NoticeViewHolder noticeViewHolder, int i) { 
    NoticeListAdapter noticeListAdapter = new NoticeListAdapter(); 
    noticeViewHolder.lv_noticeList.setAdapter(noticeListAdapter); 

    String noticeType = noticeTypes.get(i); 
    noticeViewHolder.tv_noticeType.setText(noticeType); 
    noticeViewHolder.tv_noticeType.setPaintFlags(noticeViewHolder.tv_noticeType.getPaintFlags() | Paint.FAKE_BOLD_TEXT_FLAG); 

    for(int m=0; m<5; m++) 
     noticeListAdapter.addNoticeSet(noticeTitles.get(i).get(m),noticeDates.get(i).get(m)); 
} 

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

public static class NoticeViewHolder extends RecyclerView.ViewHolder { 

    protected TextView tv_noticeType; 
    protected NoticeListView lv_noticeList; 

    public NoticeViewHolder(View itemView) { 
     super(itemView); 
     tv_noticeType = (TextView)itemView.findViewById(R.id.tv_noticeType); 
     lv_noticeList = (NoticeListView)itemView.findViewById(R.id.lv_noticeList); 
    } 
    } 
} 

自定義列表視圖適配器:

public class NoticeListAdapter extends BaseAdapter { 
    private ArrayList<String> noticeTitles; 
    private ArrayList<String> noticeDates; 

    public NoticeListAdapter() { 
     noticeTitles = new ArrayList<>(); 
     noticeDates = new ArrayList<>(); 
    } 

    public void addNoticeSet(String noticeTitle, String noticeDate) { 
     noticeTitles.add(noticeTitle); 
     noticeDates.add(noticeDate); 
    } 

    @Override 
    public int getCount() { 
     return noticeTitles.size(); 
    } 

    @Override 
    public HashMap<String, String> getItem(int position) { 
     HashMap<String, String> noticeSet = new HashMap<>(); 
     noticeSet.put(noticeTitles.get(position), noticeDates.get(position)); 
     return noticeSet; 
    } 

    @Override 
    public long getItemId(int position) { 
     return position; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     final int pos = position; 
     final Context context = parent.getContext(); 

     View v = convertView; 

     if(v == null) { 
      LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      v = inflater.inflate(R.layout.noticelist_row, parent, false); 
      TextView noticeTitle = (TextView)v.findViewById(R.id.tv_noticeTitle); 
      TextView noticeDate = (TextView)v.findViewById(R.id.tv_noticeDate); 

      noticeTitle.setText(noticeTitles.get(position)); 
      noticeDate.setText(noticeDates.get(position)); 
     } 
     return v; 
    } 
} 

自定義列表視圖:

public class NoticeListView extends ListView { 
    public NoticeListView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public NoticeListView(Context context, AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
    } 

    public NoticeListView(Context context) { 
     super(context); 
    } 

    @Override 
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, 
       MeasureSpec.AT_MOST); 
     super.onMeasure(widthMeasureSpec, expandSpec); 
    } 
} 

cardview XML:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:id="@+id/cv_notice" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_margin="5dip" 
     android:layout_gravity="center" 
     card_view:cardCornerRadius="4dip"> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical" 
      android:padding="10dp"> 

      <TextView 
       android:id="@+id/tv_noticeType" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:padding="10dip" /> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"> 

       <com.rb16.mainpage.NoticeListView 
        android:id="@+id/lv_noticeList" 
        android:layout_width="match_parent" 
        android:layout_height="fill_parent" 
        android:footerDividersEnabled="false" 
        android:overScrollFooter="@android:color/transparent"/> 

      </LinearLayout> 
     </LinearLayout> 

    </android.support.v7.widget.CardView> 

</LinearLayout> 

我怎麼把itmes在主要活動:

private void setNoticeCard() { 

    //noticeTitle = new ArrayList<String>(); 
    //noticeDate = new ArrayList<String>(); 
    //noticeAdapter = NoticeAdapter(); 

    noticeTitle.add("1"); 
    noticeTitle.add("1"); 
    noticeTitle.add("1"); 
    noticeTitle.add("1"); 
    noticeTitle.add("1"); 

    noticeDate.add("2015-05-31"); 
    noticeDate.add("2015-05-32"); 
    noticeDate.add("2015-05-33"); 
    noticeDate.add("2015-05-34"); 
    noticeDate.add("2015-05-35"); 

    noticeAdapter.addNoticeSet("Notice1", noticeTitle, noticeDate); 
    noticeTitle.clear(); 
    noticeDate.clear(); 

    noticeTitle.add("2"); 
    noticeTitle.add("2"); 
    noticeTitle.add("2"); 
    noticeTitle.add("2"); 
    noticeTitle.add("2"); 

    noticeDate.add("2016-07-08"); 
    noticeDate.add("2015-05-30"); 
    noticeDate.add("2015-05-30"); 
    noticeDate.add("2015-05-30"); 
    noticeDate.add("2015-05-30"); 

    noticeAdapter.addNoticeSet("Notice2", noticeTitle, noticeDate); 
    noticeTitle.clear(); 
    noticeDate.clear(); 

    noticeTitle.add("3"); 
    noticeTitle.add("3"); 
    noticeTitle.add("3"); 
    noticeTitle.add("3"); 
    noticeTitle.add("3"); 

    noticeDate.add("2015-05-30"); 
    noticeDate.add("2015-05-30"); 
    noticeDate.add("2015-05-30"); 
    noticeDate.add("2015-05-30"); 
    noticeDate.add("2015-05-30"); 

    noticeAdapter.addNoticeSet("Notice3", noticeTitle, noticeDate); 
} 

回答

0

而不是創建通知標題和通知日期的字符串ArrayList。創建一個NoticeDetails類。

public class NoticeDetail { 

String notice_name; 
String notice_date; 
String notice_title; 

public String getNotice_name() { 
    return notice_name; 
} 

public void setNotice_name(String notice_name) { 
    this.notice_name = notice_name; 
} 

public String getNotice_date() { 
    return notice_date; 
} 

public void setNotice_date(String notice_date) { 
    this.notice_date = notice_date; 
} 

public String getNotice_title() { 
    return notice_title; 
} 

public void setNotice_title(String notice_title) { 
    this.notice_title = notice_title; 
} 

}

加入datat創建類的新對象和數據集類,並在年底前,現在創建這個類,每次的ArrayList中添加的類別爲NoticeDetails類的數組列表。然後將ArrayList傳遞給Adapter而不是字符串數組。

+0

非常感謝!它效果不錯 –

+0

其我的榮幸.. –