ListView
에 대한 FastScroller
구현은 Android 버전에 따라 다릅니다. KitKat (API 19) 이전에는 엄지 손가락이 Drawable
이며 ListView
에 직접 그려져 있습니다. KitKat부터는 손가락이 ImageView
이고 ListView
의 ViewGroupOverlay
에 추가됩니다. 두 경우 모두, 반영을 통해 필요한 것을 얻는 것은 쉽습니다.
궁극적 인 목표는 ShowcaseView
과 함께 사용하는 것이므로 특정 유형에 관계없이 엄지 손가락의 크기와 좌표에만 신경을 쓰는 것이 좋습니다. 이 방법으로 Android 버전과 상관없이 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;
}
우리는 단순히 반환
Point
가 null가 아닌 것을 확인하고 반환에서 만든
Builder
PointTarget
을 통과
ShowcaseView
이것을 사용합니다.
Point thumbPoint = getFastScrollThumbPoint(listView);
if (thumbPoint != null) {
new ShowcaseView.Builder(this)
.setTarget(new PointTarget(thumbPoint))
.setContentTitle("ShowcaseView")
.setContentText("This is highlighting the fast scroll thumb")
.hideOnTouchOutside()
.build();
}
글쎄, 당신은 이것을 반영 할 수 있지만, 안드로이드 버전에 따라 크게 달라질 수 있습니다. 어떤 버전을 지원하고 있습니까? –
의견을 보내 주셔서 감사합니다. @MikeM. API 14 이상의 모든 버전을 지원하고 싶습니다. 좀 더 안내 할 수 있습니까? –
API 18부터는 엄지 손가락이'ImageView '이므로 충분히 쉽게 읽을 수 있지만 특정 버전에 따라 필드 이름이 달라집니다. 별로 중요하지 않습니다. 그러나 API 18 이전에는 엄지 손가락이 'ListView'에 직접 그려진 Drawable이므로 라이브러리에 전달할 수있는 특정 'View'가 없습니다. 그 라이브러리를 사용하지 않았으므로 다른 옵션을 사용할 수 있는지 여부를 모르겠습니다. –