Android ListView listSelector无法正常工作 [英] Android ListView listSelector not working

查看:249
本文介绍了Android ListView listSelector无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将自定义选择器设置为ListView.在较新的设备上可以正常工作,但在较低版本的设备上不能正常工作.我希望ListView选中的项目保持突出显示状态.

I am trying to set a custom selector to a ListView. It's working fine on newer devices but not working on lower version of devices.I want the ListView selected item to be stay highlighted.

请帮助.

谢谢.

ListView.xml

<?xml version="1.0" encoding="utf-8"?>
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >

            <ListView
                android:id="@+id/listViewBell"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:cacheColorHint="#00000000"
                android:choiceMode="singleChoice"
                android:divider="#b5b5b5"
                android:dividerHeight="1dp"
                android:listSelector="@drawable/list_selector_color" >

            </ListView>

        </LinearLayout>

list_selectror_color.xml

<?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle" >

        <solid android:color="@color/list_selector" />

        <stroke
            android:dashWidth="2dp"
            android:width="1dp"
            android:color="@color/white" />

    </shape>

我也尝试过使用选择器,但没有任何反应

I have also tried with selector but nothing happens

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/list_selector_color" android:state_pressed="true"/>
    <item android:drawable="@drawable/list_selector_color" android:state_focused="true"/>
    <item android:drawable="@drawable/list_selector_color" android:state_selected="true"/>
    <item android:drawable="@drawable/list_selector_normal"/>

</selector>

这是我的自定义适配器getView方法

Here is my Custom adapter getView Method

public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        convertView = inflater.inflate(R.layout.listview_bell_items, parent,
                false);

        ImageView imageview = (ImageView) convertView
                .findViewById(R.id.list_bell_image);
        imageview.setImageResource(mDataImage[position]);

        TextView textview = (TextView) convertView
                .findViewById(R.id.txt_bell_title);
        textview.setText(mDataText[position]);

        return convertView;
    }

推荐答案

我遇到了类似的问题.抱歉,无法发表评论,所以我发表了一个可能的答案.

I had similar problem. Sorry, cannot comment, so I post a possible answer.

  1. 从您的ListView声明中删除android:listSelector="@drawable/list_selector_color"属性

在您的R.layout.listview_bell_items中,为根布局指定自定义选择器.例如,如果列表项的根目录布局为RelativeLayout,请尝试:
<RelativeLayout ... android:background="@drawable/listitem_selector">...

In your R.layout.listview_bell_items specify your custom selector for a root layout. E.g., if root layout of your list item is RelativeLayout, try:
<RelativeLayout ... android:background="@drawable/listitem_selector">...

任何其他类型的布局也是如此.

The same goes for any other type of Layout.

如果仍然不能满足您的要求,请提供更多详细信息.

If this still still does not give you the result you want, provide more details.

更新 好的,如果没有其他帮助,则可能是您的问题的暂时解决方法.我不明白为什么它行不通.

Update Ok, if nothing else helps, there's a temporary dirty workaround of your problem. I do not see why it would not work.

引入一个 selectedPos 变量来保存当前选中的项目.

Introduce a selectedPos variable holding currently selected item.

private class MyAdapter extends .../*your base adapter*/ {                
    private static final int NOT_SELECTED = -1;
    private int selectedPos = NOT_SELECTED;

    // if called with the same position multiple lines it works as toggle
    public void setSelection(int position) {
        if (selectedPos == position) {
            selectedPos = NOT_SELECTED;
        } else {
            selectedPos = position;
        }
        notifyDataSetChanged();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);
        if (position == selectedPos) {
            // your color for selected item
            view.setBackgroundColor(Color.parseColor("#000000"));
        } else {
            // your color for non-selected item
            view.setBackgroundColor(Color.parseColor("#FFFFFF"));
        }
        return view;
    }
}

现在,在创建并设置ListView的适配器后,添加以下代码:

Now, add the following code after you create and set ListView's adapter:

final MyAdapter adapter = new MyAdapter(...);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        adapter.setSelection(position);
    }
});

这篇关于Android ListView listSelector无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆