【问题标题】:Android: Create a fragment with variable elementsAndroid:创建具有可变元素的片段
【发布时间】:2016-10-02 22:03:25
【问题描述】:

当我单击主活动中的按钮时,我需要在垂直滚动中动态创建垂直线性布局。线性布局的行数必须与主 Activity 的编辑文本中指示的一样多。

我现在如何创建具有线性布局的片段和按下按钮时的滚动,但我不知道如何根据主活动的编辑文本的值用按钮填充这个线性布局。

图解说明:

我有这个代码:

public void onAdd(View view) { // This is the method called when button is clicked
    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction transaction = fragmentManager.beginTransaction();
    BlankFragment fragment = new BlankFragment();
    transaction.add(R.id.table2,fragment);
    transaction.commit();
}

这是片段的xml代码:

<LinearLayout 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"
android:orientation="vertical"
tools:context="com.example.xxxxx.yyyyy.BlankFragment">

    <ScrollView
        android:id="@+id/sv1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <LinearLayout
            android:id="@+id/ll6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <!-- Buttons go here -->

        </LinearLayout>
    </ScrollView>

【问题讨论】:

  • 可以使用 ListView 或 RecyclerView。 LinearLayout 只是您需要使用自定义适配器实现列表的布局格式

标签: android button scroll fragment android-linearlayout


【解决方案1】:

不要在 LinearLayout 中手动添加视图,而是使用 RecyclerView 或 ListView 并创建一个简单的适配器来呈现视图。之后,您需要做的就是根据用户从 EditText 的输入更新适配器的项目。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-26
    • 1970-01-01
    • 2016-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-25
    • 1970-01-01
    相关资源
    最近更新 更多