简体   繁体   English

与适配器持有人混淆,如何防止getView方法修改不适当的ListView行?

[英]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.

相关问题 我可以从自定义适配器的getView填充一个listview吗? - Can I populate a listview from getView of a custom adapter? 点击列表项时,如何避免调用适配器类的getView()方法? - How can I avoid calling of getView() method of adapter class when I tap on listitems? 如何从GridView将数组传递到getView方法? - How I can passing an array into getView method from GridView? 如何将自定义getView绑定到ListView适配器? - How to bind custom getView to ListView adapter? 在重写用于列表视图的自定义适配器的getview方法之后,在列表视图中什么也看不到 - after override getview method for custom adapter for a listview,see nothing in listview 需要在DialogFragment内将适配器设置为ListView,但是使用getView()方法会得到空指针异常吗? 为什么? - Need to set an adapter to a ListView inside a DialogFragment but I get a null pointer exception with the getView() method? Why? Android getView 方法混淆 - Android getView method confusion Android:从viewPager中的Fragment调用ListView的适配器的getview() - Android: calling getview() for ListView's Adapter from Fragment in viewPager 如何从适配器向列表视图添加两个按钮? - How can i add two buttons to the listview from the adapter? 如何将布局/视图连接到getView()适配器方法中的特定对象? - How do I connect a Layout/view to a specific object in a getView() adapter method?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM