Reputation: 1148
I'm using RecyclerView
to which I attach a LinearLayoutManager
and a RecyclerView.Adapter
RecyclerView conversationList = (RecyclerView)findViewById(R.id.conversationList);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
conversationList.setLayoutManager(layoutManager);
MessagesAdapter adapter = new MessagesAdapter();
conversationList.setAdapter(adapter);
In onBindViewHolder
of RecyclerView.Adapter
I'm passing the adapter item object to the view by using setTag:
@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
viewHolder.itemView.setTag(getItem(position));
...
}
I'm iterating over child views of the layoutManager
and get the needed object by using the getTag
method
for (int i = 0; i < layoutManager.getChildCount(); i++) {
LinearLayout container = (LinearLayout)layoutManager.getChildAt(i);
LinearLayout container2 = (LinearLayout)container.getChildAt(0);
Object tag = container2.getTag();
}
It doesn't seem right to me. Is there a better way of doing this?
Upvotes: 2
Views: 3487
Reputation: 11
when you scroll the view,the ViewHolder
may reused, so it`s was reset. Due to this you may get a wrong tag when you want get it.
Upvotes: 1
Reputation: 5096
You can add something like that to your Adapter class:
public ArrayList<Objects> getAllTags(){
ArrayList<Objects> arrayList = new ArrayList<>();
for (int i = 0; i < getItemCount() ; i++){
arrayList.add(getItem(i));
}
return arrayList;
}
And the retrieve the objects via:
adapter.getAllObjects();
I'm assume that :
getItem(position)
Is method you defines in adapter class.
Upvotes: 0