Android studio java-在视图分页器内的地图片段中显示底页行为

问题描述 投票:0回答:1

我有一个由页面查看器组成的应用程序,包含3个片段。该片段之一是地图,其想法是在地图标记单击上显示bottosheetbehaviuor。

这是我的布局:

<?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和通过对函数的调用进行映射

 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个回答
0
投票

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

val bottomSheetDialogFragment = MytBottomSheetDialogFragment.newInstance()
//show it
bottomSheetDialogFragment.show(fragmentManager!!, bottomSheetDialogFragment.tag)
© www.soinside.com 2019 - 2024. All rights reserved.