listview - 如何将 listView 添加到另一个布局
问题描述
我创建了一个bottomSheet 菜单,该菜单在按钮单击时来自屏幕底部。这是我的 XML 文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/bottomSheetContainer"
android:background="@drawable/bottom_sheet_background"
android:orientation="vertical">
<TextView
android:id="@+id/Title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="15dp"
android:text="My Shopping List"
android:textColor="@color/colorPrimary"
android:textSize="15sp"
android:textStyle="bold"
/>
<ImageView
android:id="@+id/Share"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/Title"
android:src="@drawable/ic_baseline_share_24"
android:padding="15dp"
/>
<ListView
android:id="@+id/Listview"
android:layout_below="@+id/Share"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:overScrollMode="never" />
</RelativeLayout>
我正在尝试在其中添加一个 ListView ,如下所示:
edit_profile.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String btn = edit_profile.getText().toString();
if(btn.equals("Shopping List"))
{
// MyShoppingList.setVisibility(View.VISIBLE);
BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(
ProfileActivity.this, R.style.BottomSheetDialogTheme
);
View bottomSheetView = LayoutInflater.from(getApplicationContext())
.inflate(R.layout.layout_shopping_list,
(LinearLayout)findViewById(R.id.bottomSheetContainer)
);
bottomSheetView.findViewById(R.id.Share).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT,"Noting To Share");
sendIntent.setType("text/plain");
Intent.createChooser(sendIntent,"Share via");
startActivity(sendIntent);
}
});
bottomSheetDialog.setContentView(bottomSheetView);
bottomSheetDialog.show();
}
final ArrayAdapter<String> myArrayAdapter = new ArrayAdapter<String>(ProfileActivity.this, android.R.layout.simple_list_item_1 , myArrayList);
ListView = findViewById(R.id.Listview);
ListView.setAdapter(myArrayAdapter);
// Collections.reverse(myArrayList);
firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
mRef= FirebaseDatabase.getInstance().getReference("ShoppingList").child(firebaseUser.getUid());
mRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
/* Map<String, String> map = (Map<String, String>) snapshot.getValue();
// HashMap value = snapshot.getValue(HashMap.class);
myArrayList.add(""+map);
myArrayAdapter.notifyDataSetChanged();
*/
String ingre = snapshot.child("Ingre").getValue(String.class);
int index = ingre.indexOf(":");
if(index != -1) {
ingre = ingre.substring(0, index);
myArrayList.add(ingre);
myArrayAdapter.notifyDataSetChanged();
}
}
我BottomSheet
在活动的 XML 文件中包含这样的:
<include
android:id="@+id/MyShoppingList"
android:visibility="gone"
layout="@layout/layout_shopping_list"
/>
但是当我单击按钮时,列表显示在bottomSheet
. 像这样
如您所见,列表在 之外,bottomSheet
我怎样才能将其放入其中?
更新:它终于奏效了,它是这样工作的:
listView = bottomSheetView.findViewById(R.id.Listview);
仅将 listView 分配给我的 bottomSheetView。
解决方案
推荐阅读
- android - 使用 Alarmmanger 的心跳不能使服务保持活力
- mysql - Spring boot multitenancy ,其中一个租户连接到多个模式
- javascript - Woocommerce 如何获取产品数据以在自定义模式中使用它们
- apache-camel - 骆驼在处理文件之前验证校验和
- python-3.x - 在 Python3 的另一个类中使用枚举实例
- vba - 公共类型不能在类模块中声明
- python - 迭代 python 对象是 2.7。显示未找到错误
- javascript - 被服务器重定向后反应处理表单提交(带有数据)?
- python - python logger的后代-好处
- java - Spring Boot(用户''@'localhost'拒绝访问数据库'myappdb')