【发布时间】: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