[英]Confusion with adapter holder, how can I prevent getView method from modifying inappropriate ListView rows?
I haven't got an idea of custom adapters used in ListViews. 我还没有在ListViews中使用自定义适配器的想法。 Especially I mean holder class, which is appropriate for holding specified row data. 特别是我的意思是持有人类别,适用于保存指定的行数据。 I have an ArrayList which has NewsItem objects. 我有一个具有NewsItem对象的ArrayList 。 NewsItem object has some string fields, such as title, timestamp, etc. This class has boolean called page_separator_flag , and this question is around this particular variable. NewsItem对象具有一些字符串字段,例如title,timestamp等。此类具有称为page_separator_flag的布尔值,并且此问题与该特定变量有关。 Depending on this value row item in ListView changes its background and hides some information. 根据此值,ListView中的行项目会更改其背景并隐藏一些信息。
I have read that when I use holder for my adapter I will prevent unwanted changes of rows. 我已经读到,当我为适配器使用支架时,可以防止不必要的行更改。 So I created the holder called NewsItemHolder which has some fields to keep Views from particular row, and it has similar " page_separator_flag " too. 因此,我创建了一个名为NewsItemHolder的持有人,它具有一些字段来阻止特定行的View,并且它也具有类似的“ page_separator_flag ”。 According to my research, I thought that I can control row apperance by if statement. 根据我的研究,我认为我可以通过if语句控制行外观。
Unfortunately some rows which I didn't want to change has been changed (especially when I scroll my ListView object fast). 不幸的是,我不想更改的某些行已更改(尤其是当我快速滚动ListView对象时)。
My getView method: 我的getView方法:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
NewsItemHolder holder = new NewsItemHolder();
if(rowView == null) {
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
rowView = inflater.inflate(layoutResourceId, parent, false);
holder.newsPicture = (ImageView)rowView.findViewById(R.id.newsPicture);
holder.newsTimestamp = (TextView)rowView.findViewById(R.id.newsTimestamp);
holder.newsTitle = (TextView)rowView.findViewById(R.id.newsTitle);
holder.newsItemLayout = (LinearLayout)rowView.findViewById(R.id.newsLayout);
// Setting font
holder.newsTimestamp.setTypeface(typeface);
holder.newsTitle.setTypeface(typeface);
rowView.setTag(holder);
}
else {
holder = (NewsItemHolder) rowView.getTag();
}
String tests = (String)holder.newsTitle.getText();
NewsItem single_item = items.get(position);
holder.newsTimestamp.setText(single_item.getTimestamp());
holder.newsTitle.setText(single_item.getTitle());
holder.page_separator_flag = single_item.page_separator_flag;
// For page headers there is a special treatment
// if (holder.page_separator_flag == true) {
if(holder.newsTitle.getText().toString().contains("Page") && holder.page_separator_flag == true) {
holder.newsPicture.setVisibility(View.GONE);
holder.newsTimestamp.setVisibility(View.GONE);
holder.newsItemLayout.setBackground(context.getResources().getDrawable(R.drawable.background_card_page));
holder.newsTitle.setTextSize(Float.valueOf(holder.newsTitle.getTextSize()) * 0.50f);
holder.newsTitle.setTextColor(Color.WHITE);
}
// Load picture using Picasso (we haven't to focus on caching and preventing re-download)
Picasso.with(context).load(single_item.getImgURL()).placeholder(R.mipmap.ic_empty_news).into(holder.newsPicture);
return rowView;
}
My NewsItemHolder class: 我的NewsItemHolder类:
static class NewsItemHolder {
boolean page_separator_flag;
ImageView newsPicture;
TextView newsTimestamp;
TextView newsTitle;
LinearLayout newsItemLayout;
}
As you can see in the if statement mentioned above, row appearance will change when title View will contain "Page" word (it's about separating one set of data from another). 正如您在上述if语句中看到的那样,当标题视图包含“页面”字时,行的外观将发生变化(这是将一组数据与另一组数据分开)。 Unfortunately as I said, it affects on rows which are not page separators and contain needed data. 不幸的是,正如我所说,它会影响不是页面分隔符并包含所需数据的行。
How can I handle this properly when holder fails? 持有人失败后,我该如何正确处理? What am I doing wrong? 我究竟做错了什么?
you have to handle else case too otherwise it wont work 您也必须处理其他情况,否则它将无法正常工作
if(holder.newsTitle.getText().toString().contains("Page ") && holder.page_separator_flag == true) {
holder.newsPicture.setVisibility(View.GONE);
holder.newsTimestamp.setVisibility(View.GONE);
holder.newsItemLayout.setBackground(context.getResources().getDrawable(R.drawable.background_card_page));
holder.newsTitle.setTextSize(Float.valueOf(holder.newsTitle.getTextSize()) * 0.50f);
holder.newsTitle.setTextColor(Color.WHITE);
}
else {
YOUR DEFAULT VALUES WHEN BOOLEAN IS FALSE
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.