Listview setOnItemClickListener不会触发

时间:2016-12-28 10:28:50

标签: android xml listview

我试图以编程方式更改ListView项目的背景颜色,当用户点按它时。我正在尝试使用setOnItemClickListener实现解决方案,但我无法使侦听器工作。这是我的代码:

ArrayList<Integer> menuIcons = app.Theme.Icons.getMenuPanelIconList();
ArrayList<String> menuText = app.Languages.Active.getMenuPanelItemNames();
listview_menu = (ListView) findViewById(R.id.listview_menu);
listview_menu.setBackgroundColor(Color.parseColor("#" + app.Theme.ThemeBackColor));



MenuPanelListViewAdapter menuPanelListViewAdapter = new MenuPanelListViewAdapter(ActivityMain.this, menuIcons, menuText);
listview_menu.setAdapter(menuPanelListViewAdapter);

    listview_menu.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> adpterView, View view, int position,
                                long id) {
            for (int i = 0; i < listview_menu.getChildCount(); i++) {
                if(position == i ){
                    listview_menu.getChildAt(i).setBackgroundColor(Color.BLUE);
                }else{
                    listview_menu.getChildAt(i).setBackgroundColor(Color.TRANSPARENT);
                }
            }
        }
    });

这是我的mainactivity.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:fresco="http://schemas.android.com/apk/res-auto"
    android:id="@+id/relativelayout_base"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:id="@+id/relativelayout_toolbar"
        android:layout_width="match_parent"
        android:layout_height="?android:actionBarSize">

        <LinearLayout
            android:id="@+id/linearlayout_menu"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
            android:clickable="true"
            android:gravity="center"
            android:paddingLeft="10dp"
            android:paddingRight="10dp">

            <ImageView
                android:id="@+id/imageview_menu"
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:src="@drawable/menushow_g" />
        </LinearLayout>


        <com.hotelstayapp.android.custom.CustomTextView
            android:id="@+id/customtextview_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@+id/linearlayout_menu"
            android:textColor="#FFFFFF"
            android:textSize="27sp"
            android:ellipsize="end"
            android:singleLine="true"
            android:paddingBottom="1dp"/>
    </RelativeLayout>

    <android.support.v4.widget.DrawerLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        tools:context="com.hotelstayapp.android.UI.ActivityMain"
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/relativelayout_toolbar">

        <FrameLayout
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        </FrameLayout>

        <RelativeLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/relativelayout_menupanel"
            android:layout_width="280dp"
            android:layout_height="match_parent"
            android:layout_gravity="left">

            <RelativeLayout
                android:id="@+id/relativelayout_menuheader"
                android:layout_width="match_parent"
                android:layout_height="130dp"
                android:clickable="true">


                <com.facebook.drawee.view.SimpleDraweeView
                    fresco:actualImageScaleType="centerCrop"
                    android:id="@+id/imageview_menubgimage"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:adjustViewBounds="true"
                    android:scaleType="centerCrop"

                    />


                <com.facebook.drawee.view.SimpleDraweeView
                    android:layout_width="240dp"
                    android:layout_height="70dp"
                    fresco:actualImageScaleType="fitCenter"
                    android:id="@+id/imageview_menulogo"
                    android:layout_centerInParent="true"
                    />
            </RelativeLayout>

            <ListView
                android:id="@+id/listview_menu"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@id/relativelayout_menuheader"
                android:layout_gravity="right"
                android:divider="@android:color/transparent"
                android:dividerHeight="0dp"
                android:paddingTop="10dp"
                android:smoothScrollbar="true"
                android:focusable="false"
                android:focusableInTouchMode="false">
            </ListView>
        </RelativeLayout>

    </android.support.v4.widget.DrawerLayout>

</RelativeLayout>

这是我的ListView项目布局:

<?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="wrap_content"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="1"
        android:paddingBottom="14dp"
        android:paddingTop="14dp"
        android:paddingLeft="0dp">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            android:layout_weight="0.8"
            android:gravity="center">
            <ImageView
                android:layout_width="23dp"
                android:layout_height="23dp"
                android:id="@+id/imageViewIcon"
                android:layout_centerVertical="true"
                android:layout_alignParentLeft="true"
                android:src="@drawable/menuhome_g"
                android:layout_centerHorizontal="true" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            android:layout_weight="0.2"
            android:paddingLeft="15dp"
            android:gravity="center_vertical">
            <com.hotelstayapp.android.custom.CustomTextView
                android:id="@+id/textViewMenuItemName"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center|left"
                android:text="Home"
                android:textColor="#000000"
                android:textSize="15sp"
                android:textStyle="bold"/>
        </LinearLayout>
    </LinearLayout>
    <View
        android:id="@+id/viewMenuDivider"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#eeeeee"

        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"></View>

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

首先将所有项目的背景更改为transperent,然后将点击后的视图背景设置为您想要的颜色,如下所示。

MenuPanelListViewAdapter menuPanelListViewAdapter = new MenuPanelListViewAdapter(ActivityMain.this, menuIcons, menuText);
        listview_menu.setAdapter(menuPanelListViewAdapter);

        listview_menu.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> adpterView, View view, int position,
                                    long id) {
                for (int i = 0; i < listview_menu.getChildCount(); i++) {
                    listview_menu.getChildAt(i).setBackgroundColor(Color.TRANSPARENT);
                }
                view.setBackgroundColor(Color.BLUE);
                menuPanelListViewAdapter.notifyDataSetChanged(); // this is importent
            }
        });