我正在尝试为我的列表视图创建 3 个不同的主题。我已经验证每个主题实际上都会显示正确的背景布局并绑定到数据对象。我面临的问题是,在首选项中切换主题并返回主 ViewActivity 后,通常所有视图仍将具有旧的主题布局。直到我上下滚动或更改手机方向。
调用顺序是 onResume() -> updateUI() -> 清除列表视图适配器,添加要绑定的新数据,调用 dataSetChanged() -> 适配器中的自定义 getView() 查看主题并相应地选择正确的视图。
下面是几段代码(我在 Google IO 视频中看到了类型的东西)......
getView() 的主题之一
// bind to the view
if(convertView == null)
convertView = vi.inflate(R.layout.viewrow, parent, false);
iv = (ImageView) convertView.findViewById(R.id.viewrow_image);
dt = (TextView) convertView.findViewById(R.id.viewrow_dayview);
tt = (TextView) convertView.findViewById(R.id.viewrow_toptext);
bt = (TextView) convertView.findViewById(R.id.viewrow_bottomtext);
sImage = (ImageView) convertView.findViewById(R.id.viewrow_status);
// adjust for batch mode
if (model.isChecked()) convertView.setBackgroundColor(Color.LTGRAY);
else convertView.setBackgroundResource(0);
// adjust for completion
if (model.isComplete()) {
tt.setPaintFlags(tt.getPaintFlags()
| Paint.STRIKE_THRU_TEXT_FLAG);
sImage.setImageResource(R.drawable.ctask);
} else {
tt.setPaintFlags(tt.getPaintFlags()
& ~Paint.STRIKE_THRU_TEXT_FLAG);
sImage.setImageResource(R.drawable.ictask);
}
// adjust for priority
String priority = model.getPriority();
if (priority.equals("IMMEDIATE")) iv.setImageResource(R.drawable.immediatepriorityicon);
else if (priority.equals("HIGH")) iv.setImageResource(R.drawable.highpriorityicon);
else if (priority.equals("MEDIUM")) iv.setImageResource(R.drawable.mediumpriorityicon);
else iv.setImageResource(R.drawable.lowpriorityicon);
// update priority preference
if (!app.api.getCheckPriorityIcon()) iv.setVisibility(View.GONE);
else iv.setVisibility(View.VISIBLE);
// color day text
long days = model.getDueIn();
if (days > 0) {
dt.setTextColor(Color.GREEN);
} else if (days == 0) {
dt.setTextColor(Color.WHITE);
} else {
dt.setTextColor(Color.RED);
}
// adjust for completion preference
// 0 strike
// 1 text
// 2 invisible
int cMode = app.api.getCompletionModeEvent();
switch (cMode) {
case 0:
sImage.setVisibility(View.GONE);
break;
case 1:
tt.setPaintFlags(tt.getPaintFlags() & ~Paint.STRIKE_THRU_TEXT_FLAG);
sImage.setVisibility(View.VISIBLE);
break;
case 2:
tt.setPaintFlags(tt.getPaintFlags() & ~Paint.STRIKE_THRU_TEXT_FLAG);
sImage.setVisibility(View.GONE);
break;
}
break;
updateUIList() 会:
protected void updateUIList()
{
eventAdapter.clear();
if(selectedDate == null)
{
if(app.api.getShowCompleteEvents())
allEvents = app.edo.getAllEvents();
else
allEvents = app.edo.getIncompleteEvents();
if(eventFilter != null){
allEvents = eventFilter.filterEvents(allEvents);
}
allEvents = preferenceSort(allEvents);
int count = 0;
if(allEvents != null)
count = allEvents.size();
setTitle(app.APP_TITLE + ": All Entries (" + count + ")");
}
else
{
allEvents = preferenceSort(app.edo.searchByDate(selectedDate, !app.api.getShowCompleteEvents()));
int count = 0;
if(allEvents != null)
count = allEvents.size();
setTitle(app.APP_TITLE + ": Entries on " + selectedDate + " (" + count + ")");
}
// if we have no events, hide our list view and display the static "No Tasks" text, otherwise show it.
if(allEvents == null)
{
emptyTextView.setVisibility(View.VISIBLE);
eventContainer.setVisibility(View.GONE);
return;
}
if(emptyTextView.getVisibility() == View.VISIBLE)
{
emptyTextView.setVisibility(View.GONE);
}
if(eventContainer.getVisibility() == View.GONE)
{
eventContainer.setVisibility(View.VISIBLE);
}
for(Event anEvent : allEvents)
{
eventAdapter.add(anEvent);
}
eventAdapter.notifyDataSetChanged();
}
`
和主题类型:
@Override
public int getViewTypeCount() {
// TODO should correspond to themes array
return 3;
}
@Override
public int getItemViewType(int position) {
if(position == 0){
theme = app.api.getThemeView();
}
return theme;
}
谢谢。