突出显示“ListFragment”中的所选项目?

时间:2012-08-26 12:50:37

标签: 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

7 个答案:

答案 0 :(得分:7)

以下为我工作:

<强> RES /颜色/ 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 /值/ 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;
    }

}    

答案 1 :(得分:6)

ListView,致电setChoiceMode(ListView.CHOICE_MODE_SINGLE)。然后,只要您想突出显示所选项目,请致电setItemChecked(index, true)

答案 2 :(得分:4)

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

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)对列表项不起作用!

答案 3 :(得分:4)

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

里面

onListItemClick(ListView l,查看v ,int position,long id)

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

答案 4 :(得分:3)

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

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

答案 5 :(得分:0)

我代码

getListView().setItemChecked(0, true); 

之后

setListAdapter(oAdapter);

代码块

答案 6 :(得分:0)

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

相关问题