recyclerview获取当前显示的item位置

        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);

                if (recyclerView != null && recyclerView.getChildCount() > 0) {
                    try {
                        int currentPosition = ((RecyclerView.LayoutParams) recyclerView.getChildAt(0).getLayoutParams()).getViewAdapterPosition();
                        Log.e("=====currentPosition", "" + currentPosition);
                    } catch (Exception e) {
                    }
                } 
 
            }
        });

 

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
RecyclerView可以通过设置不同的LayoutManager来实现不同的布局效果,其中GridLayoutManager可以实现分栏显示Item的效果。 GridLayoutManager可以设置每行或每列显示Item个数,从而实现分栏显示的效果。具体实现步骤如下: 1. 在布局文件中添加RecyclerView: ``` <androidx.recyclerview.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="match_parent" /> ``` 2. 在代码中初始化RecyclerView和Adapter,并设置LayoutManager为GridLayoutManager: ``` // 初始化RecyclerView RecyclerView recyclerView = findViewById(R.id.recyclerView); // 初始化Adapter MyAdapter adapter = new MyAdapter(dataList); // 设置LayoutManager为GridLayoutManager int spanCount = 2; // 每行显示Item个数 GridLayoutManager layoutManager = new GridLayoutManager(this, spanCount); recyclerView.setLayoutManager(layoutManager); recyclerView.setAdapter(adapter); ``` 3. 如果需要设置每个Item的间隔,可以通过设置ItemDecoration来实现: ``` // 设置每个Item之间的间隔 int spacing = 10; // 间隔大小 recyclerView.addItemDecoration(new GridSpacingItemDecoration(spanCount, spacing, true)); ``` 其中,GridSpacingItemDecoration是自定义的一个ItemDecoration,用于设置Item之间的间隔。具体实现可以参考以下代码: ``` public class GridSpacingItemDecoration extends RecyclerView.ItemDecoration { private int spanCount; // 每行显示Item个数 private int spacing; // Item之间的间隔大小 private boolean includeEdge; // 是否包括边缘 public GridSpacingItemDecoration(int spanCount, int spacing, boolean includeEdge) { this.spanCount = spanCount; this.spacing = spacing; this.includeEdge = includeEdge; } @Override public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { int position = parent.getChildAdapterPosition(view); // 获取当前Item位置 int column = position % spanCount; // 获取当前Item所在列的位置 if (includeEdge) { outRect.left = spacing - column * spacing / spanCount; outRect.right = (column + 1) * spacing / spanCount; if (position < spanCount) { outRect.top = spacing; } outRect.bottom = spacing; } else { outRect.left = column * spacing / spanCount; outRect.right = spacing - (column + 1) * spacing / spanCount; if (position >= spanCount) { outRect.top = spacing; } } } } ``` 通过以上步骤,即可实现RecyclerView的分栏显示效果。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值