gpt4 book ai didi

android - View Pager 仅在第一次为空

转载 作者:行者123 更新时间:2023-11-29 01:09:10 26 4
gpt4 key购买 nike

我有一个包含回收 View 的 fragment ,在每个项目上按下都会显示另一个 fragment ,该 fragment 持有一个在每次滑动时显示 fragment 的 View 寻呼机。当我在按下回收 View 项后第一次进入 fragment 时,寻呼机不可见。第二次和其余时间数据按预期显示。

代码:

主要 fragment 包含 View 寻呼机:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">


<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/lineItems"
style="@style/TitleTextView"/>

<android.support.v4.view.ViewPager
android:id="@+id/CardTablepager"
android:layout_width="match_parent"
android:layout_alignParentBottom="true"
android:layout_height="wrap_content"/>

</LinearLayout>

内容 fragment (查看寻呼机滑动此 fragment ):

   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/poMainlayout">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/CardViewStyle"
android:layout_margin="8dp"
>
<android.support.v7.widget.RecyclerView
android:id="@+id/poLineItemsTable"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbarStyle="outsideInset"
/>
</android.support.v7.widget.CardView>

</LinearLayout>

我在没有帮助的情况下阅读了几篇文章:Android Viewpager display wrong data on first load

我还试图在没有帮助的情况下使主 fragment 中的适配器无效:

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
viewPager.getAdapter().notifyDataSetChanged();

}

有什么想法吗?

最佳答案

只需通知您的适配器 onCreateView 而不是 onViewCreated

@Override
public void onCreateView(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
viewPager.getAdapter().notifyDataSetChanged();

}

关于android - View Pager 仅在第一次为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44514611/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com