Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get child view from RecyclerView?

I am trying to get child view by position. I could get view when one item is clicked:

rvSellRecords.addOnItemTouchListener(new RecyclerItemClickListener(getActivity(), new RecyclerItemClickListener.OnItemClickListener() {
        @Override
        public void onItemClick(View view, int position) {
            ((MainActivity) getActivity()).showSellRecordFragment(position, view);
        }
    }));

Now I cannot get child view, without click - let's say by position for example:

rvSellRecords.someMagicalMethodWhichReturnsViewByPosition(5);

Question: How to get child view from RecyclerView?

EDIT FOR BOUNTY:

enter image description here

I have RecyclerView to show products list. When I click on it, I am adding new Fragment where I show product information. While opening I am updating toolbar with view from RecyclerView - this is working perfectly:

    rvSellRecords.addOnItemTouchListener(new RecyclerItemClickListener(getContext(), new RecyclerItemClickListener.OnItemClickListener() {
                    @Override
                    public void onItemClick(View view, int position) {
                        sellPresenter.onSellRecordSelected(position, view);
                    }
                }));

    When I click blue button with "+", I am incrementing quantity by 1.





    public void onIncrementButtonClicked(){
                sellRecord.setCount(sellRecord.getCount() + 1);
                showQuantity();
                bus.post(new SellRecordChangedEvent(sellRecord, sellRecordPosition));
                }

Then I am posting updated sellRecord to first fragment using EventBus. There I am updating list data. I supposed that updating value(sell) automatically updates adapter. Now I am getting view from adapter using custom method(getView) which was created by me(you can find it below).



 @Subscribe
public void onEvent(SellRecordChangedEvent event){
    sell.getSellRecords().set(event.getSellRecordPosition(), event.getSellRecord());
    sell.recalculate();
    int position = event.getSellRecordPosition();
    View view = adapter.getView(position);
    bus.post(new TransactionTitleChangedEvent(null, view));
}

This is my adapter class - I changed adapter little bit to collect view in list and added method which returns view for respective position:



public class SellRecordsAdapter extends RecyclerView.Adapter<SellRecordsAdapter.ViewHolder> {

    .....
    .....
    .....
    List<View> viewList;

    public SellRecordsAdapter(List<SellRecord> sellRecordList) {

        .....

        viewList = new ArrayList<>();
    }

    .....
    .....
    .....

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int i) {

        .....
        .....
        .....

        viewList.add(i, viewHolder.itemView);

    }


    public View getView(int position){
        return viewList.get(position);
    }

}

My problem: when I updating view in toolbar, I am getting old view. When quantity is 3, I am getting view with 2. When quantity 10 - view is with 9.

My question: how to get view from recycler view using position of item(without on click listener)?

enter image description here

like image 706
Joe Richard Avatar asked Feb 16 '16 09:02

Joe Richard


People also ask

What is LayoutManager in RecyclerView?

This wear-specific implementation of LinearLayoutManager provides basic offsetting logic for updating child layout. A LayoutManager is responsible for measuring and positioning item views within a RecyclerView as well as determining the policy for when to recycle item views that are no longer visible to the user.

What is ItemDecoration RecyclerView?

An ItemDecoration allows the application to add a special drawing and layout offset to specific item views from the adapter's data set. This can be useful for drawing dividers between items, highlights, visual grouping boundaries and more.

What is setHasFixedSize in RecyclerView?

setHasFixedSize(true) means the RecyclerView has children (items) that has fixed width and height. This allows the RecyclerView to optimize better by figuring out the exact height and width of the entire list based on the your adapter.


6 Answers

Use recyclerView.findViewHolderForLayoutPosition(position) or reyclerView.findViewHolderForAdapterPosition(position) to get the viewholder for postion. Then you can access any child from your viewholder.

Checkout Recyclerview

like image 130
Ravi Theja Avatar answered Oct 02 '22 18:10

Ravi Theja


RecyclerView.ViewHolder holder = recycleView.findViewHolderForAdapterPosition(position);
ImageView imageView = holder.itemView.findViewById(R.id.iv_product);

This is a supplement to @Ravi Teja's answer. You can get the viewHolder from the recyclerView using position of the particular item, then get a particular view from the viewHolder as shown above

like image 43
mut tony Avatar answered Oct 02 '22 17:10

mut tony


You can use RecyclerView's LayoutManager for it.

View view = layoutManager.findViewByPosition(position)
like image 20
Geralt_Encore Avatar answered Oct 02 '22 18:10

Geralt_Encore


Hope this helps someone: I was getting null pointer exceptions with:

  • recyclerView.findViewHolderForAdapterPosition
  • recyclerView.findViewHolderForItemId
  • layoutManager.findViewByPosition.

The reason was that there is a slight delay for the viewholder to be created. I found the solution here: https://stackoverflow.com/a/33414430/7952427

like image 41
S. Lopes Avatar answered Oct 02 '22 16:10

S. Lopes


I post an answer because which is really complex to findviews() from RecyclerView.

@Joe: After spending 4hours found one answer. Which gives me the proper view of the index.

mAdapter is adapter of RecyclerView        
View v = recyclerView.findViewHolderForItemId(mAdapter.getItemId(index/position)).itemView; 

Now just access your views by: v.findViewById(R.id.edittext) OR any id.

like image 34
Ajinkya Avatar answered Oct 02 '22 17:10

Ajinkya


it helped me, make a 100 ms delay before manipulate it, like this:

Handler handler = new Handler();        
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
// rcv is my recyclerview
rcvStatus.getChildAt(1).setBackground(getActivity().getResources().getDrawable(R.drawable.disabled));
// or:
rcvStatus.getChildAt(1).setClickable(false);
}
}, 100);
like image 41
David Homayouni Avatar answered Oct 02 '22 18:10

David Homayouni