【问题标题】:Android inflate linear layout with data from databaseAndroid 使用数据库中的数据膨胀线性布局
【发布时间】:2014-06-21 21:43:18
【问题描述】:

您好,我正在构建一个 Android 应用程序。我正在从我的数据库中获取一些数据并在布局中对其进行膨胀。目前我正在列表视图中显示数据。但是除了在 Listview 中显示数据之外,还有其他方法吗?我的意思是像 Listview 一样以线性布局显示数据?

我不想使用 Listview,因为我的内容放置在 ScrollView 中,并且在 ScrollView 中使用 ListView 不会完全显示 Listview 元素的高度。

请看图

我希望我的整个布局滚动。为了更简单,我想要一个完整的窗口滚动,就像我们在 cmets 部分的 facebook android 应用程序中一样。就像顶部的图像和底部的 cmets 一样,整个窗口滚动,但评论编辑文本和按钮保持固定。

有什么帮助吗??

对不起,如果我的问题太长了。

提前致谢!

【问题讨论】:

    标签: android-layout android-listview android-linearlayout


    【解决方案1】:

    嗨,赛义德·塞胡·穆贾米尔 A,

    您可以利用LinearListViewlibrary 来实现您的目标。这个库允许您创建一个绑定到 LinearLayout 的 ListAdapter。然后,您可以将这个“ListView”(它实际上不是引擎盖下的 ListView)放在 ScrollView 中。然后你的整个布局可以滚动到你想要的样子。

    例如,您可以这样设置视图:

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                
                <!-- Your Title and other stuff in the green section here -->
            </LinearLayout>
    
            <com.linearlistview.LinearListView
                xmlns:app="http://schemas.android.com/apk/res-auto"
                android:id="@+id/main_linear_list_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:showDividers="none|middle|beginning|end"
                android:divider="#488888"
                android:dividerPadding="2dp"
                app:dividerThickness="2dp"
                />
    
    
        </ScrollView>
    

    然后,您可以将带有 BaseAdapter 或 ListAdapter 的 LinearListView 视为普通的 ListView(确保优化您的 getView() 方法)。不过这里要小心的是,这个库使用了线性布局,并且加载很多元素可能会锁定 UI 线程(不像 ListView 会巧妙地加载和加载对象)。可以巧妙地处理您的 getView() 和 notifyDatasetChange() 方法以更好地优化初始加载(即:诸如 AsyncTasks 和 ExecutorThreadPools 来帮助加载数据)。

    希望对你有帮助!

    -埃文

    【讨论】:

      猜你喜欢
      • 2019-01-22
      • 2016-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多