Android Listview触摸事件突出显示错误的元素
我在活动中有一个自定义列表视图。listview行项具有自定义布局,具有两个文本资源和一个按钮 该列表已正确填充,我可以在项目布局中使用“android:DegenantFocusability=blocksDescendants”单击按钮,而无需激活实际的项目行 但当我敲击不同的行时,我会看到奇怪的高光 该列表有两个条目。轻敲项目1的主行会照亮整行,很好。点击按钮将突出显示该按钮,再次显示良好 但点击项目2的主行,该行中的按钮亮起,而不是行项目本身。与正常情况一样,轻敲按钮也会使按钮亮起 那么为什么轻触时2行没有突出显示 活动有一个父相关视图:Android Listview触摸事件突出显示错误的元素,android,listview,android-listview,Android,Listview,Android Listview,我在活动中有一个自定义列表视图。listview行项具有自定义布局,具有两个文本资源和一个按钮 该列表已正确填充,我可以在项目布局中使用“android:DegenantFocusability=blocksDescendants”单击按钮,而无需激活实际的项目行 但当我敲击不同的行时,我会看到奇怪的高光 该列表有两个条目。轻敲项目1的主行会照亮整行,很好。点击按钮将突出显示该按钮,再次显示良好 但点击项目2的主行,该行中的按钮亮起,而不是行项目本身。与正常情况一样,轻敲按钮也会使按钮亮起 那么
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/screenListView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
列表视图项目:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text"
android:id="@+id/name"
android:layout_margin="5dp"
android:layout_alignParentLeft="true"
android:textSize="16sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text"
android:id="@+id/playlist"
android:textSize="12sp"
android:layout_margin="5dp"
android:layout_below="@+id/name"
android:layout_alignParentLeft="true" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/playButton"
android:src="@drawable/ic_action_play"
android:layout_alignParentRight="true" />
</RelativeLayout>
适配器:
public class ScreenListViewAdapter extends ArrayAdapter<ScreenModel> {
private final Context context;
private final ArrayList<ScreenModel> optionsArrayList;
public ScreenListViewAdapter(Context context, ArrayList<ScreenModel> optionsArrayList) {
super(context, R.layout.playlist_list_item, optionsArrayList);
this.context = context;
this.optionsArrayList = optionsArrayList;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.screen_list_item, parent, false);
if (position % 2 == 1) {
rowView.setBackgroundColor(context.getResources().getColor(R.color.list_row_colour_1));
}
rowView.setTag(optionsArrayList.get(position).getPlaylistId());
TextView name = (TextView) rowView.findViewById(R.id.name);
TextView playlistName = (TextView) rowView.findViewById(R.id.playlist);
ImageButton playBtn = (ImageButton) rowView.findViewById(R.id.playButton);
playBtn.setTag(optionsArrayList.get(position).getPlaylistId());
name.setText(optionsArrayList.get(position).getName());
playlistName.setText(optionsArrayList.get(position).getPlaylistName());
// handle "play" button
playBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// do stuff
}
});
return rowView;
}
你能发布适配器类吗?啊哈,看来改变背景颜色是原因!知道为什么会这样吗?以及如何使用不同的颜色,同时修复突出显示?您编写了这样的条件:如果位置%2==1{rowView.setBackgroundColorcontext.getResources.getColorR.color.list_row_color_1;}。这意味着只有位置值奇数,然后才进入如果condition@SandeepMaram我知道。。。问题是关于高光颜色中断。它与条件语句无关,甚至可以完全删除条件语句,只需在所有行上设置背景色即可。