首页 > 解决方案 > 单击 DrawerLayout 导致单击主布局

问题描述

我有带有 DrawerLayout 的 Activity,我实现了 Click ClickListener。问题是当 DrawerLayer 打开并且用户单击 darwerlayout 上的空白区域时,调用 OnClick 就好像他在主布局中单击了它后面的对象一样。我检查了 ID 是否出现在 onclick 中,它是抽屉后面的对象。

public class MainActivity extends AppCompatActivity implements
       android.view.View.OnClickListener{
     protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
         findViewById(R.id.resetThekir).setOnClickListener(this);
         // when User Clicks on the drawer that's already opened and above the image 
         //it calls the Onclick as if he clicks on the Image.
   }
   public void onClick(View v) {
        switch (v.getId()) {
        case R.id.addThkir:
        case R.id.currentThkirTxt:
        case R.id.thkirCount:
     }
}

XML

 <?xml version="1.0" encoding="utf-8"?>
 <android.support.v4.widget.DrawerLayout 
 xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">

     <ImageView
            android:id="@+id/resetThkir"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="13dp"
            android:layout_weight="1"
            app:srcCompat="@drawable/reset"
            android:contentDescription="@string/todo" />
</RelativeLayout>

<!-- The navigation drawer -->
<ListView android:id="@+id/left_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:entries="@array/menuitems"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    android:background="#CCCCCC"/>
 </android.support.v4.widget.DrawerLayout>

标签: androiddrawerlayout

解决方案


您需要break在每个案例之后添加:

   public void onClick(View v) {
        switch (v.getId()) {
        case R.id.addThkir: addThkir() break;
        case R.id.currentThkirTxt: currentThikTxt() break;
        case R.id.thkirCount: thkirCount() break;
     }

推荐阅读