在RecyclerView中设置文本并有条件地显示TextView [英] Set text and show TextView conditionally in a RecyclerView

查看:537
本文介绍了在RecyclerView中设置文本并有条件地显示TextView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在RecyclerView中按字母顺序显示列表, 每个字母组应在每个组的顶部左侧一次显示一个字母. 分隔线在ItemDecorator中呈现.

我正在尝试在onBindViewHolder中解决此问题. 初始布局工作正常.当我从上到下滚动时,一切都会如预期. 但是当我向上滚动时,首字母大写字母丢失或重新排序.

向下滚动将按预期有条件地显示首字母:

在此示例中,向上滚动后,缺少k:

public void onBindViewHolder(WordItemViewHolder wordItemViewHolder, final int position) {

        final WordModel wordModel = wordModels.get(position);
        wordItemViewHolder.textView.setText(wordModel.getWord());

        String word = wordModel.getWord();
        String currentFirstLetter = word.substring(0,1);


        if(maxListRendered <= position){
            if(!previousLetter.contentEquals(currentFirstLetter) || position == 0){
                wordItemViewHolder.initialView.setText(currentFirstLetter.toUpperCase());
                wordItemViewHolder.initialView.setVisibility(View.VISIBLE);
                previousLetter = currentFirstLetter;
            }else{
                wordItemViewHolder.initialView.setVisibility(View.INVISIBLE);
            }
        }
      maxListRendered++;//initialised as 0 in attempt to track calls to onBindViewHolder
    }

任何帮助表示感谢,谢谢.

解决方案

从一开始就删除maxListRendered和previousLetter-这很危险,并且会引起问题.

请检查当前索引上方的索引.另外,除了显示和隐藏元素之外,我还建议通过覆盖getItemViewType来具有不同的视图类型-这样可以分别回收它们,以便它们可以具有不同的视图.

I'm displaying a list alphabetically in a RecyclerView, each letter group should have a letter displayed to the left side, once, at the top of each group. The divider line is rendered in an ItemDecorator.

I'm trying to solve this in onBindViewHolder. The initial layout works fine. As I scroll from the top to bottom everthing is as expected. But when I scroll back up the the initial/capital letter goes missing or it gets reordered.

Scrolling down is showing the initial letter conditionally as expected:

After scrolling back up k is missing in this example, :

public void onBindViewHolder(WordItemViewHolder wordItemViewHolder, final int position) {

        final WordModel wordModel = wordModels.get(position);
        wordItemViewHolder.textView.setText(wordModel.getWord());

        String word = wordModel.getWord();
        String currentFirstLetter = word.substring(0,1);


        if(maxListRendered <= position){
            if(!previousLetter.contentEquals(currentFirstLetter) || position == 0){
                wordItemViewHolder.initialView.setText(currentFirstLetter.toUpperCase());
                wordItemViewHolder.initialView.setVisibility(View.VISIBLE);
                previousLetter = currentFirstLetter;
            }else{
                wordItemViewHolder.initialView.setVisibility(View.INVISIBLE);
            }
        }
      maxListRendered++;//initialised as 0 in attempt to track calls to onBindViewHolder
    }

Any help appreciated, thank you.

解决方案

Get rid of maxListRendered and previousLetter to begin with - that's dangerous to do and will cause issues.

Instead check the index above the current. Also instead of showing and hiding elements, I'd recommend having different view types by overriding getItemViewType - that makes them being recycled separately so they can have different views.

这篇关于在RecyclerView中设置文本并有条件地显示TextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆