BottomSheetDialog setOnClickListener未触发
问题描述:
我遇到的问题触发BottomSheetDialog菜单的setOnClickListener
的onClick
方法。BottomSheetDialog setOnClickListener未触发
这是XML片段布局或用于Fragment
的BottomSheetDialog
<?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;
}
}
我在做什么错?
答
用途:公共类GroceryItemsMenu扩展片段实现View.OnClickListener
+0
我添加了实现View.OnClickListener并实现了public void onClick(View v)。然后我做了一个开关R.id.LayoutName但它没有工作 – wodubayo
尝试删除线的应用:layout_behavior =“android.support.design.widget.BottomSheetBehavior” –
这似乎不起作用 – wodubayo
您可以发布代码的活动? –