[英]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.