Android 添加数据到自定义布局和 ScrollView

标签 android xml android-layout

在我的应用程序中,我有简单的布局来填充任何具有类结构的数据。添加项目后,我想将此布局添加到 ScrollView 中。将布局添加到 ScrollView 是成功的,但我无法将数据设置为自定义布局。例如,这是我设置数据的自定义布局:

tile.xml:

<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="50dp"
            android:background="@drawable/drop_shadow">
    </LinearLayout>
    <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="30dp" android:minHeight="100dp" android:background="#eee">
        <TextView
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text="New Text"
                android:id="@+id/username"/>
    </LinearLayout>
</LinearLayout>

类数据结构:

public class SubjectStructure {
    public String    username;
    public String    topic;
    public String    description;
    public String    avatar;
    public String    sender;
    public Integer   grade;
    public Integer   type;
    public String    date;
}

现在我想填充 tile.xml 并将其添加到下面的布局中:

fragment_home.xml:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="#F1F1F1">
    <LinearLayout android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:orientation="vertical"
                  android:id="@+id/scrollView1">
    </LinearLayout>
</ScrollView>

我的代码是这样的: public View onCreateView(LayoutInflater inflater, ViewGroup 容器, bundle 保存的实例状态){

    View rootView = inflater.inflate( R.layout.fragment_home, container, false);
    scrollView = (LinearLayout) rootView.findViewById ( R.id.scrollView1 );

    for(SubjectStructure SS: G.subject_items){
        LinearLayout newLL = new LinearLayout( G.context );
        TextView newTV = new TextView( G.context );
        newTV.setText ( SS.topic );
        newLL.addView(newTV);

        LayoutInflater in =  (LayoutInflater) G.context.getSystemService(G.context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.tile, null);
        scrollView.addView(view);
    }
    return rootView;
}

在这段代码中,我可以将 tile.xml 添加到 fragment_home.xml 中。但我无法将文本 SS.topic 设置为 tile.xmlSS.topic 不为空且有数据。如何解决这个问题?

最佳答案

你肯定错过的是

 scrollView.addView(newLL); 

每次迭代。关于

LayoutInflater in =  (LayoutInflater) G.context.getSystemService(G.context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.tile, null);
 scrollView.addView(view);

我不确定它的用途。您在每次迭代时都创建了一个新的 tile 实例,但您对它的内容什么也不做。在次要方面,您已经有一个 LayoutInflater。您不需要在每次迭代时向系统请求一个,使用您获得的一个作为参数。

关于Android 添加数据到自定义布局和 ScrollView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26960183/

相关文章:

android - 使用通过推送创建的节点更新 Firebase 数据库

Android VideoView简历和seekTo

android - 使用 XML 自定义布局

JavaScript 循环不会分隔 <TD> 元素

android - 以编程方式使 TextView 背景颜色在按下时发生变化

android - 设置提示时 EditText 不扩展

java - 如何在不使用多行的情况下换行 edittext?

java - Android 复选框白色边框

c# - 从 XML 到 PLIST 的转换

android - 在三星 Galaxy Note Edge 上扩展类 android.support.design.widget.AppBarLayout 时出错