Android RecycleView 的findChildViewUnder()方法,十分方便返回指定位置的childView
来源:互联网 发布:windows优化 编辑:程序博客网 时间:2024/06/04 19:12
观察到现在很多列表视图和网格视图需要返回指定位置下的view,例如小米手机的相册功能,滑动到不同的日期的图片,提示框出现的日期也随之变化。在listview和gridview中,估计需要自己写方法来获取,但强大的RecycleView提供了一个非常便利的findChildViewUnder(float x ,float y)来给开发者带来方便
查看了一下此方法的源代码,非常简单:
这个ChildHelper类,它会协助获取RecyclerView中的childVIew,并提供忽略隐藏Children的功能,也就是说,调它的getChildAt只会在当前显示的Children中去查找,如果想查HiddenChildren,需要调getUnfilteredChildAt。
public View findChildViewUnder(float x, float y) { final int count = mChildHelper.getChildCount(); for (int i = count - 1; i >= 0; i--) { final View child = mChildHelper.getChildAt(i); final float translationX = ViewCompat.getTranslationX(child); final float translationY = ViewCompat.getTranslationY(child); //判断该点是否在childView的范围内 if (x >= child.getLeft() + translationX && x <= child.getRight() + translationX && y >= child.getTop() + translationY && y <= child.getBottom() + translationY) { return child; } } return null; }
写一个很简单的测试程序来测试一下该方法:
其中布局文件中加一个自定义的DrawView来标定位置:
<?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v7.widget.RecyclerView android:id="@+id/recycleView" android:layout_width="match_parent" android:layout_height="match_parent"/> <com.example.yuanh.surfacetest.DrawView android:layout_width="match_parent" android:layout_height="match_parent" /></FrameLayout>
DrawView的onDraw代码:
public class DrawView extends View { public DrawView(Context context) { super(context); } @Override protected void onDraw(Canvas canvas) { //画一条标定的线条 Paint paint = new Paint(); paint.setColor(0x333333); paint.setAntiAlias(true); paint.setAlpha(255); paint.setStrokeWidth(10); canvas.drawLine(0,100,320,100,paint); }}
MainActivity的代码如下
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initDate(); RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycleView); recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)); recyclerView.setAdapter(new MyRecycleViewAdapter(this, contacts)); //利用滚动监听器来监听滚动时间,在onScrolled()方法中调用findChildViewUnder()方法 recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { //由于返回的是一个view,需要调用findViewById方法找到子类的view View v = recyclerView.findChildViewUnder(100,100); TextView textView = (TextView)v.findViewById(R.id.textName); String name = textView.getText().toString(); Log.v("scroll","child:"+name); } }); }
测试结果如下:
02-05 15:50:59.815 4486-4486/com.example.yuanh.surfacetest V/scroll: child:Danny
02-05 15:50:59.832 4486-4486/com.example.yuanh.surfacetest V/scroll: child:Danny
02-05 15:50:59.850 4486-4486/com.example.yuanh.surfacetest V/scroll: child:HTC Sammi
02-05 15:50:59.865 4486-4486/com.example.yuanh.surfacetest V/scroll: child:HTC Sammi
02-05 15:51:03.646 4486-4486/com.example.yuanh.surfacetest V/scroll: child:ne
02-05 15:51:03.679 4486-4486/com.example.yuanh.surfacetest V/scroll: child:ne
测试成功,非常好用的一个方法
- Android RecycleView 的findChildViewUnder()方法,十分方便返回指定位置的childView
- Android RecycleView 的findChildViewUnder()方法,十分方便返回指定位置的childView
- Android 十分方便的EasyRecyclerView
- 28.Android 十分方便的EasyRecyclerView
- 22.Android 十分方便的滑动标签页EasySlidingTabs
- 返回指定位置的字符串
- 返回指定位置的字符
- JS之返回指定位置字符的charAt方法
- 十分简单方便的二维码工具
- 【Javascript】返回指定位置的字符
- recycleView的条目长按换位置
- Android RecyclerView:设置指定位置的两种方法
- 一个移植十分方便的malloc函数族的实现
- RecycleView的方法
- 不用一个一个分区的整理磁盘,十分快捷方便。
- 一个移植十分方便的类似printf函数
- 一个移植十分方便的类似printf函数,升级版
- Android RecycleView的使用
- robot framework环境搭建
- android 自定义图片合集(自定义控件)
- 获取当前线程ID [NSThread currentThread]
- iOS8中提示框的使用UIAlertController(UIAlertView和UIActionSheet二合一)
- js产生随机数
- Android RecycleView 的findChildViewUnder()方法,十分方便返回指定位置的childView
- 格式化金额
- iOS入门学习(Objective-c类的声明与实现)
- 统计报表测试
- bootstrap 代码片段
- springMVC整合FastJson实现RestFul风格API
- 如何提高自己的知识水平?
- IndentationError: unindent does not match any outer indentation level(Python你是猪头吗?)
- CentOs6.5 通过vncserver安装oracle