我有一个名单ListView。为它的触感写了一个处理程序。当您触摸菜单项(ACTION_DOWN)时,我将其高亮显示。当您释放项目(ACTION_UP) -返回原始颜色。问题是,如果你触摸和滚动-然后该项目是突出显示。或者,如果你触摸和移动你的手指在另一个项目。
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);
}}发布于 2014-07-13 01:01:27
不要使用触摸处理程序,而是将文本视图设置为使用文本颜色的颜色状态列表,并将背景资源设置为可绘制的选择器。框架将根据视图的当前状态(例如按下)处理选择正确的资源。
res/color/color_color.res:
<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>https://stackoverflow.com/questions/24716787
复制相似问题