如何从RecyclerView获取子视图? [英] How to get child view from RecyclerView?

查看:647
本文介绍了如何从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);

问题:如何从RecyclerView中获取子视图?

编辑赏金:

我有RecyclerView个可以显示产品列表.当我单击它时,我将在其中显示产品信息的地方添加新的片段.打开时,我正在使用RecyclerView中的视图更新工具栏-这非常正常:

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);
    }

}

我的问题:当我更新工具栏中的视图时,我正在使用旧视图.当数量为3时,我得到的视图为2.当数量为10时,我得到的视图为9.

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)?

推荐答案

使用recyclerView.findViewHolderForLayoutPosition(position)reyclerView.findViewHolderForAdapterPosition(position)来获取视点的位置.然后,您可以从您的观察者访问任何孩子.

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

结帐 Recyclerview

这篇关于如何从RecyclerView获取子视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆