单击DrawerLayout原因单击主布局

时间:2018-09-30 19:36:46

标签: android drawerlayout

我具有DrawerLayout活动,我实现了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>

1 个答案:

答案 0 :(得分:0)

每种情况下,您需要添加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;
     }