我想突出显示区域设置ListView中的单个视图,但是由于某些原因,即使我在“突出显示”之前使用了条件并且该条件返回false,其他视图也被突出显示。
如果我使用else语句重设字体,它将按预期工作:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//Check if existing view is being reused
View listItemView = convertView;
if(listItemView == null) {
listItemView = LayoutInflater.from(this.getContext()).inflate(R.layout.list_locale_item, parent, false);
}
Locale currentLocale = this.getItem(position);
TextView name = listItemView.findViewById(R.id.locale_name);
name.setText(currentLocale.getDisplayName());
if(isCurrent(currentLocale)) name.setTypeface(null, Typeface.BOLD);;
***else*** name.setTypeface(Typeface.DEFAULT);
return listItemView;
}
如果我排除else语句,将Typeface.BOLD属性应用于其他视图是否正常?我认为它与if(x == null)有关,但令我感到困惑的是,我在另一个项目中没有这个问题:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//Check if existing view is being reused
View listItemView = convertView;
if(listItemView == null) {
//Utilities.showText("Test " + this.getItem(position).name);
listItemView = LayoutInflater.from(this.getContext()).inflate(R.layout.list_book_item, parent, false);
}
Book currentBook = this.getItem(position);
TextView titleView = listItemView.findViewById(R.id.list_book_title);
ProgressBar progressBar = listItemView.findViewById(R.id.list_book_progress);
if(currentBook instanceof BookRaw) titleView.setText(Word.prettifyName(currentBook.name));
else titleView.setText(currentBook.name);
if(currentBook.isCurrentlyReading()) {
double progress = (double) currentBook.getProgress() * 100/ currentBook.getSize();
progressBar.setProgress((int) progress);
progressBar.setVisibility(View.VISIBLE);
}
***if***(currentBook.equals(MainActivity.mBookView.getBook())) titleView.setTypeface(null, Typeface.BOLD);
return listItemView;
}
即使突出显示的条件一次为真,并排突出显示的Here视图。令我感到困惑的是,即使代码是等效的,为什么它会在一个项目中发生而在另一个项目中却没有发生。 修改满足条件的视图而无需创建新视图的正确方法是什么?