我在 Android Studio (Java) 中的底部向上滑动布局有问题。我会尽力描述我的问题。 好吧。我用它的相关 Java 类构建了 Bottom slide up 布局。在谷歌地图活动中,我有标记 onclicklistener 功能。当我点击标记底部的向上滑动布局显示时,但问题是。它只显示布局的前端(我已经在 .xml 文件中拥有的内容)但它不会加载或执行与该 .xml 向上滑动布局相关的 Java 类。
这是唯一的问题。单击谷歌地图标记时会显示向上滑动布局。主要和一般问题是。它不执行与 java 类(tools:context=".java_class")函数(上下文内容)相关的向上滑动布局。我不知道问题出在哪里。
下面是我的底部工作表布局的代码,它是向上滑动布局设计内容。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:id="@+id/bottomSheetContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:behavior_peekHeight="80dp"
tools:context=".Bottom_sheet_layout">
<RelativeLayout
android:id="@+id/userTopInfo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:paddingBottom="100dp">
</RelativeLayout>
<LinearLayout
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:layout_marginTop="-100dp">
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="150dp"
android:padding="3dp"
tools:ignore="ContentDescription" />
</LinearLayout>
<RelativeLayout
android:id="@+id/TopInfo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bottom_sheet_background"
android:layout_marginTop="-10dp"
android:paddingBottom="10dp">
<LinearLayout
android:id="@id/linearLayoutID"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:id="@+id/dbText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-light"
android:padding="5dp"
android:text="Brand"
android:textColor="#000000"
android:textSize="24sp" />
<TextView
android:id="@+id/dbText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-light"
android:padding="5dp"
android:text="text from Database"
android:textColor="#000000"
android:textSize="24sp" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>
下面是一个地图活动,其中谷歌地图带有标记 onClickListener 功能。
googleMap.setOnMarkerClickListener(marker1 -> {
//marker1.hideInfoWindow();
Geocoder geocoder = new Geocoder(MapsActivity.this,Locale.getDefault());
try {
addresses = geocoder.getFromLocation(marker1.getPosition().latitude,marker1.getPosition().longitude, 1);
} catch (IOException e) {
e.printStackTrace();
}
String address = addresses.get(0).getAddressLine(0);
//String city = addresses.get(0).getLocality();
Toast.makeText(MapsActivity.this, "Marker onclicked. Slideup shows.", Toast.LENGTH_SHORT).show();
// Down below is the code which takes care of bottom layout slide up functionality. Of course this
// code works it does show slide up layout mentioned, but that layout does NOT do any related java
// context functions.
BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(MapsActivity.this, R.style.BottomSheetDialogTheme);
View bottomSheetView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.layout_bottom_sheet, findViewById(R.id.bottomSheetContainer));
bottomSheetDialog.setContentView(bottomSheetView);
bottomSheetDialog.show();
return true;
});
就是这个。这是应该工作但不工作的代码。我的意思是 slideup it self.. 是的,它在布局中向上滑动。但是没有执行任何java类函数,甚至在布局向上滑动后也没有执行onCreate()。我已经测试过向 onCreate() 添加一些简单的 Toast() 消息以确保它已加载或未加载。不! Toast 没有出现,这意味着 onCreate() 和任何其他 public void function() 都没有执行。 PS - 我确实为这个向上滑动布局类使用了“绑定”。如果这很重要,那么用于数据设置和获取的 google firebase 功能很少。
任何帮助表示赞赏。 谢谢!
tools:context
属性只是提示该视图属于哪个类。它什么都不做。
您应该创建自己的
BottomSheetDialog
并在onCreateView
方法中设置此布局。关于bottom sheet的文章很多,比如看看这篇。