2016-08-07 2 views
1

나는이 github 라이브러리 https://github.com/amlcurran/ShowcaseView을 사용하여 사용자 onboarding (또는 설치 후 처음 열어 본 앱) 중에 내 앱의보기 요소에 오버레이를 표시합니다.안 드 로이드 목록보기 빠른 스크롤 엄지 손가락을 프로그래밍 방식으로 얻는 방법?

이 라이브러리는보기를 입력해야하며 사용자에게 해당보기에 대해 더 알릴 수 있도록 해당보기에 초점을 맞춘 활동에 대한 오버레이를 표시합니다.

빠른 스크롤을 사용할 수있는 listView가 있습니다. 빠른 스크롤 썸에 오버레이를 표시하고 싶습니다. 따라서 listView의 빠른 스크롤 썸을 볼 수 있지만 absListView 구현에 public 메서드가 없기 때문에이 작업을 수행 할 수 없습니다.

도와주세요.

+0

글쎄, 당신은 이것을 반영 할 수 있지만, 안드로이드 버전에 따라 크게 달라질 수 있습니다. 어떤 버전을 지원하고 있습니까? –

+0

의견을 보내 주셔서 감사합니다. @MikeM. API 14 이상의 모든 버전을 지원하고 싶습니다. 좀 더 안내 할 수 있습니까? –

+0

API 18부터는 엄지 손가락이'ImageView '이므로 충분히 쉽게 읽을 수 있지만 특정 버전에 따라 필드 이름이 달라집니다. 별로 중요하지 않습니다. 그러나 API 18 이전에는 엄지 손가락이 'ListView'에 직접 그려진 Drawable이므로 라이브러리에 전달할 수있는 특정 'View'가 없습니다. 그 라이브러리를 사용하지 않았으므로 다른 옵션을 사용할 수 있는지 여부를 모르겠습니다. –

답변

1

ListView에 대한 FastScroller 구현은 Android 버전에 따라 다릅니다. KitKat (API 19) 이전에는 엄지 손가락이 Drawable이며 ListView에 직접 그려져 있습니다. KitKat부터는 손가락이 ImageView이고 ListViewViewGroupOverlay에 추가됩니다. 두 경우 모두, 반영을 통해 필요한 것을 얻는 것은 쉽습니다.

궁극적 인 목표는 ShowcaseView과 함께 사용하는 것이므로 특정 유형에 관계없이 엄지 손가락의 크기와 좌표에만 신경을 쓰는 것이 좋습니다. 이 방법으로 Android 버전과 상관없이 ShowcaseViewPointTarget을 사용할 수 있습니다.

다음 반사 방법은 ListViewFastScroller 인스턴스 잡고는 엄지 손가락의 크기에 적절한 유형을 사용하여 위치를 결정하고, 가능한 경우, 엄지의 중심점의 좌표와 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(); 
} 
+0

이 답변을 주셔서 감사합니다, @MikeM. API 19+에서이 작업을 신속하게 실행하고이 대답을 받아 들였습니다. :) –

+0

괜찮습니다. 간단한 테스트를 위해 ImageView에 다른 이미지를 설정하여 올바른 것을 얻고 있는지 확인했습니다. 나중에 아마 내일까지는 시간이 걸릴 것입니다. 그 라이브러리를 살펴보고 이전 버전의 Drawable으로 할 수있는 것을 볼 수 있습니다. –

+0

당신은 대단합니다, @MikeM. 고마워요! 이것은 나에게 많은 시간을 안겨주고 + 나는 안드로이드에서 뷰를 얻는 새로운 방법을 배웠다. 나는 또한'Drawable' 문제를 보려고했다. 해결책을 찾지 못하면 다음에 시간을 낼 때 더 많은 도움이 될 수 있다면 감사하게 생각합니다. 그럼에도 불구하고, 이것은 확실히 도움이되었습니다! –

관련 문제