Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/211.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 我是否应该在单个ScrollView中使用/添加多个片段,而不是ListView/RecyclerView?_Android_Android Fragments_Android Scrollview - Fatal编程技术网

Android 我是否应该在单个ScrollView中使用/添加多个片段,而不是ListView/RecyclerView?

Android 我是否应该在单个ScrollView中使用/添加多个片段,而不是ListView/RecyclerView?,android,android-fragments,android-scrollview,Android,Android Fragments,Android Scrollview,简而言之,我需要多个组件才能在特定时间点调用其特定的API端点。这些组件列在可滚动的父级中。起初,我想像往常一样使用RecyclerView。但是我担心子视图持有者不适合动态API调用。因此,我想出了使用一些片段作为这些组件的想法,并将它们添加到ScrollView父级中 布局如下所示 <android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/andro

简而言之,我需要多个组件才能在特定时间点调用其特定的API端点。这些组件列在可滚动的父级中。起初,我想像往常一样使用RecyclerView。但是我担心子视图持有者不适合动态API调用。因此,我想出了使用一些片段作为这些组件的想法,并将它们添加到ScrollView父级中

布局如下所示

<android.support.v4.widget.NestedScrollView
    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">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:divider="@drawable/divider_linearlayout_transparent_16dp"
        android:orientation="vertical"
        android:showDividers="middle">

        <fragment
            android:id="@+id/fragment_1"
            android:name="my.fragments.DummyChildFragment"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

        <fragment
            android:id="@+id/fragment_2"
            android:name="my.fragments.DummyChildFragment"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

        <fragment
            android:id="@+id/fragment_3"
            android:name="my.fragments.DummyChildFragment"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

        <fragment
            android:id="@+id/fragment_4"
            android:name="my.fragments.DummyChildFragment"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

        <fragment
            android:id="@+id/fragment_5"
            android:name="my.fragments.DummyChildFragment"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

        <fragment
            android:id="@+id/fragment_6"
            android:name="my.fragments.DummyChildFragment"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

        <fragment
            android:id="@+id/fragment_8"
            android:name="my.fragments.DummyChildFragment"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

    </LinearLayout>
</android.support.v4.widget.NestedScrollView>


问题是我想做的是一个好的实践?谢谢

为什么不使用该方法的灵活性呢?
然后,您可以根据您的情况为所需物品充气…

不,这不是标准做法。因为一次加载和显示这些片段需要大量内存。由于回收器视图回收视图,因此默认情况下会管理此问题

如果您知道会有什么样的碎片,那么您还可以在recycler视图中显示不同的视图。在getItemViewType()方法中,可以根据数据源中元素的位置或位置返回viewType。 然后在onCreateViewHolder()中,获得从getItemViewType()方法返回的viewType,并相应地为不同的视图和onBindViewHolder()方法中相同的视图膨胀不同的布局


您可以先调用API填充数据源,然后显示回收器视图。

感谢您的回复。是的,我也知道。但考虑到ViewHolder会被其他的“数据对象”通过滚动代替,但在某个时候,我需要它来调用API,这似乎很奇怪。我不明白你想做什么。。。你能为你想要的行为添加一些模型/精度吗?你的API调用做得怎么样?谢谢你的关注。简单地说,一行中有10个单元格(可能需要使用ListView),每个单元格都连接到一个单独的API端点,我从服务器接收GCM,以便在每个单元格的特定时间点更新/调用API。每个单元格的时间戳都不一样。你好,阮。正如RJ详细介绍的和我提到的,您应该使用recyclerview和“getItemViewType方法”: