How to get child view from RecyclerView?

前端 未结 11 1871
离开以前
离开以前 2020-12-24 08:18

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

rvSellRecords.addOnItemTouchListener(new RecyclerItemClickListener(         


        
11条回答
  •  囚心锁ツ
    2020-12-24 09:04

    I wouldn't recommend tracking the view list yourself. It could lead to weird issues with item updates, position updates, etc.

    Instead on your SellRecordChangedEvent, use findViewHolderForAdapterPosition() instead of adapter.getView().

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

    http://developer.android.com/reference/android/support/v7/widget/RecyclerView.html#findViewHolderForAdapterPosition(int)

    And as a side note, it's better to implement an actual item click listener to the itemView on the ViewHolder instead of using touch listener. There's lots of examples of this online.

提交回复
热议问题