我有一个习惯ListView
,我OnTouchListener
为每个列表项设置了一个。当用户触摸项目时,我使用TransitionDrawable
在 期间更改项目的背景颜色MotionEvent.ACTION_DOWN
并使其恢复正常MotionEvent.ACTION_UP
。
现在,当我滚动此列表时,滚动操作开始时触摸的项目会更改背景颜色。我想避免这种行为。因此,在滚动时,我想禁用背景颜色的任何更改。有没有办法做到这一点?请看下面的代码:
public class ListItemOnTouchListener implements OnTouchListener {
@Override
public boolean onTouch(View v, MotionEvent event) {
Context mContext = getActivity().getApplicationContext();
Resources res = mContext.getResources();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
TransitionDrawable transitionDown = (TransitionDrawable) res
.getDrawable(R.drawable.row_background_transition_down);
v.setBackgroundDrawable(transitionDown);
transitionDown.startTransition(350);
Log.d(TAG, "ACTION_DOWN");
return true;
case MotionEvent.ACTION_UP:
TransitionDrawable transitionUp = (TransitionDrawable) res
.getDrawable(R.drawable.row_background_transition_up);
v.setBackgroundDrawable(transitionUp);
transitionUp.startTransition(1000);
// Get list view
ListView listView = getListView();
int position = listView.getPositionForView((LinearLayout) v.getParent());
listView.performItemClick(v, position, 0);
Log.d(TAG, "ACTION_UP");
return true;
case MotionEvent.ACTION_MOVE:
Log.d(TAG, "ACTION_MOVE");
break;
case MotionEvent.ACTION_CANCEL:
Log.d(TAG, "ACTION_CANCEL");
v.setBackgroundResource(R.drawable.row_background_normal);
break;
}
return false;
}
}