简体   繁体   English

如何从RecyclerView ListAdapter按位置获取项目?

[英]How get item by position from RecyclerView ListAdapter?

I have a RecyclerView of custom objects which uses custom ListAdapter. 我有一个使用自定义ListAdapter的自定义对象的RecyclerView Is there a way to get the position of Item in Adapter from Activity or how can I access the containing list? 有没有一种方法可以从Activity中获取Item在适配器中的位置,或者如何访问包含列表?

Is there a way to get the position of Item in Adapter from Activity or how can I access the containing list? 有没有一种方法可以从Activity中获取Item在适配器中的位置,或者如何访问包含列表?

You need to declare a method inside your custom list adapter that exposes the underlying list you provide on the constructor. 您需要在自定义列表适配器中声明一个方法,该方法公开您在构造函数中提供的基础列表。

Something like this: 像这样:

List<MyItem> myList = new List<MyItem>()
MyCustomListAdapter mAdapter = new MyCustomListAdapter(myList);

In MyCustomListAdapter constructor assign your list to a field mInternalList and declare a method: 在MyCustomListAdapter构造函数中,将您的列表分配给字段mInternalList并声明一个方法:

public List<MyItem> getInternalList(){
    return mInternalList;
}

After this you can use all list utility methods to retrieve the index of an element. 之后,您可以使用所有列表实用程序方法来检索元素的索引。

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

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