首页 > 解决方案 > 如何将 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。

标签: listview

解决方案


推荐阅读