ListView和行选择器不工作 [英] Listview and row selector not working

查看:176
本文介绍了ListView和行选择器不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我疑惑这一点。我正在寻找在2小时内不能明白发生了什么。

I'm puzzled with this. I am searching for over 2 hours and cannot understand what is happening.

我有我想要的物品(行)是可选列表视图。
要做到这一点,我有一个选择,我设置为背景对我行的布局。

I have a Listview that I want the items (rows) to be selectable. To achieve this, I have a selector that I set as a background to my row layout.

ListView控件在fragment.xml之布局文件定义是这样的:

The ListView is defined this way in the fragment.xml layout file:

    <ListView
        android:id="@+id/lst_friends"
        android:layout_width="match_parent"
        android:dividerHeight="1dp"
        android:divider="#ff0000"
        android:layout_height="0dp"
        android:layout_weight="1.0" />

选择器( bg_row_contact.xml )是这样的:

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@android:color/white" android:state_selected="false"/>
    <item android:drawable="@color/pink" android:state_selected="true"/>

</selector>

我行布局( row_contact.xml )是这样的:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/bg_row_contact"
    android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_margin="7dp"
        android:gravity="center" >
        <ImageView
            android:id="@+id/img_lst"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="@string/contentDescription_pic" />
    </LinearLayout>

    <TextView
        android:id="@+id/txt_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginRight="20dp"
        android:layout_marginLeft="10dp"
        android:ellipsize="end"
        android:maxLines="2"
        android:textColor="#000000"
        android:textSize="16sp" />

</LinearLayout>

现在,我有一个自定义适配器,在我这样做是为了纪念项目作为选择时的行,这完美的作品在用户点击:

Now, I have a custom adapter, in which I do this to mark an item as selected when the user clicks on the row, which works perfect:

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // View from recycle
        View row = convertView;

        // Handle inflation and make sure not to re-use a header view
        if (row == null) {
            LayoutInflater inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.row_contact, null);
            row.setTag(position);

            row.setOnClickListener(new OnClickListener() {                  
                @Override
                public void onClick(View v) {
                    v.setSelected(!v.isSelected());
                }
            });
        }

        boolean condition = (position==3);

        row.setSelected(condition);
    }

我现在的问题是,我想很明显保持选中状态时,列表视图滚动。所以,我有我对证,并设置该行对这一选择或不化,你可以看到在一个条件

My problem now is that I want to obviously maintain the selection state when the listview is scrolled. So I have a condition which I check against and set the row to selected or not based on this, as you can see in the

row.setSelected(boolean condition);

的问题是,虽然条件对于一些行属实,该行的背景选择不工作,行不highlighed。

The problem is that although the condition is true for some rows, the row's background selector is not working and the row is not highlighed.

我已经试过一切,但不能找到什么喊来是怎么回事。

I have tried everything, but cannot find what the bleep is going on here.

有什么建议?

推荐答案

我一直在玩弄一点,我想我找到了解决您的问题。这一件作品与 list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

I've been playing around a little and I think I found a solution to your problem. This one works with list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

您可以通过 list.getCheckedItemPositions所选的项目()。关于它的唯一奇怪的是,在你的选择,你必须使用的android:state_activated 而不是的android:state_checked

You can get the selected items via list.getCheckedItemPositions(). The only weird thing about it is, that in your selector you have to use android:state_activatedinstead of android:state_checked.

活动code:

    ListView list = (ListView) findViewById(R.id.listView);

    // dummy values
    final String[] values = new String[] { "a", "b", "c", "d", "e", "f", "g",
            "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
            "t", "u", "w", "x", "y", "z" };
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            R.layout.row_contact, values){

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // View from recycle
            View row = convertView;

            // Handle inflation and make sure not to re-use a header view
            if (row == null) {
                LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                row = inflater.inflate(R.layout.row_contact, null);
                row.setTag(position);

            }
            ((TextView)row.findViewById(R.id.txt_title)).setText(values[position]);
            return row;
        }

    };
    list.setAdapter(adapter);
    list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

选择器(bg_row_contact.xml)

The selector (bg_row_contact.xml)

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@android:color/white" android:state_activated="false"/>
    <item android:drawable="@android:color/black" android:state_activated="true"/>

</selector>

希望这有助于。

这篇关于ListView和行选择器不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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