【问题标题】:ListView selected highlightingListView 选中高亮
【发布时间】:2013-11-04 12:35:22
【问题描述】:

首先,我知道有很多关于同一主题的问题,但我尝试了很多解决方案,但都没有实现。

我有一个带有DrawerLayout 的应用程序,我想更改代码选择的项目,选项更改但项目没有突出显示,我不知道为什么。它必须在 >= 2.3 (api 9) 的版本中工作 如果我手动单击它会起作用

项目的布局是:我将android:focusableInTouchMode 设置为false,因为我读到所选项目在启用时不起作用。 我也放了背景。

选择模式:drawerList.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/llItem"
    android:layout_width="match_parent"
    android:layout_height="48dp"
    android:background="@drawable/list_selector_holo_light"
    android:focusableInTouchMode="false">

    <ImageView
        android:id="@+id/iconoMenu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:adjustViewBounds="true"
        android:paddingLeft="5dp"
        android:paddingRight="12dp"/>

    <TextView
        android:id="@+id/etTitulo"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center_vertical"
        android:gravity="center_vertical"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="@color/menu_item"/>

</LinearLayout>

xml文件到后台:

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

    <item android:state_window_focused="false" android:drawable="@android:color/transparent" />

    <!-- Even though these two point to the same resource, have two states so the drawable will invalidate itself when coming out of pressed state. -->
    <item android:state_focused="true"  android:state_enabled="false" android:state_pressed="true" android:drawable="@drawable/list_selector_disabled_holo_light" />
    <item android:state_focused="true"  android:state_enabled="false"                              android:drawable="@drawable/list_selector_disabled_holo_light" />
    <item android:state_focused="true"                                android:state_pressed="true" android:drawable="@drawable/list_selector_background_transition_holo_light" />
    <item android:state_focused="false"                               android:state_pressed="true" android:drawable="@drawable/list_selector_background_transition_holo_light" />
    <item android:state_focused="true"                                                             android:drawable="@drawable/list_focused_holo" />
    <item android:state_focused="false" android:state_selected="true" android:drawable="@drawable/list_pressed_holo_light"/>

</selector>

然后我使用此代码模拟对项目的点击(主页是选定的选项):

drawerList.performItemClick(drawerList, home, drawerList.getItemIdAtPosition(home));

drawerList.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView parent, View view,
            int position, long id) {
        changeOption(position, true);

        view.setSelected(true);

    }
});


private void changeOption(int position, Boolean cerrar) {
    fragment = new Home();
    FragmentManager fragmentManager = getSupportFragmentManager();

    fragmentManager.beginTransaction()
    .replace(R.id.content_frame, fragment).commit();

    tituloSeccion = ((MenuLateralItem) opcionesMenu[position])
    .getTitulo();
    getSupportActionBar().setTitle(tituloSeccion);

    if ((cerrar) && (!isDrawerLocked)) {
        drawerLayout.closeDrawer(drawerList);
    }
}

谢谢!对不起我的英语。

【问题讨论】:

    标签: android listview highlighting


    【解决方案1】:

    一个简单的解决方法是如果 DrawerLayout.然后在制作convertView时,检查当前项是否为选中项并相应设置其自定义背景

    public class YourDrawerAdapter extends BaseAdapter {
    
        int selectedItem;
        // all your code goes here
    
    
        //call this method in the onItemClick listener of the listview
    
        public void setSelectedItem(int position) {
            selectedItem = position;
        }
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            //the code for the generating the view..
    
            //check here if the current item is the selected one
            if(position == selectedItem) {
                //set the selected layout for listItem
            } else {
                //set the normal layout for listItem
            }
            return convertView;
        }
    }
    

    【讨论】:

    • 是的,我试过了,但应用程序第一次只调用 getView 方法。它是一个数组适配器。谢谢你的回答
    • 选择项目后在您的适配器上调用notifyDataSetChanged()。它将重新填充列表视图。
    • 我还不能测试。我解决问题。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2014-10-24
    • 2013-08-07
    • 2023-03-05
    • 2015-02-20
    • 2011-09-07
    • 1970-01-01
    • 2011-08-30
    • 2014-09-02
    相关资源
    最近更新 更多