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.
参考