时,我有一个自定义的ListView
中,我已经为每个列表项的OnTouchListener
。当用户触摸的项目,我用TransitionDrawable
改变时MotionEvent.ACTION_DOWN
项目的背景颜色,并在MotionEvent.ACTION_UP
使其恢复正常。定制的ListView问题与onTouch滚动
现在,当我滚动这个列表,这是在滚动行动开始触项目改变背景颜色。我想避免这种行为。所以,当滚动我想禁用任何背景颜色的变化。有没有办法做到这一点?请参阅下面的代码:
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;
}
}
如果撤消对ACTION_UP效果不是那么重要,那么你可以替换成一个计时器,并使用点击或选择的事件? – 2013-03-14 14:46:43
实际上,在ACTION_UP中改变颜色,因为我想显示淡化效果。另外,我在ACTION_UP中生成itemclick事件,因为我想注册用户的点击。请参阅我编辑的问题中的代码。 – Robin 2013-03-14 15:12:29