android - ListView选中高亮

标签 android listview highlighting

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

我有一个带有 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>

然后我用这段代码模拟点击项目(home 是选定的选项):

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

谢谢!对不起我的英语。

最佳答案

一个简单的解决方法是将所选项目的值保存在适配器中,如果 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;
    }
}

关于android - ListView选中高亮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19589042/

相关文章:

java - onResume() 使我的应用程序无缘无故崩溃

Android - 如何将列表框实现为模态对话框/弹出窗口

android - 使用android在动态 ListView 中添加图标

jquery - 制作将注意力转移到网页表单的效果

javascript - 定期检测 HTML 元素内的特定字符串

Android:在不耗尽电池的情况下使用 AlarmManager 的正确方法

android - 在繁忙的 ListView 中更新 TextView 而不会失去 Spinner 焦点

java - 通过在自定义 View 的 onDraw 中设置单位矩阵来抵消 Canvas

android - 如何在使用 Android 填充 ListView 时停止显示 TextView 的 setEmptyView?