【问题标题】:how to programmatically add clickable item in a scrollview?如何以编程方式在滚动视图中添加可点击项目?
【发布时间】:2022-11-09 22:21:43
【问题描述】:

我会尽力说清楚。

我正在尝试创建一个项目的滚动视图(假设它是一家商店),我(用户)自己通过应用程序的界面添加它,然后可以通过在滚动视图中单击它来进行修改。 例如,我的主页包含一个按钮和项目列表。当我点击它时,它会打开一个对话框,询问我要添加的项目的信息。完成配置项目后,我回到主页,可以看到刚刚添加的项目,如果需要,我可以单击它进行修改。

我在这里遇到的问题是,在滚动视图中我们必须添加视图。即使我知道如何通过 java 来完成,我如何为每个新项目添加一个 clicklistener ?考虑到我只能设置 int ids 的事实,如何为每个新视图(项目)设置 id?等等

有人知道我尝试做的任何方法吗?为了清楚起见,我将在这里制作一个非常简单的代码和界面截图示例。

我的 XML 主页:“activity_main.xml”

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <Button
            android:id="@+id/popUpAddItem"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Add Your Item"
            tools:ignore="MissingConstraints">
        </Button>
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/scrollViewItems">
            <!-- this LinearLayout is an exemple of the shape/structure of an item -->
            <LinearLayout
                android:clickable="true"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:id="@+id/protoItem">
                <TextView
                    android:clickable="true"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text=" Item1 "
                    android:id="@+id/protoName">
                </TextView>
                <TextView
                    android:clickable="true"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text=" Qt1 "
                    android:id="@+id/protoQuantity">
                </TextView>
                <TextView
                    android:clickable="true"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text=" Price1 "
                    android:id="@+id/protoPrice">
                </TextView>
            </LinearLayout>
        </ScrollView>
    </LinearLayout>

    

</androidx.constraintlayout.widget.ConstraintLayout>

自定义弹出窗口的 XML:“dialog_popup.xml”

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:id="@+id/window"
        tools:ignore="MissingConstraints">
        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/itemName"
            android:hint="Enter the name of the item">
        </EditText>
        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/itemQuantity"
            android:hint="Enter the quantity of the item">
        </EditText>
        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/itemPrice"
            android:hint="Enter the price the item">
        </EditText>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Valider"
            android:id="@+id/validationButton">
        </Button>
    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

创建自定义弹出窗口的类:“CreateCustomPopUp.java

package com.example.myapplication;

import android.app.Activity;
import android.app.Dialog;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

public class CreateCustomPopUp extends Dialog {
    private LinearLayout page;
    private EditText name, quantity, price;
    private Button validation;
    private String varName="";
    private int varQt= 0;
    private float varPrice =0;
    public CreateCustomPopUp(Activity activity)
    {
        super(activity, androidx.appcompat.R.style.Theme_AppCompat_Dialog);
        setContentView(R.layout.dialog_popup);
        this.page = findViewById(R.id.window);
        this.name = findViewById(R.id.itemName);
        this.price = findViewById(R.id.itemPrice);
        this.quantity = findViewById(R.id.itemQuantity);
        this.validation = findViewById(R.id.validationButton);
        validation.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                varName=name.getText().toString();
                varQt=Integer.valueOf(quantity.getText().toString());
                varPrice=Float.valueOf(price.getText().toString());
                dismiss();
            }
        });

    }
    public void build()
    {
        show();
    }
    public int getQt(){
        return varQt;
    }
    public String getName(){
        return varName;
    }
    public float getPrice(){
        return varPrice;
    }
}

主要活动 Java:“MainActivity.java”

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;


public class MainActivity extends AppCompatActivity {
    public MainActivity activity;
    public String name ="";
    public int qt =0;
    public float price = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        this.activity=this;
        Button addItem = (Button) findViewById(R.id.popUpAddItem);
        addItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                CreateCustomPopUp popUp = new CreateCustomPopUp(activity);
                popUp.build();
                popUp.setOnDismissListener(new DialogInterface.OnDismissListener() {
                    @Override
                    public void onDismiss(DialogInterface dialogInterface) {
                        name = popUp.getName();
                        qt = popUp.getQt();
                        price = popUp.getPrice();
                        //Put/call here a function/class or whatever works that add this created item in the scrollview
                    }
                });
            }
        });
        LinearLayout prototypeItem = (LinearLayout) findViewById(R.id.protoItem);
        prototypeItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Another popup that I'm borred to create, but I think you see the idea...
            }
        });
        // and here is my issue : I cant reproduce this "set on click listener" for each item... because they dont already exist and I dont know how many I'll have !
    }
}

希望它很清楚,你可以帮助我^^

再见

【问题讨论】:

    标签: java android xml android-studio scrollview


    【解决方案1】:

    请改用&lt;RecyclerView&gt;RecyclerView 是当您有多个相同类型的项目并且必须动态修改它们时的最佳选择。按照这些链接了解回收器视图并单击回收器视图中的侦听器RecyclerView example.Recycler View Click Listener。我将简要讨论如何做到这一点。

    1. 创建项目布局文件。这基本上就是您的回收站视图中每个项目的外观。在您的情况下,这将是在单独的 xml 文件中声明的 protoitem 中的 LinearLayout
    2. 创建一个数据模型文件,定义代表您的单个实体。例如。创建一个类Item,其中包含字段名称、数量、价格和适当的方法。
    3. 在您的activity_main.xml 中仅添加 recyclerview
    4. 创建一个适配器,将您的数据加载到回收器视图中,并将您的适配器链接到回收器视图。
    5. 创建自定义项目类列表List&lt;Item&gt; myList = new ArrayList&lt;&gt;();
    6. 现在,每当您通过对话框添加新项目时,只需将您的项目添加到 myList 并通过 adapter.notifyDataSetChanged(); 更新您的适配器,适配器将自动将该项目添加到您的回收站视图中。
    7. 对于点击监听器,您需要在适配器中创建一个接口并在您的主活动中实现该点击监听器。

    【讨论】:

      猜你喜欢
      • 2014-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-24
      • 1970-01-01
      • 1970-01-01
      • 2015-11-28
      • 1970-01-01
      相关资源
      最近更新 更多