[CymChad/BaseRecyclerViewAdapterHelper]如何在适配器中获取当前点击item的position

2024-08-20 323 views
0

继承BaseQuickAdapter后,如何在适配器中获取当前点击item的position

回答

4

int position = helper.getLayoutPosition();

8

这个测试了,获取的position不正确。在convert方法里面调用int position = helper.getLayoutPosition();方法后,获取的position不是我点击项的position,每次都显示的是当前界面展示的最后item的position

9

aaa

6
@Override
protected void convert(BaseViewHolder helper, final CarItemBean item) {
    currentPosition=helper.getLayoutPosition();

    carCounterView=helper.getView(R.id.car_fragment_layout_item_carCounterView);
    final EditText countEdit= (EditText) carCounterView.findViewById(R.id.et_count);
    currentNumber=item.currentNumber;
    countEdit.setText(String.valueOf(currentNumber));
    countEdit.setSelection(countEdit.getText().toString().trim().length());
    ImageView addImageView= (ImageView) carCounterView.findViewById(R.id.iv_count_add);
    addImageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.e("convert>>","currentPosition:"+currentPosition);
            currentNumber++;
            countEdit.setText(String.valueOf(currentNumber));
            countEdit.setSelection(countEdit.getText().toString().trim().length());
            item.currentNumber=currentNumber;
        }
    });

}

日志如下: 12-13 10:00:29.586 4863-4863/com.ylfood E/convert>>: currentPosition:3 12-13 10:00:32.236 4863-4863/com.ylfood E/convert>>: currentPosition:3 12-13 10:00:33.166 4863-4863/com.ylfood E/convert>>: currentPosition:3 12-13 10:00:34.386 4863-4863/com.ylfood E/convert>>: currentPosition:3 12-13 10:00:35.046 4863-4863/com.ylfood E/convert>>: currentPosition:3 12-13 10:00:35.686 4863-4863/com.ylfood E/convert>>: currentPosition:3

9

我的item布局里面还嵌套有一个子布局,我点击子布局组件时,获取position

2

你声明了成员变量currentPosition,所以会在最后一个item赋值给currentPosition,造成你说的问题

4

好的,已解决,谢谢

3

@zhanhon 感谢你的回复