繁体   English   中英

在 ListView 中动态添加 LinearLayout

[英]Adding LinearLayout inside a ListView dynamically

我创建了一个片段 OrderFragment,我在其中创建了两个选项卡,Order_Subscription 和 Order_OneTime,每个选项卡也是一个片段。 我在两个片段中使用了一个 listView。

<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/listViewSubscription">
</ListView>

我想以 row_order.xml 的格式填充列表视图,其中包含客户详细信息和他们购买的产品(下面的 LinearLayout,id =“product_details”)。 产品的数量可以变化,所以我想动态添加线性布局。

row_order.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:id="@+id/row_card"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">


        <TextView
            android:id="@+id/customer_name"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="5"
            android:text="Customer Name"
            android:textStyle="bold">

        </TextView>

        <TextView
            android:id="@+id/address_phoneno"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="5"
            android:text="Phone Number"
            android:textStyle="bold">

        </TextView>

        <TextView
            android:id="@+id/amount_left"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="5"
            android:text="Amount Left"
            android:textStyle="bold">

        </TextView>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingLeft="20sp"
        android:paddingRight="10sp"
        android:id="@+id/product_details">

        <TextView
            android:id="@+id/product_name"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="5"
            android:gravity="center_vertical"
            android:text="Product Name">

        </TextView>

        <TextView
            android:id="@+id/quantity_order"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="5"
            android:gravity="center_vertical"
            android:text="Qty">

        </TextView>

        <TextView
            android:id="@+id/amount_order"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="5"
            android:gravity="center_vertical"
            android:text="Amount">

        </TextView>

        <CheckBox
            android:id="@+id/delivered_checkbox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center_vertical">

        </CheckBox>
    </LinearLayout>



</LinearLayout>

在Order_Subscription.java Fragment中,我的getView function如下。

public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
            //Following is for Customer Details. This works
            LayoutInflater layoutInflater = (LayoutInflater) getActivity().getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View row_order = layoutInflater.inflate(R.layout.row_order, parent, false);
            TextView cust_name = row_order.findViewById(R.id.customer_name);
            TextView ph_no_addr = row_order.findViewById(R.id.address_phoneno);
            TextView amnt = row_order.findViewById(R.id.amount_left);
            cust_name.setText(customerName[position]);
            ph_no_addr.setText(ph_no[position]);


            //Product details I want to add. Right now adding without loop
           LinearLayout linearLayout = (LinearLayout)row_order.findViewById(R.id.product_details);
           linearLayout.removeAllViews();
           View child = getLayoutInflater().inflate(R.id.product_details, null);
            TextView prod_name = linearLayout.findViewById(R.id.product_name);
            prod_name.setText("New Product");
            TextView quat_order = linearLayout.findViewById(R.id.quantity_order);
            TextView amnt_order = linearLayout.findViewById(R.id.amount_order);
            quat_order.setText("50");
            amnt_order.setText("100");
            linearLayout.addView(child);




            return row_order;
        }
    }

我想动态添加产品详细信息。 由于 XML,默认情况下有一组 LinearLayout(product_details)。RemoveAllViews() 能够清除它。 如果我不使用 removeAllView function,我可以设置该布局的文本。

我已经检查过: android: listview in listview我无法弄清楚哪里出错了。

强烈建议您应该使用ReceyclerView ,如果您不想,那么至少将ListAdapterListView一起使用,这样它会为您处理添加绑定和删除。

这是一个 例子

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM