android - 突出显示 "ListFragment"中的选定项目?

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

同样的问题我已经发过好几次了,但是还没有解决。我有一个 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

最佳答案

以下对我有用:

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

}    

关于android - 突出显示 "ListFragment"中的选定项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12130266/

相关文章:

android - 如何处理android屏幕 fragment ?

java - 在 Android ListView 中添加商品价格

java - 如何在Android ListView中加载list_item_event.xml?

android - 如何从另一个页面控制 viewPages 页面

android - 为 Android arm64-v8a 构建 OpenCV 时出错

android - 应用程序应提示按下后退按钮

Android ListView丢失项目高度

android - 有时 ListView 出现 viewTypeCount < 1 错误

android - 动态 ListView OnItemClick - 应该在另一个 Activity 中填充值

java - ListView 包含复选框滚动问题