如何以编程方式获取android listView fast scroll thumb的视图?

时间:2016-08-07 06:06:03

标签: android listview showcaseview fastscroll

我正在使用此github库https://github.com/amlcurran/ShowcaseView在用户入职期间(或安装后第一次打开应用程序)在我的应用程序的视图元素上显示叠加层。

此库需要视图作为输入,并在您的活动上显示重点关注该视图的叠加层,以便您可以告诉用户有关该视图的更多信息。

我有一个启用快速滚动的listView。我想在快速滚动拇指上显示叠加。因此,我想查看listView的快速滚动缩略图,但由于absListView实现中没有任何公共方法,我无法做到这一点。

请帮忙。

1 个答案:

答案 0 :(得分:2)

Item的{​​{1}}实施因Android版本而异。在KitKat(API 19)之前,拇指是Article_Price,直接在FastScroller上绘制。从KitKat开始,拇指是ListView,已添加到Drawable ListView。在任何一种情况下,通过反思都可以轻松获得我们所需要的东西。

由于最终目标是将其与ImageView一起使用,因此只关注拇指的尺寸和坐标,无论其具体类型如何,都是有意义的。通过这种方式,我们可以使用ListView ViewGroupOverlay,无论Android版本。

以下反思方法抓取ShowcaseView ShowcaseView个实例,使用适当的类型确定拇指的大小和位置,然后返回PointTarget个对象如果可能的话,用拇指的中心点坐标。

ListView

要与FastScroller一起使用,我们只需检查返回的Point是否为空,然后传递从返回时创建的private Point getFastScrollThumbPoint(final ListView listView) { try { final Class<?> fastScrollerClass = Class.forName("android.widget.FastScroller"); final int[] listViewLocation = new int[2]; listView.getLocationInWindow(listViewLocation); int x = listViewLocation[0]; int y = listViewLocation[1]; final Field fastScrollerField; if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) { fastScrollerField = AbsListView.class.getDeclaredField("mFastScroll"); } else { fastScrollerField = AbsListView.class.getDeclaredField("mFastScroller"); } fastScrollerField.setAccessible(true); final Object fastScroller = fastScrollerField.get(listView); if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) { final Field thumbImageViewField = fastScrollerClass.getDeclaredField("mThumbImage"); thumbImageViewField.setAccessible(true); final ImageView thumbImageView = (ImageView) thumbImageViewField.get(fastScroller); final int[] thumbViewLocation = new int[2]; thumbImageView.getLocationInWindow(thumbViewLocation); x += thumbViewLocation[0] + thumbImageView.getWidth() / 2; y += thumbViewLocation[1] + thumbImageView.getHeight() / 2; } else { final Field thumbDrawableField = fastScrollerClass.getDeclaredField("mThumbDrawable"); thumbDrawableField.setAccessible(true); final Drawable thumbDrawable = (Drawable) thumbDrawableField.get(fastScroller); final Rect bounds = thumbDrawable.getBounds(); final Field thumbYField = fastScrollerClass.getDeclaredField("mThumbY"); thumbYField.setAccessible(true); final int thumbY = (Integer) thumbYField.get(fastScroller); x += bounds.left + bounds.width() / 2; y += thumbY + bounds.height() / 2; } return new Point(x, y); } catch (Exception e) { e.printStackTrace(); } return null; } 一个ShowcaseView

Point