Moe*_*eez 5 android android-linearlayout android-view
我在单击按钮时添加布局。
private void addLayout() {
layout2 = LayoutInflater.from(mContext).inflate(R.layout.product_layout, mLinearLayout, false);
productAuto = (AutoCompleteTextView) layout2.findViewById(R.id.tv_product);
qtyEditText = (EditText) layout2.findViewById(R.id.prod_qty);
prodPriceEditText = (EditText)layout2.findViewById(R.id.prod_price);
prodSpecsEditText = (EditText)layout2.findViewById(R.id.prod_specs);
removeProduct = (Button)layout2.findViewById(R.id.btn_rmv);
mLinearLayout.addView(layout2);
setProd();
this.initListenersPrd(getActivity());
}
private void initListenersPrd(final Context context) {
productAuto.setOnItemClickListener((parent, view, position, id) -> {
String newName = parent.getItemAtPosition(position).toString();
ProductDetail productDetail = mProductManager.getProductByPrdName(newName);
if (productDetail != null) {
this.prodPriceEditText.setText(productDetail.getPrice());
this.prodSpecsEditText.setText(productDetail.getProductSpec());
}
});
removeProduct.setOnClickListener(v -> {
if (mLinearLayout.getChildCount()>0)
{
((LinearLayout)mLinearLayout.getParent()).removeView(mLinearLayout);
}
});}
Run Code Online (Sandbox Code Playgroud)
现在我想从应用程序中删除特定的产品表单,因此我已((LinearLayout)mLinearLayout.getParent()).removeView(mLinearLayout);单击“删除”按钮。但它删除了动态添加的整个布局。
图形用户界面
正如您在上图中看到的,我添加了 4-5 个产品布局,但是当我单击“删除”按钮时,我删除了所有布局。
更新1
我在主布局中添加了以下布局,然后以编程方式调用其中的产品布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll_prod"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
产品布局是单独创建的,我在主布局中调用它。
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2014 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/ll_out"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/background_round"
android:orientation="vertical"
android:padding="5sp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10sp"
android:orientation="horizontal">
<AutoCompleteTextView
android:id="@+id/tv_product"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="left|center_vertical"
android:gravity="left"
android:hint="Enter Product"
android:inputType="text" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10sp"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:orientation="vertical">
<EditText
android:id="@+id/prod_qty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:editable="false"
android:focusable="true"
android:focusableInTouchMode="true"
android:hint="Enter Quantity"
android:gravity="left"
android:inputType="number" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:orientation="vertical">
<EditText
android:id="@+id/prod_price"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:editable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:hint="Prod Price"
android:gravity="left"
android:inputType="none" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:orientation="vertical">
<EditText
android:id="@+id/prod_specs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:editable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:gravity="left"
android:hint="Prod Specs"
android:inputType="none" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp"
android:layout_marginTop="1dp"
android:padding="0dp">
<Button
android:id="@+id/btn_rmv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Remove Product"
android:textColor="@color/white" />
</LinearLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
每当我点击 时就会调用上面的布局Add New Product Button。
如何删除特定布局?
任何帮助将不胜感激。
在这些情况下,将 RecyclerView 与 Adapter 结合使用是您的最佳选择。我的工作代码如下
类列表适配器
package com.akshita.recycler;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class listAdapter extends RecyclerView.Adapter<listAdapter.ViewHolder> {
private ArrayList<String> product_name;
private ArrayList<Integer> quantity;
private ArrayList<Double> price;
private ArrayList<String> specs;
private static Context scontext;
public listAdapter(Context context)
{
scontext = context;
this.product_name = new ArrayList<String>();
this.price = new ArrayList<Double>();
this.quantity = new ArrayList<Integer>();
this.specs = new ArrayList<String>();
}
public listAdapter(Context context, ArrayList<String> pdname, ArrayList<Integer> quantity, ArrayList<Double> price, ArrayList<String> specs)
{
this.product_name = pdname;
this.price = price;
this.quantity = quantity;
this.specs = specs;
scontext = context;
}
@NonNull
@Override
public listAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View listItem = layoutInflater.inflate(R.layout.productview,parent,false);
ViewHolder viewHolder = new ViewHolder(listItem);
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull listAdapter.ViewHolder holder, int position) {
holder.actv.setText(product_name.get(position));
holder.qty.setText(quantity.get(position).toString());
holder.price.setText(price.get(position).toString());
holder.desc.setText(specs.get(position));
}
public void addView(String pdname, Double prc, Integer quant, String spec ) {
product_name.add(pdname);
quantity.add(quant);
price.add(prc);
specs.add(spec);
notifyItemInserted(product_name.size());
notifyItemInserted(quantity.size());
notifyItemInserted(price.size());
notifyItemInserted(specs.size());
}
public void removeAt(int position) {
product_name.remove(position);
quantity.remove(position);
price.remove(position);
specs.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, product_name.size());
notifyItemRangeChanged(position, quantity.size());
notifyItemRangeChanged(position, price.size());
notifyItemRangeChanged(position, specs.size());
}
@Override
public int getItemCount() {
return product_name.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
public AutoCompleteTextView actv;
public EditText qty;
public EditText price;
public EditText desc;
public Button button;
public ViewHolder(View itemView) {
super(itemView);
this.actv = itemView.findViewById(R.id.tv_product);
this.qty = itemView.findViewById(R.id.prod_qty);
this.price = itemView.findViewById(R.id.prod_price);
this.desc = itemView.findViewById(R.id.prod_specs);
this.button = itemView.findViewById(R.id.btn_rmv);
button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(v.equals(button)){
removeAt(getAdapterPosition());
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的主课
MainActivity.java
package com.akshita.recycler;
import android.os.Bundle;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
listAdapter adapter;
RecyclerView hs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
hs = findViewById(R.id.rcview);
hs.setHasFixedSize(false);
hs.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
}
@Override
public void onClick(View v) {
switch(v.getId())
{
case R.id.add: addView(v);
}
}
public void addView(View v)
{
if(adapter == null) {
adapter = new listAdapter(this);
hs.setAdapter(adapter);
}
adapter.addView("Name",0.0, 0, "Specs");
}
}
Run Code Online (Sandbox Code Playgroud)
产品视图.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:orientation="vertical">
<LinearLayout
android:id="@+id/ll_out"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5sp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10sp"
android:orientation="horizontal">
<AutoCompleteTextView
android:id="@+id/tv_product"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="left|center_vertical"
android:gravity="left"
android:hint="Enter Product"
android:inputType="text" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10sp"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:orientation="vertical">
<EditText
android:id="@+id/prod_qty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:editable="false"
android:focusable="true"
android:focusableInTouchMode="true"
android:hint="Enter Quantity"
android:gravity="left"
android:inputType="number" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:orientation="vertical">
<EditText
android:id="@+id/prod_price"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:editable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:hint="Prod Price"
android:gravity="left"
android:inputType="none" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:orientation="vertical">
<EditText
android:id="@+id/prod_specs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:editable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:gravity="left"
android:hint="Prod Specs"
android:inputType="none" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp"
android:layout_marginTop="1dp"
android:padding="0dp">
<Button
android:id="@+id/btn_rmv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Remove Product"
android:textColor="@color/white" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
活动主文件
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:gravity="center|top"
android:orientation="vertical">
<Button
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="addView"
/>
<androidx.recyclerview.widget.RecyclerView
android:layout_marginTop="10dp"
android:id="@+id/rcview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
完整的工作代码可以在 GitHub 上找到
| 归档时间: |
|
| 查看次数: |
757 次 |
| 最近记录: |