简体   繁体   English

Recyclerview:隐藏项目而不将它们从 ArrayList 中删除

[英]Recyclerview: Hiding Items without removing them from the ArrayList

I've been searching for that for quite a while and haven't found a proper answer:我已经搜索了很长一段时间,但没有找到正确的答案:

Is there a way to remove an Item from a RecyclerView without removing it from the underlying ArrayList?有没有办法从 RecyclerView 中删除 Item 而不从底层 ArrayList 中删除它? I want to add a functionality, to temporarily hide Items from the List, when a certain condition is true.我想添加一个功能,当某个条件为真时,暂时隐藏列表中的项目。 I still need those items back later, but managing 2 separate ArrayList seems overly complicated.稍后我仍然需要这些项目,但管理 2 个单独的 ArrayList 似乎过于复杂。 If I set the Visibility of the Item inside the Adapter to gone , I have a visible gap where the item was, so that is not a solution.如果我将 Adapter 内 Item 的 Visibility 设置为gone ,则该项目所在的位置有一个可见的间隙,因此这不是解决方案。

Is there any way by which I can avoid managing 2 separate ArrayLists?有什么方法可以避免管理 2 个单独的 ArrayLists?

I think you'll have to actually remove from the data model.我认为您必须实际从数据模型中删除。 Otherwise, you'll either end up with blank rows, as you say, or when you scroll, the "hidden" recycled elements will get jumbled up.否则,正如您所说,您最终会得到空白行,或者当您滚动时,“隐藏的”回收元素将变得混乱。

Just because you remove from the the Arraylist doesn't mean you can't add another list to (temporarily) store the items that were removed.仅仅因为您从 Arraylist 中删除并不意味着您不能添加另一个列表来(临时)存储已删除的项目。

For example, define this in the adapter例如,在适配器中定义它

private List<Item> removed = new ArrayList<>();

public void remove(int position) {
    removed.add(items.get(position));
    items.remove(position);
    notifyDataSetChanged();
}

If you need better functionality about which items you have removed, a Hashmap may be better.如果您需要关于已删除项目的更好功能,Hashmap 可能会更好。

you have an ArrayList of object type You have a model/pojo class of Object type你有一个对象类型的 ArrayList 你有一个对象类型的模型/pojo 类

while iterating JsonData from JsonResponse just pass boolean value true/false in object type Model class.从 JsonResponse 迭代 JsonData 时,只需在对象类型 Model 类中传递布尔值 true/false。 The data you want to show make it true else false, later you can set the 'false value to true' to your Model class.您要显示的数据使其为真,否则为假,稍后您可以将“假值为真”设置为您的模型类。

Hope this will work.希望这会奏效。

就我而言,我使用的是getApplicationContext()而不是getContextthis

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

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