RecycleView使用

1. RecycleView滚动条展示隐藏
Recycleview或ScrollView 滚动条控件的滑块颜色,其实通过布局属性android:scrollbarThumbVertical 可以关联一个drawable对象


// 设置滚动条样式
android:scrollbarThumbVertical="@drawable/scrollbar_handle_holo_light"

// 设置滚动条是否可见
android:scrollbars="none"

2. 获取RecyclerView的某个Item的坐标


final ViewTreeObserver viewTreeObserver = getActivity().getWindow().getDecorView().getViewTreeObserver();

// 写这句代码是因为很多同学都喜欢在RecycleView以及内部View未加载完毕后调用获取宽高之类的方法
// 原因是View都还没加载成功,一定是获取不到预期宽高之类的数据的
// 当然你确保RecycleView已经加载完毕,可以直接使用onGlobalLayout里面代码
viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
		
		RecyclerView.ViewHolder holder 
		= mRecyclerView.findViewHolderForAdapterPosition(0);
		// 假设第一个Item是CategoryViewHolder类型
    	if (holder != null && holder instanceof CategoryViewHolder) {
       	 	CategoryViewHolder viewHolder = (CategoryViewHolder) holder;
       	 	int[] location = new int[2];
        	viewHolder.image.getLocationOnScreen(location);
        	// 获取到Item中image控件在屏幕上的坐标位置
        }
    
		......
		// 切记记得remove
		viewTreeObserver.removeOnGlobalLayoutListener(this);
        }
    });
   

3. RecycleView在某些场景自动滚动问题

RecycleView 由于会自动获取焦点,其父ViewGroup可以通过如下设置来解决

// 通过设置RecycleView父View focusable 和focusableInTouchMode 为true
<......Layout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:focusable="true"
        android:focusableInTouchMode="true">

        <......RecyclerView
            android:id="@+id/recycle_vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fadingEdge="none"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:scrollbarStyle="insideOverlay"
            android:scrollbars="none" />

</......Layout>

4. RecycleView刷新单个Item
参考:

5. 获取RecyclerView的某个Item的坐标另一种实现

if(mAdapter.getItemCount() > 0) {
    RecyclerView.ViewHolder holder = mRecyclerView.findViewHolderForAdapterPosition(0);
    if (holder != null && holder instanceof CategoryViewHolder) {
        CategoryViewHolder viewHolder = (CategoryViewHolder) holder;
        int[] location = new int[2];
        // 获取RecycleView中某个Item在屏幕上坐标(走上角)
        viewHolder.image.getLocationOnScreen(location);
    }
}

6. RecyclerView快速滑动到顶部

// RecyclerView提供了以下几个api控制RecyclerView滑动

// 按一定速度滚动到指定的位置
SmoothScrollToPosition(int position) 
// 按一定速度滚动指定的距离
smoothScrollBy(int dx, int dy)   
//  跳转到指定的位置
scrollToPosition(int position)  
// 跳转指定的距离
scrollBy(int dx,int dy)    

7. 参考

  1. NestedScrollView嵌套RecyclerView
  2. RecyclerView抢焦点自动滚动
  3. RecycleView一些扩展用法
  4. RecyclerView快速滑动到顶部(源码分析) 改变滑动速度等
  5. RecyclerView滑动到指定位置
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

初心一点

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值