滑动导航抽屉无法处理对Android菜单项的点击 [英] Sliding navigation drawer not handling clicks on menu items android

查看:73
本文介绍了滑动导航抽屉无法处理对Android菜单项的点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在应用程序中使用滑动导航抽屉.但是,它似乎没有处理OnItemClick方法.当我单击某个项目时,什么都没有发生.如何使它正常工作?谢谢

I am trying to use the sliding navigation drawer in the app. However it seems that it's not handling the OnItemClick method. When I click on an item nothing happens.How can I make it work?Thanks

//MainActivity

//MainActivity

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    drawer_layout = (DrawerLayout)findViewById(R.id.drawerLayout);
    list_view = (ListView)findViewById(R.id.drawerList);
    menu_items = getResources().getStringArray(R.array.nav_drawer_items);
    list_view.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, menu_items));
    list_view.setOnItemClickListener(this);

}

@Override
 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    // TODO Auto-generated method stub



        Toast.makeText(MainActivity.this, menu_items[position] + " was selected", Toast.LENGTH_LONG).show();

}

//MainActivity.xml

// MainActivity.xml

 <android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
    android:id="@+id/mainContent"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<ListView android:id="@+id/drawerList"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="left"
    android:background="@android:color/white"
    android:divider="@android:color/black"
    android:dividerHeight="1dp"
    android:listSelector="@android:color/black"
    android:choiceMode="singleChoice"
    /> 

<LinearLayout 
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:ignore="MergeRootFrame"
tools:context="com.example.MainActivity"
 >



<TextView
    android:id="@+id/tv1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/textview1"
    android:layout_gravity="center"
    android:textStyle="bold"
    android:textSize="20sp"
    android:gravity="center"
    android:layout_marginTop="30dp"/>


<Button 
  android:id="@+id/Button1"
  android:layout_width="100dp"
  android:layout_height="100dp"
  android:layout_gravity="center"
  android:background="@drawable/ok_button"
  android:layout_marginTop="20dp"/>

</LinearLayout>

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

推荐答案

您的DrawerLayout的子View的Z顺序不正确.抽屉的View(在本例中为ListView)需要最后列出,以便位于其他所有内容的顶部.

Your DrawerLayout's child Views are incorrectly z-ordered. The View for the drawer - in this case, the ListView - needs to be listed last in order to be on top of everything else.

这篇关于滑动导航抽屉无法处理对Android菜单项的点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆