android - ListView 放在 ScrollView 中,是个坏主意吗?那么如何替换 ScrollView 中动态填充的列表呢?

标签 android listview dynamic scroll android-scrollview

我遇到了这个问题,我需要在其中一个以 ScrollView 为根元素的 Activity 中动态输出一个“列表”。

我发现很多帖子都展示了解决方法和技巧,但大多数人都说将 ListView 放在 ScrollView 中是一个坏习惯。我明白,但他们都没有表明哪个是正确的方法?我可以考虑动态创建 TextView 并将它们添加到布局中,但我不确定这是否是最好的方法。

那么,如何正确完成这件事呢? 谢谢!

最佳答案

不幸的是我没有得到我想要的答案。我继续寻找,找到了一个很好的解决方案,非常适合我的情况!

我创建了一个简单的 LinearLayout 并将我的 ArrayAdapter 连接到它,同时删除了我之前使用的 ListView。不建议长列表,但我的列表长度少于 10 项。

...

        //Creating a custom ArrayAdapter
        ArrayAdapter adapt2 = new DirectionsListAdapter(this, myList);      

        //Pointing to the LinearLayout
        LinearLayout testContainer = (LinearLayout) findViewById(R.id.testContainer);
        final int adapterCount = adapt2.getCount();

        //adding each adapter item to the LinearLayout
        for (int i = 0; i < adapterCount; i++) {
              View item = adapt2.getView(i, null, null);
              testContainer.addView(item);
            }

...

到目前为止,它运行得非常好,我拥有以前拥有的所有自定义选项,它快速而干净,最好的是它甚至在根 ScrollView 内部也能自动调整其高度。

关于android - ListView 放在 ScrollView 中,是个坏主意吗?那么如何替换 ScrollView 中动态填充的列表呢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27474153/

相关文章:

自定义 cursorAdapter 的 android newView/bindView 未调用

c# - 动态变量如何影响性能?

android - 如何测试依赖于 CountDownTimer 的 Android 类

java - 在 ListView 中加载共享首选项

android - 如何正确设置 EditText 的 InputType?

android - 在 Jetpack Compose 中启动动画矢量 Drawable

android - 在 listView 中加载异步图片

android - ViewHolder 模式 - 如何正确更新 View

c# - 是否有 C# 版本的 CTypeDynamic?

c函数指针在运行时传递参数