如何在列表视图中的最后一个项目之后添加按钮 + 纯文本?



我对安卓开发和安卓工作室很陌生;我将尽量清楚地表达我的问题。如果我在提到某些事情时使用错误的术语,请在您的答案中告诉我。

我想做什么?

我想从字符串数组创建一个项目列表,这些字符串的值已存储在数组中.xml。我希望用户通过选中每个字符串旁边的框来选择要使用的字符串。此外,我想在数组中的最后一项之后添加以下内容:

[+] _Add_your_own_item__

(用户将编写一个字符串并按 [+] 按钮,这将在上面的列表中添加一个新项目,并带有一个框来勾选)。

我希望此按钮 + 纯文本位于包含列表的列表视图中,就好像此按钮 + 文本是列表中的最后一项一样。

到目前为止,我能够做什么?

我已经设法为数组的所有元素创建了一个动态复选框列表:

protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_characters);
listView = (ListView)findViewById(R.id.listView);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
characters = getResources().getStringArray(R.array.characters);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, characters);
listView.setAdapter(arrayAdapter);
for(int i = 0; i < characters.length; ++i)
{
listView.setItemChecked(i, false);
}
}

我尝试创建按钮+纯文本直接修改我的活动的.xml文件,但它只是将它们放在ListViex之外。

根据我非常有限的理解,我的 ArrayAdapter 将每个字符串转换为"复选框"视图,并将它们放在 ListView 中一个接一个之下;我怎样才能对按钮 + 文本做同样的事情?我只需要推动正确的方向,然后我可以尝试找出将我的新字符串添加到复选框列表中的逻辑。

到目前为止,我的布局是这样的:(那里的按钮只是告诉我选择了哪些字符串)

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:text="@string/characters"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
android:onClick="getSelectedSuspects"
android:text="@string/continue_text"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<LinearLayout
android:id="@+id/checksContainer"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
android:orientation="vertical"
app:layout_constraintBottom_toTopOf="@+id/button4"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>

列表视图的最简单方法是设置页脚视图。 页脚视图位于列表内部并随之滚动,但不计为列表的元素。 使用listView.addFooterView(view)添加它。 视图通常通过 LayoutInflater 从 xml 膨胀。

如果有人处于我几个小时前的位置:我将根据我从@Game的回答中理解的内容添加更新手友好的回复。

我创建了一个新布局,该布局将作为另一个视图组插入到列表视图的底部。对于我问题中的示例,这将是:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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=".DisplayCharactersActivity">
<EditText
android:id="@+id/editText2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Add a suspect"
android:inputType="textPersonName"
android:text=""
app:layout_constraintEnd_toStartOf="@+id/button5"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:text="+"
app:layout_constraintBaseline_toBaselineOf="@+id/editText2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/editText2" />
</android.support.constraint.ConstraintLayout>

然后,在活动的 OnCreate() 方法中添加:

LayoutInflater layoutInflater = getLayoutInflater();
ViewGroup footer = (ViewGroup)layoutInflater.inflate(R.layout.footer_button_text, listView, false);
listView.addFooterView(footer);

我不明白这行背后的所有细节,但它在列表视图的底部"附加"了这个 ViewGroup;如果有人想更详细地解释这一点,请随时编辑。

最新更新