簡體   English   中英

如何從底部工作表片段開始活動

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

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM