gpt4 book ai didi

java - 如何从底部工作表 fragment 启动 Activity

转载 作者:行者123 更新时间:2023-12-01 19:54:56 24 4
gpt4 key购买 nike

我刚刚开始使用 Android 版 bottomSheetDialog。我已经能够正确显示 bottomSheet 但我需要通过单击任何项​​目来启动 Activity 。我该怎么办?

MainFragment.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;
}

}
}

最佳答案

您是否尝试从您的 View 中获取上下文?

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;
}

}
}

关于java - 如何从底部工作表 fragment 启动 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59046648/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com