Created
July 14, 2012 02:36
-
-
Save obiwong/3108898 to your computer and use it in GitHub Desktop.
custom view test long press
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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