Skip to content

Instantly share code, notes, and snippets.

@obiwong
Created July 14, 2012 02:36
Show Gist options
  • Save obiwong/3108898 to your computer and use it in GitHub Desktop.
Save obiwong/3108898 to your computer and use it in GitHub Desktop.
custom view test long press
/* usage:
<com.ht.longpresstest.ItemView
android:layout_width="40dp"
android:layout_height="40dp"
android:background="@drawable/ic_launcher"
/>
*/
public class ItemView extends View {
public ItemView(Context context) {
super(context);
init();
}
public ItemView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public ItemView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View v) {
ViewGroup parent = (ViewGroup) getParent();
int i = 0;
for (; i < parent.getChildCount(); i++) {
if (parent.getChildAt(i) == ItemView.this) {
break;
}
}
if (i < parent.getChildCount()) {
Toast.makeText(getContext(), "child index is: " + i, Toast.LENGTH_SHORT).show();
}
return true;
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment