在滾動RecyclerView
時,即使logtime.get(i).getTime()
數組中的數據沒有變化,TextView
中的數據也會發生變化。僅當我們將數據放入動態生成的textview中時纔會更改。以編程方式在recyclerview內添加textview onbindView適配器
public void onBindViewHolder(PunchCardViewHolder holder, int position) {
PunchCardReport punchCardReport = punchCardReports.get(position);
holder.sNumber.setText(punchCardReport.getmSNumber());
holder.logDate.setText(punchCardReport.getmLogDate());
StringTokenizer stringTokenizer = new StringTokenizer(punchCardReport.getmLogTime(), ",");
List<LogTime> logtime = punchCardReport.getmLogTimeList();
for (int i = 0; i < logtime.size(); i++) {
Log.d(TAG, "onBindViewHolder: "+logtime.get(i).getTime());
}
LinearLayout.LayoutParams dim = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
for (int i = 0; i < logtime.size(); i++) {
TextView textView=new TextView(mContext);
textView.setLayoutParams(dim);
textView.setText(logtime.get(i).getTime());//the problem is here when we put data inside textview
holder.logTime.addView(textView);
}
}
@Override
public int getItemCount() {
return punchCardReports.size();
}
謝謝..這個工程... –
此解決方案可能會導致內存問題。您應該爲文本視圖和其他視圖創建XML。創建視圖不建議在OnBindViewHolder()內部。如果你想,我給你演示這個源代碼。 –