Listview上的点击事件不起作用

时间:2015-04-13 23:00:56

标签: android listview android-listview

我正在尝试在列表视图中添加单击侦听器,但单击时,不执行任何操作。你能帮我找到我做错的事吗? 这是根据android教程

中的一个示例改编的
public class Main extends Activity {
private ListView menuListView;
 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // Inflate the menu items for use in the action bar
        //getMenuInflater().inflate(R.menu.menu_main,menu);

        //Get the Menu List from resources
        menu = getResources().getStringArray(R.array.menu);
        //get the drawer Layout
        menuDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);

        ArrayAdapter<String> itemAdapter = new ArrayAdapter<String>(this,R.layout.custom_menu_list,menu);
        //get the list view
        menuListView = (ListView) findViewById(R.id.listview);
        menuListView.setAdapter(itemAdapter);
        menuListView.setOnItemClickListener(new menuItemClickListener());
}
private class menuItemClickListener implements ListView.OnItemClickListener{

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.d("Check","clicked");
        }
    }
}//
}

这是listview textview的xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:gravity="center_vertical"
    android:paddingLeft="6dip"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:focusable="false"
    android:focusableInTouchMode="false"
    />

这是主要的xml

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF">
    <!-- The main content view -->
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:descendantFocusability="blocksDescendants"/>
    <!-- The navigation drawer -->
    <ListView android:id="@+id/listview"
        android:layout_width="320dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="#000000"/>


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/toastMsg"
        android:text="Hello"/>

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

1 个答案:

答案 0 :(得分:1)

修改你的main.xml文件,删除TextView并运行代码。它的工作原理

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF">
    <!-- The main content view -->
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:descendantFocusability="blocksDescendants"/>
    <!-- The navigation drawer -->
    <ListView android:id="@+id/listview"
        android:layout_width="320dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"/>

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