简体   繁体   English

按textview的长度排序回收站视图

[英]Sort recycler view by length of textview

I am looking for solution for below problem. 我正在寻找以下问题的解决方案。

I have recycler view which contains couple of textviews, I need to sort it by the length of one of the textviews. 我有包含几个textviews的回收站视图,我需要按textviews之一的长度对其进行排序。 The shortest length should be first. 最短的长度应该是第一。 I was trying to find some similar problems but I could not find any. 我试图找到一些类似的问题,但找不到任何问题。

Does anyone have some optimal way to do it? 有没有人有一些最佳的方法来做到这一点?

You will sort your List by using Comparator . 您将使用ComparatorList进行排序。 Below is the way. 下面是方法。

adapter.setList(Collections.sort(adapter.getList(), new LengthComparator()););
adapter.notifyDataSetChanged();

LengthComparator.java LengthComparator.java

public class LengthComparator implements Comparator<String> {
    @Override
    public int compare(String s1, String s2) {
        return s1.length() - s2.length();
    }
}

for anyone who is looking for answer in Kotlin your List should be used in bind of Your ViewHolder(Also in Java) 对于在Kotlin中寻找答案的任何人,您的列表都应与ViewHolder绑定使用(也在Java中)

 inner class ViewHolder(var binding: ViewDataBinding) : RecyclerView.ViewHolder(binding.root)
    {

        fun bind(step : Steps, handler: ItemClickHandler)
        {
            list?.sortedBy { steps -> itemView.title.length() }
            binding.setVariable(BR.steps,step)
            binding.executePendingBindings()

        }
    }

its Worked For me 它为我工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM