Java ListView onTouch运动事件

Java ListView onTouch运动事件,java,android,listview,motionevent,ontouch,Java,Android,Listview,Motionevent,Ontouch,我有一个列表视图。为它编写了一个处理程序。当你触摸菜单项时(操作向下),我会突出显示它。当您释放该项目时(操作向上)-返回原始颜色。 问题是,如果您触摸并滚动-然后该项目会高亮显示。或者,如果您在其他项目上触摸并移动手指 public boolean onTouch(View v, MotionEvent event) { if (event.getAction()==MotionEvent.ACTION_DOWN) { holder.tv_name_exercise.setTextCo

我有一个列表视图。为它编写了一个处理程序。当你触摸菜单项时(操作向下),我会突出显示它。当您释放该项目时(操作向上)-返回原始颜色。 问题是,如果您触摸并滚动-然后该项目会高亮显示。或者,如果您在其他项目上触摸并移动手指

public boolean onTouch(View v, MotionEvent event) {
if (event.getAction()==MotionEvent.ACTION_DOWN) {
    holder.tv_name_exercise.setTextColor(Color.parseColor("#fe9503"));
    holder.tv_description_exercise.setTextColor(Color.parseColor("#ffffff"));
    holder.row.setBackgroundResource(R.drawable.list_item_bg_active);
    }
if (event.getAction()==MotionEvent.ACTION_UP) {
    holder.tv_name_exercise.setTextColor(Color.parseColor("#000000"));
    holder.tv_description_exercise.setTextColor(Color.parseColor("#666667"));
    holder.row.setBackgroundResource(R.drawable.list_item_bg);
    }}

不要使用触摸处理程序,而是将文本视图设置为使用文本颜色的颜色状态列表,并将背景资源设置为可绘制的选择器。该框架将根据视图的当前状态(如按下)选择正确的资源

res/color/exercise_color.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:color="#fe9503" />
    <item android:color="#000000"/>
</selector>

res/drawable/row_bg.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
          android:drawable="@drawable/list_item_bg_active" />
    <item android:color="@drawable/list_item_bg"/>
</selector>


我两个都试过了。有工作背景。有文字的人不想工作工作,但state_pressed=“true”不工作。这就是我尝试使用MotionEvent的原因。你能尝试在TextView上设置android:duplicateParentState=“true”吗?是的,在TextView上。使用android:duplicateParentState=“true”-工作。森克斯!