[英]Android studio java - Showing bottom sheet behavior in a map fragment inside a view pager
我有一個由具有 3 個片段的頁面查看器組成的應用程序。 此片段之一是 map,其想法是在 map 標記單擊上顯示底部表行為。
這是我的布局:
<?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期間我設置了兩個視圖尋呼機,map和底頁
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 通過調用 aa 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);
我啟動應用程序,單擊標記,在登錄日志中我可以看到正確的 state(我正在打印它,就像您從調用中看到的那樣),但絕對無法在屏幕上看到它。 我沒有什么想法,因為沒有錯誤,我不知道如何移動。
有人可以讓我朝着正確的方向前進嗎?
非常感謝!
您可以嘗試構建自己的 BottomSheetDialogFragment 並使用
val bottomSheetDialogFragment = MytBottomSheetDialogFragment.newInstance()
//show it
bottomSheetDialogFragment.show(fragmentManager!!, bottomSheetDialogFragment.tag)
因此,您不必在布局文件中創建視圖,並且可以簡單地重用它。
更新:
您可以在 setupDialog function 中設置布局。 只需覆蓋它並膨脹您的自定義 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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.