【发布时间】:2014-04-01 11:03:13
【问题描述】:
我有一个包含列表视图和显示所选项目全部内容的 PagerView 的两个窗格布局。 我想要这两者之间的双向关系。
- 当从列表视图中选择新项目时,项目将突出显示,ViewPager 显示相应的内容。
- 当 ViewPager 滚动到新项目时。对应的项目将在列表视图中被选中。
我在 single_row.xml 文件中设置了背景颜色
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:background="@drawable/bg_key"
>
<ImageView
.../>
<TextView
.../>
<TextView
.../>
</RelativeLayout>
这是我的 drowable/bg_key.xml 文件
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true"
android:drawable="@color/pressed_color"
/>
<item
android:drawable="@color/default_color" />
</selector>
我也使用了单选模式。
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@android:id/list"
android:layout_gravity="center_horizontal"
android:choiceMode="singleChoice" />
这是我的 ListItemClicked 事件处理程序。
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
communicator.indexChanged(position);
view.setSelected(true);
}
});
一切都很完美!!除了不是一切。当我单击一个项目时,它将被选中并突出显示,并且 ViewPager 显示内容。但是当 ViewPager 滚动到新项目时,我无法在列表视图中突出显示相应的项目。
在 MyListFragment 中有一个名为 hightlightItem 的方法,当 PagerView 中的内容发生变化时会调用该方法。
public void highlightItem(int position){
//position is a index of item i want to hightlight using java. All code to here are tested!
listView.setSelection(position); //? this will not select any item. Also it will remove the selected item as well
listView.setChecked(position,true); //? doesn't work neither
}
那我该怎么办?我有一个项目索引,我想在这个方法中突出显示它。
【问题讨论】:
-
抱歉,我没有注意到您已经为物品提供了
selector。请看我对你的问题的回答。 -
不,我没有。我确实在xml文件中设置了它。如果您再次查看我发布的代码,您会找到它。我不知道为什么这不起作用,但我得到了我想要的结果。
-
是的,这就是我的意思。
<selector>在 xml 中设置。我在两天前回答问题之前发表了该评论:)
标签: android android-layout listview android-listview