2017-08-08 115 views
3

我遇到的問題觸發BottomSheetDialog菜單的setOnClickListeneronClick方法。BottomSheetDialog setOnClickListener未觸發

這是XML片段佈局或用於FragmentBottomSheetDialog

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/fragment_grocery_menu_bottom" 
    android:layout_width="match_parent" 
    android:layout_height="180dp" 
    android:background="#ffffff" 
    android:orientation="vertical" 
    app:layout_behavior="android.support.design.widget.BottomSheetBehavior"> 

    <LinearLayout 
     android:id="@+id/fragment_grocery_bottom_sheet_search" 
     android:layout_width="match_parent" 
     android:layout_height="60dp" 
     android:clickable="true" 
     android:descendantFocusability="blocksDescendants" 
     android:focusable="true" 
     android:foreground="?android:attr/selectableItemBackground" 
     android:orientation="horizontal" 
     android:padding="16dp"> 

     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:clickable="false" 
      android:focusable="false" 
      android:focusableInTouchMode="false" 
      app:srcCompat="@drawable/ic_search_black_24dp" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:layout_marginLeft="16dp" 
      android:clickable="false" 
      android:focusable="false" 
      android:focusableInTouchMode="false" 
      android:text="Search Item" /> 
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/fragment_grocery_bottom_sheet_got" 
     android:layout_width="match_parent" 
     android:layout_height="60dp" 
     android:clickable="true" 
     android:descendantFocusability="blocksDescendants" 
     android:focusable="true" 
     android:foreground="?android:attr/selectableItemBackground" 
     android:orientation="horizontal" 
     android:padding="16dp"> 

     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:clickable="false" 
      android:focusable="false" 
      android:focusableInTouchMode="false" 
      app:srcCompat="@drawable/ic_done_black_24dp" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:layout_marginLeft="16dp" 
      android:clickable="false" 
      android:focusable="false" 
      android:focusableInTouchMode="false" 
      android:text="Mark Item as Gotten" /> 
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/fragment_grocery_bottom_sheet_delete" 
     android:layout_width="match_parent" 
     android:layout_height="60dp" 
     android:clickable="true" 
     android:descendantFocusability="blocksDescendants" 
     android:focusable="true" 
     android:foreground="?android:attr/selectableItemBackground" 
     android:orientation="horizontal" 
     android:padding="16dp"> 

     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:clickable="false" 
      android:focusable="false" 
      android:focusableInTouchMode="false" 
      app:srcCompat="@drawable/ic_delete_black_24dp" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:layout_marginLeft="16dp" 
      android:clickable="false" 
      android:focusable="false" 
      android:focusableInTouchMode="false" 
      android:text="Remove Item" /> 
    </LinearLayout> 
</LinearLayout 

的Java類的代碼如下。因此,舉例來說,如果我點擊onClick方法不開的GOT菜單(LinearLayout中)上的日誌不寫也SnackBar不顯示

import android.os.Bundle; 
import android.app.Fragment; 
import android.support.design.widget.Snackbar; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.LinearLayout; 


public class GroceryItemsMenu extends Fragment { 


    public GroceryItemsMenu() { 
     // Required empty public constructor 
    } 


    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 

     // Inflate the layout for this fragment 
     final View view = inflater.inflate(R.layout.fragment_grocery_items_menu, container, false); 

     LinearLayout delete = (LinearLayout) view.findViewById(R.id.fragment_grocery_bottom_sheet_delete); 
     LinearLayout got = (LinearLayout) view.findViewById(R.id.fragment_grocery_bottom_sheet_got); 
     LinearLayout search = (LinearLayout) view.findViewById(R.id.fragment_grocery_bottom_sheet_search); 

     search.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

      } 
     }); 

     got.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       Log.i("Item: ", "Got"); 
       Snackbar.make(getActivity().findViewById(R.id.groceryItemsCoordinatorLayout), "Got this Item", Snackbar.LENGTH_SHORT) 
         .setAction("Undo", new View.OnClickListener() { 
          @Override 
          public void onClick(View v) { 

          } 
         }).show(); 
      } 
     }); 

     delete.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       // Delete code here; 
      } 
     }); 

     return view; 
    } 
} 

我在做什麼錯?

+0

嘗試刪除線的應用:layout_behavior =「android.support.design.widget.BottomSheetBehavior」 –

+0

這似乎不起作用 – wodubayo

+0

您可以發佈代碼的活動? –

回答

0

用途:公共類GroceryItemsMenu擴展片段實現View.OnClickListener

+0

我添加了實現View.OnClickListener並實現了public void onClick(View v)。然後我做了一個開關R.id.LayoutName但它沒有工作 – wodubayo