Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

banner點擊兩側 position錯誤 #13

Closed
PascalTang opened this issue May 4, 2020 · 2 comments
Closed

banner點擊兩側 position錯誤 #13

PascalTang opened this issue May 4, 2020 · 2 comments

Comments

@PascalTang
Copy link

PascalTang commented May 4, 2020

val adapter = OriginVideoAdapter(R.layout.item_origin_banner, arrayListOf())

    adapter.setOnItemClickListener { holder, view, position ->
        Timber.tag("ddd").i("click $position")
        Timber.tag("ddd").i("click2 "+mBanner?.currentPager)
    }

你好 我是用viewpager2版本
我的banner有留左右的影片 也就是一頁三式
但當我點擊非中間的影片時
get的current position仍是中間的
但setOnItemClickListener的position位置會是正確的(可是他不是data實際的position)
請問我想要監聽是否點擊 兩側的影片
要如何取到他的值?

@zguop
Copy link
Owner

zguop commented May 6, 2020

用BaseQuickAdapter, setOnItemClickListener(BaseQuickAdapter adapter, View view, int position) 参数position 是 通过 int position = baseViewHolder.getAdapterPosition(); 回调出来的实际轮播数量的position,这个角标没有经过转换= =,因此这里通过banner.getCurrentPager()获取当前显示的position。

现在针对你你的这种情况分析是,点击了两侧的item,
回调出来的参数position还是通过int position = baseViewHolder.getAdapterPosition()没有经过转换,角标不正确;banner.getCurrentPager()是当前页的,角标不正确;

解决方案,参考下哈:

public class ImageAdapter extends BaseQuickAdapter<Object, BaseViewHolder> {
public ImageAdapter() {
    super(R.layout.item_image);
}
  //看这里 回调出正确的position
@Override
public void onBindViewHolder(@NonNull final BaseViewHolder holder, final int position) {
    super.onBindViewHolder(holder, position);
    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            setOnItemClick(holder.itemView, position);
        }
    });
}

@Override
protected void convert(@NonNull BaseViewHolder helper, Object item) {
    Glide.with(mContext)
            .load(item)
            .into((ImageView) helper.getView(R.id.img));
}

}

@PascalTang
Copy link
Author

非常感謝

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants