I want to remove a gridView item's shadow when the user presses it, and then restore it once the user releases. (cant use selector.xml because items have a user chosen color)
this code removes shadow when first pressed but upon release it stays stuck down with no shadow.
gridItemView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_DOWN) {
image.removeShadow();
image.invalidate();
}
else if(event.getAction()==MotionEvent.ACTION_UP){
image.setShadow();
image.invalidate();
}
return false;
}
});
I cant set it to true, because then .OnItemClickListener in the fragment dosent work. Also I kinda fixed it by setting the shadow to turn on in onItemClickListener, but if the user slides their thumb off the item instead of just clicking it will stay pressed
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
MainActivity.selectedItem = position;
if (lastView[0] != null) {
lastView[0].setBackgroundResource(R.drawable.nullr);
}
picker.setOldCenterColor(MainActivity.items.get(MainActivity.selectedItem).getColor());
picker.setColor(MainActivity.items.get(MainActivity.selectedItem).getColor());
View imageContainer = view.findViewById(R.id.imageContainer);
CircularImageView circleImage = (CircularImageView) view.findViewById(R.id.circleView);
artistText.setText(MainActivity.items.get(position).getArtist());
songText.setText(MainActivity.items.get(position).getSong());
int backgroundColor = Color.parseColor("#FFEECC");
GradientDrawable drawable = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, new int[]{backgroundColor, backgroundColor});
drawable.setShape(GradientDrawable.OVAL);
drawable.setStroke(25, MainActivity.items.get(MainActivity.selectedItem).getColor());
imageContainer.setBackgroundDrawable(drawable);
circleImage.setShadow();
circleImage.invalidate();
lastView[0] = imageContainer;
MainActivity.anItemIsSelected = true;
}
});
you forgot
else if(event.getAction()==MotionEvent.ACTION_UP){
image.setShadow();
image.invalidate();
}
what you can do here is try to create a custom Gridview
and override onTouchEvent, something like this, (it is not precisely accurate though)
public class MyGridv extends GridView{
//implement all constructors;
//the override onTouchEvent(MotionEvent event);
@override
protected boolean onTouchEvent(MotionEvent event){
// put your ontouch code here and return true, you might need to do
//some changes because you can not get access to your methods or
//you can make this class a private class to your mainActivity
// now delete your ontouch for your gridview
}
}
the logic here is onTouchEvent()
is initially called before your onTouchListener
and its the first to be called, returning true there will then pass the event to onTouch()
and on onClick
, there everything will work fine
hope its helpful
尝试在您的视图上添加以下内容。
android:clickable="true"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.