简体   繁体   English

如何从底部工作表片段开始活动

[英]How to start activity from bottom sheet fragment

I just started working with bottomSheetDialog for android.我刚刚开始使用用于 android 的bottomSheetDialog I have been able to show the bottomSheet correctly but I need to start an activity from clicking on any of the items.我已经能够正确显示bottomSheet ,但我需要通过单击任何项​​目来启动活动。 How do I go about this?我该怎么做?

MainFragment.java主片段.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 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;
        }

        }
    }

did you try getting context from your 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;
        }

        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM