我用CardView制作了一个RecyclerView。而CardView包含TextView和ListView。 TextView可以很好地回收,但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>
我如何把它放在主要活动中:
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 :(得分:0)
而不是创建通知标题和通知日期的StringList。创建一个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;
}
}
现在创建此类的arraylist,并在每次添加数据之前创建该类的新对象并将数据设置为类,最后将该类添加到NoticeDetails类的arraylist中。并将arraylist传递给Adapter而不是字符串数组。