【问题标题】:Highlight selected item in "ListFragment"?突出显示“ListFragment”中的选定项目?
【发布时间】:2012-08-21 05:59:24
【问题描述】:

我已多次发布相同的问题,但尚未解决。我有一个ListFragment,我想突出显示列表中的选定项目。有人建议我使用“选择器”。我不明白如何使用这个选择器。我的ListFragment 班级是:

// Create an adapter with list of stores and populate the list with
        // values
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_list_item_1, StoreList);
        setListAdapter(adapter);
        mDbHelper.close();
    }

    /*
     * (non-Javadoc)
     * 
     * Handles the event when an item is clicked on left pane, performs action
     * based on the selection in left pane
     *  
     * @see android.app.ListFragment#onListItemClick(android.widget.ListView,
     * android.view.View, int, long)
     */
    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        String selectedStore = (String) getListAdapter().getItem(position);
        DetailFragment fragment = (DetailFragment) getFragmentManager()
                .findFragmentById(R.id.detailFragment);
        if (fragment != null && fragment.isInLayout()) {
            v.setBackgroundColor(getResources().getColor(R.color.BLUE));
            // passes selectedStore to detail fragment  
            fragment.setText(selectedStore);

            // getItemList(selectedStore);

        }

使用 setBackground 永久设置颜色,但我希望在选择另一个项目时它会消失。 我了解如何在ListView 中使用选择器,但在我的情况下,如果我没有为Listview 定义任何xml,那么我将如何使用“选择器”?我正在使用预定义的android.R.layout.simple_list_item_1

【问题讨论】:

  • Android 中ListView 的“选择”概念适用于方向键、轨迹球、箭头键和其他指针设备。在平板电脑上,有一个“激活”行的相关概念,旨在突出显示触摸屏上最后点击的项目,为与其相邻的内容提供上下文(例如,主从模式)。

标签: android listview android-listview android-fragments android-listfragment


【解决方案1】:

以下对我有用:

res/color/menu_highlight.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item 
    android:state_pressed="true"
    android:drawable="@color/red"  />
  <item
    android:state_selected="true"
    android:drawable="@color/red" />
  <item 
    android:drawable="@color/white" />
</selector>

res/values/colors.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources> 
<color name="white">#FFFFFF</color>
<color name="red">#FF0000</color>
</resources>

res/layout/menuitem.xml::(列表中每个项目的 XML)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent">
    <TextView
        android:id="@+id/textmenu"
        android:layout_height="wrap_content"
        android:text="text"
        android:textColor="#FFFFFF" 
        android:background="@color/menu_highlight"
        android:visibility="visible"
        android:layout_width="fill_parent" />
</LinearLayout>

最后,在ListFragment类中,添加View previous,在onlistitemclick函数中添加如下代码..(ListFragment: highlight selected row中提到)

public class MenuListFragment extends ListFragment{

View previous;

@Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id); 
       //Logic to highlight selected item
        previous.setSelected(false);
        v.setSelected(true);
        previous=v;
    }

}    

【讨论】:

  • 这听起来会遇到 Stefan 在接受的答案中提到的相同问题:当列表大于屏幕时,您将遇到问题。因为相同的视图将被重新用于以前隐藏的项目,当它们被显示时,它们中的一些可以被显示为“选中”。
【解决方案2】:

从您的ListView 致电setChoiceMode(ListView.CHOICE_MODE_SINGLE)。然后,每当您想突出显示选定的项目时,请致电setItemChecked(index, true)

【讨论】:

  • 如果您将自定义适配器与自定义列表项布局一起使用,这将不起作用。
【解决方案3】:

我尝试了同样的方法,但没有找到任何好的解决方案。 我实际上做的是使用这段代码来设置监听器:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
    public void onItemClick(AdapterView<?> parent, View view, int position, long id){
        list_adapter.setSelectedPosition(position);
        listView.invalidate();
    }
});

列表适配器定义以下公共方法的地方

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View rowView = GuiBuilder.createHeroListItemView(heroes.get(position),getContext(),parent,false);
    if(position == selected_pos){
        rowView.setBackgroundColor((rowView.getResources().getColor(R.color.list_item_selected_color)));
    }
    return rowView;
}

public void setSelectedPosition(int selected_pos){
    this.selected_pos = selected_pos;
}

public int getSelectedPosition(){
    return selected_pos;
}

也就是说,我以编程方式更改列表项的背景。 此外,为了避免单击列表元素时出现闪烁效果,我没有为按下状态定义任何选择器

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true"
        android:drawable="@drawable/list_item_focused_color" />
    <item android:drawable="@drawable/list_item_default_color" />
</selector>

这符合我的预期。我没有找到更好的解决方案,因为 setSelected(true) 不适用于列表项!

【讨论】:

  • 看起来不错,会试试这个,让你知道我发现了什么。
【解决方案4】:

我没有得到我想要的东西,所以我继续挖掘并想出了一个“便宜”的解决方案,这可能不是最佳实践,但可以完成工作。 我希望该项目在选择时突出显示,并且当从 listFragment 中选择其他项目时颜色应该消失。 这对我有用-我定义了一个静态View V; 并初始化它V = new View(getActivity()); 然后在我的

里面

onListItemClick(ListView l, View v, int position, long id)

            V.setBackgroundResource(0);
            v.setBackgroundResource(R.color.BLUE);
            V = v;

【讨论】:

  • 甚至可以称其为不好的做法,而不是“不是最佳做法”;在静态对象中存储一个视图,该视图保持对活动的引用。从而泄露活动。
  • 当列表变得比屏幕大时你会遇到问题。因为相同的视图将被重新用于以前隐藏的项目,当它们被显示时,它们中的一些可以被显示为“选中”。
  • @Stefan 我有这个问题。你知道怎么解决吗? stackoverflow.com/questions/31442067/…
【解决方案5】:

这在ListFragment 中对我来说非常有用,但我认为它仅适用于 Android 4.0 及更高版本。我使用android:background="?android:attr/activatedBackgroundIndicator 为列表项创建了一个简单的布局(如果Android 低于4.0,您需要在drawables 中找到类似的布局):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical"
android:background="?android:attr/activatedBackgroundIndicator"
android:orientation="horizontal" >

<TextView
    android:id="@+id/list_item_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ellipsize="end"
    android:singleLine="true"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:gravity="center_vertical"
    android:layout_margin="@dimen/double_view_margin" />

然后,只需使用此布局创建一个简单的ArrayAdapter 并将选择模式设置为单一:

final ArrayAdapter<String> labelsAdapter = new ArrayAdapter<String>(getActivity(), R.layout.item_simple_list_item, R.id.list_item_text, labels);

setListAdapter(labelsAdapter);

getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);

之后,您可以使用setItemChecked 方法突出显示必要的项目:

@Override
public void onListItemClick(final ListView l, final View v, final int position, final long id) {
    getListView().setItemChecked(position, true);
}

【讨论】:

  • 如果您不覆盖onListItemClick,那么ListFragment 将自动将该项目设置为选中。或者只是确保致电super.onListItemClick(...)
【解决方案6】:

我的代码

getListView().setItemChecked(0, true); 

之后

setListAdapter(oAdapter);

代码块

【讨论】:

    【解决方案7】:

    类似于 type-a1pha 但带有选择器的东西,一旦你有了视图,你就可以 view.setSelected(true) 并且添加你在选择器上定义的样式。 执行操作后,您需要删除该标志。

    【讨论】:

      猜你喜欢
      • 2018-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-21
      • 2012-02-02
      • 1970-01-01
      相关资源
      最近更新 更多