我想在用户按下它时删除gridView项目的阴影,然后在用户释放后恢复它。 (不能使用selector.xml,因为项目具有用户选择的颜色)
此代码在第一次按下时会移除阴影,但在释放后会保持卡住而没有阴影。
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;
}
});
我无法将其设置为true,因为然后.nnItemClickListener在片段中运行。另外我通过在onItemClickListener中设置阴影来修复它,但是如果用户将拇指滑出项目而不是仅仅点击它将保持按下
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;
}
});
答案 0 :(得分:1)
else if(event.getAction()==MotionEvent.ACTION_UP){
image.setShadow();
image.invalidate();
}
你可以在这里做的是尝试创建一个自定义Gridview
并覆盖onTouchEvent,就像这样,(虽然它不准确)
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
}
}
这里的逻辑onTouchEvent()
最初是在onTouchListener
之前调用的,它是第一个被调用的,返回true然后会将事件传递给onTouch()
和{{1}一切都会好起来的
希望它有用
答案 1 :(得分:0)
尝试在视图上添加以下内容。
android:clickable="true"