突出显示列表视图中的选定行

时间:2014-04-01 11:03:13

标签: android android-layout listview android-listview

我有一个带有列表视图的两个窗格布局和一个显示所选项目的完整内容的PagerView。 我希望这两者之间存在双向关系。

  • 从列表视图中选择新项目时,项目将突出显示,ViewPager将显示相应的内容。
  • 当ViewPager滚动到新项目时。将在列表视图中选择Coresponding项目。

我在single_row.xml文件中设置了背景collor

<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>

我也使用了singleChoise模式。

<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 
 }

那我该怎么办?我有一个项目索引,我想在这个方法中突出显示它。

3 个答案:

答案 0 :(得分:1)

kiruwka的第二个解决方案是全部工作,我在我的项目中使用它。然而,这是我发现的另一个(一个更复杂的)解决方案。

public void highlightItem(int position){
    int selectedIndex = listView.getSelectedItemPosition();
    if(position != selectedIndex)
    {

        listView.performItemClick(
                listView.getAdapter().getView(position, null, null),
                position,
                listView.getAdapter().getItemId(position));
    }
}

答案 1 :(得分:0)

试试这个:

 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        for (int j = 0; j < adapterView.getChildCount(); j++)
            adapterView.getChildAt(j).setBackgroundColor(Color.TRANSPARENT);

        // change the background color of the selected element
        view.setBackgroundColor(R.color.YOUR_COLOUR);
});

答案 2 :(得分:0)

添加listView.requestFocusFromTouch()

public void highlightItem(int position) {
    listView.requestFocusFromTouch();
    listView.setSelection(position);
 }

替代解决方案是在不请求焦点或设置选择的情况下调用listView.setItemChecked(position, true);,为此工作添加

<item android:state_activated="true" android:drawable="@color/pressed_color"/>
在你的drowable / bg_key.xml

重要提示::哦,不要忘记为列表设置选择模式,你需要做一次,即:

listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

希望有所帮助

相关问题