Skip to content

Instantly share code, notes, and snippets.

@amyu
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amyu/cd742fbf5803b3e83d9a to your computer and use it in GitHub Desktop.
Save amyu/cd742fbf5803b3e83d9a to your computer and use it in GitHub Desktop.
Touch座標からIndexとるやつ
import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.ListView;
/**
* Created by amyu on 15/02/28.
*/
public class HitRectListView extends ListView {
public HitRectListView(Context context) {
super(context);
}
public HitRectListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public HitRectListView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public HitRectListView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
public int getTouchChildIndex(final int x, final int y) {
int childCount = getChildCount();
Rect rect = new Rect();
for (int index = 0; index < childCount; index++) {
getChildAt(index).getHitRect(rect);
if (rect.contains(x, y)) {
return index;
}
}
return -1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment