【问题标题】:Android studio java - Showing bottom sheet behavior in a map fragment inside a view pagerAndroid studio java - 在视图寻呼机内的地图片段中显示底部工作表行为
【发布时间】:2025-12-15 09:15:02
【问题描述】:

我有一个由具有 3 个片段的页面查看器组成的应用程序。 该片段之一是地图,其想法是在地图标记单击时显示底部表行为。

这是我的布局:

<?xml version="1.0" encoding="utf-8"?>

<androidx.coordinatorlayout.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.myapp.android.activities.MainActivity"
    android:nestedScrollingEnabled="true">



    <RelativeLayout
        android:id="@+id/rlMap"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        tools:layout="@layout/content_main"
        />


    <androidx.core.widget.NestedScrollView
        android:id="@+id/bottom_sheet"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:background="@android:color/white"
        app:behavior_hideable="true"
        app:behavior_peekHeight="155dp"
        app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">

        <TextView
            android:id="@+id/bottom_sheet_text"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:padding="16dp"
            android:textSize="16sp" />

    </androidx.core.widget.NestedScrollView>

</androidx.coordinatorlayout.widget.CoordinatorLayout>


然后...在 oncreate 期间,我设置了视图寻呼机、地图和底页

super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }

        bottomSheetText = findViewById(R.id.bottom_sheet_text);
        View bottomSheet = findViewById(R.id.bottom_sheet);
        mBottomSheetBehaviour = BottomSheetBehavior.from(bottomSheet);
        mBottomSheetBehaviour.setState(BottomSheetBehavior.STATE_HIDDEN);

        ViewPager viewPager = findViewById(R.id.viewpager);
        setupViewPager(viewPager);

        TabLayout tabLayout = findViewById(R.id.tablayout);
        tabLayout.setupWithViewPager(viewPager);

viewpager 和 map 通过调用 a func

 private void setupViewPager(ViewPager viewPager) {
        Log.d(TAG, "setupViewPager");
        ViewPagerAdapter adapter = new ViewPagerAdapter
                (getSupportFragmentManager());
        createUpMap();
        adapter.addFragment(mapFrag, "Map");
        //TEMP
        adapter.addFragment(new SupportMapFragment(), "List");
        adapter.addFragment(new SupportMapFragment(), "Session");
        viewPager.setAdapter(adapter);


private void createUpMap() {
        Log.d(TAG, "createUpMap");
        if (mapFrag == null) {
            Log.d(TAG, "mapFrag is null, creation of map...");
            mapFrag = (SupportMapFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.rlMap);
            mapFrag = new SupportMapFragment();

最后,我跳过标记点击以显示底页:

 mMap.setOnMarkerClickListener(marker -> {

            Log.e(TAG, "POINT HAS BEEEN CLICKED status: "  + mBottomSheetBehaviour.getState());
            if (mBottomSheetBehaviour.getState() == BottomSheetBehavior.STATE_EXPANDED) {

                bottomSheetText.setText(marker.getTag().toString());
                mBottomSheetBehaviour.setPeekHeight(200);
                mBottomSheetBehaviour.setState(BottomSheetBehavior.STATE_EXPANDED);

我启动应用程序,单击标记,然后在登录日志中我可以看到正确的状态(我正在打印它,就像您从调用中看到的那样),但绝对无法在屏幕上看到它。我没有什么想法,因为没有错误,我不知道如何移动。

有人可以让我朝着正确的方向前进吗?

非常感谢!

【问题讨论】:

    标签: java android android-fragments android-mapview bottom-sheet


    【解决方案1】:

    您可以尝试构建自己的 BottomSheetDialogFragment 并显示它

    val bottomSheetDialogFragment = MytBottomSheetDialogFragment.newInstance()
    //show it
    bottomSheetDialogFragment.show(fragmentManager!!, bottomSheetDialogFragment.tag)
    

    因此您不必在布局文件中创建视图并且可以简单地重复使用它。

    更新:

    您可以在 setupDialog 函数中设置布局。只需覆盖它并扩展您的自定义 BottomSheetLayout。

    public void setupDialog(Dialog dialog, int style) {
        super.setupDialog(dialog, style);
    
        View contentView = View.inflate(getContext(), R.layout.custom_layout, null);
        dialog.setContentView(contentView);
    }
    

    【讨论】:

    • 你好,我要试试看是否可行,问题是实际上我需要在其中有一个特定的布局
    • 感谢您的回答,我看到这是 btw bottomsheetdialogfragment,我使用的是bottosheetbehaviuor 而不是只有 setstate 方法的人。这怎么能重新适应?
    • 也许这个答案对你有帮助:*.com/a/35976745/7454336
    • 再次感谢,不幸的是我找不到在片段中显示的方法......真的很无聊! :(
    最近更新 更多