java - Android studio java - 在 View 寻呼机内的 map fragment 中显示 Bottom Sheet 行为

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

我有一个由具有 3 个 fragment 的页面查看器组成的应用程序。 这个 fragment 之一是一张 map ,其想法是在点击 map 标记时显示一个 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 期间我设置了 view pager 、 map 和 bottom sheet

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 通过调用一个 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();

最后,我通过标记单击以显示 Bottom Sheet :

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

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

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

非常感谢!

最佳答案

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

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

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

更新:

您可以在 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);
}

关于java - Android studio java - 在 View 寻呼机内的 map fragment 中显示 Bottom Sheet 行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58334306/

相关文章:

android - 带有 intentfilter 的 fragment

java - 使用 JDBC 检查表是否存在

java - 查找链表中的多个链接

java - 选择随机 Java 变量?这可能吗

javascript - 如何格式化适合宽度受限设备(如 iPhone)的长 <pre> HTML block

java - fragment_main.xml和activity_main.xml之间的区别

android - 如何在android中使用动态 fragment ?

java - 在java中组合比较器

Android 开发生命周期结束的应用程序

android - 网格布局与。表格布局