【问题标题】:How to add item blocks to a scrollview?如何将项目块添加到滚动视图?
【发布时间】:2021-02-17 18:47:16
【问题描述】:

我正在创建一个供个人使用的简单应用程序,但我遇到了困难,因为我需要从滚动视图中添加/删除文本。在下面的屏幕截图中,蓝色方块是我的滚动视图,邮件按钮(占位符)需要打开一个弹出窗口,我可以在其中将 2 个值放入 2 个不同的框中,一个字符串和一个双精度值。

Main_Activity

我几乎什么都知道,但是每次点击邮件按钮and it should look like this when I click on it时都不知道如何添加文字

this is how it should be when is done 积木的左侧按钮是“从滚动视图中删除”按钮,可将其从滚动视图和首选项中删除,右侧按钮与第二张图片类似,但我可以在其中进行编辑。

我的问题是:

如何在滚动视图中添加项目“块”?我的意思是每个块:编辑和删除按钮以及示例中的文本。

关于添加 2 个输入字段的弹出窗口使用什么的建议?

【问题讨论】:

  • 使用对话框获取输入并使用recyclerview创建列表。
  • 我会尝试,但是如何创建带有 2 个输入字段的弹出窗口?我的意思是最简单的方法

标签: java android android-studio


【解决方案1】:

在我看来,由于性能问题,我建议改用 recyclerView。 link

但是,如果您需要使用 ScrollView 的方式,请按照此操作。

在您的 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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/holo_blue_dark"
        android:fillViewport="true"
        android:padding="16dp">

        <LinearLayout
            android:background="@android:color/holo_red_light"
            android:id="@+id/linearLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" />

    </ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>

并在处理点击事件时将项目添加到线性布局中。

  val linear = view.findViewById<LinearLayout>(R.id.linearLayout)
        val item =
            LayoutInflater.from(requireContext()).inflate(R.layout.item_list_row, linear, false)
        linear.addView(item)

【讨论】:

  • 对不起,我可能太着急了。
    对于多个输入,您可以将子视图膨胀到警报对话框中,如下所示 stackoverflow.com/questions/16169787/…
  • 我可能会将字符串保存为数组,对于双精度字符串也是如此。我可能会有类似:汽车[a,b,c,d,e], price[1,2,3,4,5] 如果我使用recycleview 中的按钮,我如何删除与按钮对应的汽车按下?就像我删除汽车 c 我想删除价格 3. 所有这些都在我的 sharedPreferences 中
  • 你可以按照这个快速示例github.com/siwarakDee/AddCar
猜你喜欢
  • 2016-12-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多