ListView的setItemChecked()或者setSelection()无效是怎么回事?

在Android开发中遇到ListView的setItemChecked()或setSelection()不起作用的情况,问题可能出在Item View的选择状态设置。正确做法是选择如android.R.layout.simple_list_item_activated_1的布局,以确保UI能正确显示选中状态。检查这些步骤可以帮助解决此类问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天在看android sample中NotePad的代码,突然想给NotesList中的list item添加一个单选效果:

setListAdapter(adapter);
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);

主要是设置ChoiceMode,然后在item click监听事件中添加如下代码:

@Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
    	Log.v(TAG, "choice mode = " + l.getChoiceMode());
    	getListView().setItemChecked(position, true);
    	Log.v(TAG, "checked item = " + l.isItemChecked(position));
    	return;
        /*// Constructs a new URI from the incoming URI and the row ID
        Uri uri = ContentUris.withAppendedId(getIntent().getData(), id);

        // Gets the action from the incoming Intent
        String action = getIntent().getAction();

        // Handles requests for note data
        if (Intent.ACTION_PICK.equals(action) || Intent.ACTION_GET_CONTENT.equals(action)) {

            // Sets the result to return to the component that called this Activity. The
            // result contains the new URI
            setResult(RESULT_OK, new Intent().setData(uri));
        } else {

            // Sends out an Intent to start an Activity that can handle ACTION_EDIT. The
            // Intent's data is the note ID URI. The effect is to call NoteEdit.
            startActivity(new Intent(Intent.ACTION_EDIT, uri));
        }*/
    }

虽然从log中可以看到setItemChecked()起了作用,但是从UI效果上看,界面没有任何不同,并不能高亮某个选中的item,于是本人觉得很奇怪,后来才发现了原因。原来这个和

item view有关,list view会在check某个item时,自动设置这个item view 的check状态,所以最终显示的应该是这个item view在checked状态时的图像。因此,为了达到UI上checked的效果,我们必须选用合适的item view,如果选用系统的,我选择使用 android.R.layout.simple_list_item_activated_1,代码如下:

// Creates the backing adapter for the ListView.
        SimpleCursorAdapter adapter
            = new SimpleCursorAdapter(
                      this,                             // The Context for the ListView
                      android.R.layout.simple_list_item_activated_1,//R.layout.noteslist_item,          // Points to the XML for a list item
                      cursor,                           // The cursor to get items from
                      dataColumns,
                      viewIDs
              );
这个item view是如何设置的呢,我们看一下它的layout文件\sdk\platforms\android-19\data\res\layout\simple_list_item_activated_1.xml:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:gravity="center_vertical"
    android:paddingStart="?android:attr/listPreferredItemPaddingStart"
    android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
    android:background="?android:attr/activatedBackgroundIndicator"
    android:minHeight="?android:attr/listPreferredItemHeightSmall"
/>
再看一下它用的背景图像是
android:background="?android:attr/activatedBackgroundIndicator"
这个style来自于系统文件\sdk\platforms\android-19\data\res\layout\theme.xml: 

<item name="activatedBackgroundIndicator">@drawable/activated_background</item>
这个drawable是什么呢,\sdk\platforms\android-19\data\res\drawable\activated_background.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_activated="true" android:drawable="@android:drawable/list_selector_background_selected" />
    <item android:drawable="@color/transparent" />
</selector>
哦,原来在其他状态下,这个是透明图像,在activited状态,也就是listview 设置item view的时候用的状态,就显示为
list_selector_background_selected
这个图像如下:




如果你设置了对listview的item的check或者select而UI上没有效果,请首先参考上面的步骤是否正确。^_^!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值