[英]How to start activity from bottom sheet fragment
我刚刚开始使用用于 android 的bottomSheetDialog
。 我已经能够正确显示bottomSheet
,但我需要通过单击任何项目来启动活动。 我该怎么做?
主片段.java
public class MainFragment extends Fragment {
private BottomSheetDialog bottomSheetDialog;
public SearchFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_search, container, false);
all.setOnClickListener(v -> {
//open bottom sheet from fragment
BottomSheetDialog bottomSheetDialog = new BottomSheetDialog();
bottomSheetDialog.show(Objects.requireNonNull(getActivity()).getSupportFragmentManager(), "bottomSheet");
});
return view;
}
BottomSheetDialogFrament.java
public class BottomSheetDialog extends BottomSheetDialogFragment {
private BottomSheetListener bottomSheetListener;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.bottom_sheet_layout, container, false);
LinearLayout mRestaurants = v.findViewById(R.id.restaurants);
mRestaurants.setOnClickListener(v1 -> {
//need to start activity from here
Intent intent = new Intent(getActivity(), MapActivity.class);
intent.putExtra("mapData", "Restaurants");
startActivity(intent);
});
return v;
}
}
}
您是否尝试从您的观点中获取上下文?
public class BottomSheetDialog extends BottomSheetDialogFragment {
private BottomSheetListener bottomSheetListener;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.bottom_sheet_layout, container, false);
LinearLayout mRestaurants = v.findViewById(R.id.restaurants);
mRestaurants.setOnClickListener(v1 -> {
//need to start activity from here
Intent intent = new Intent(getActivity(), MapActivity.class);
intent.putExtra("mapData", "Restaurants");
v1.getContext().startActivity(intent);
});
return v;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.