【问题标题】:View Pager is empty only at first timeView Pager 仅在第一次为空
【发布时间】:2017-06-13 15:36:16
【问题描述】:

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

代码:

主片段包含视图寻呼机:

    <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>

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

   <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

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

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

    }

有什么想法吗?

【问题讨论】:

  • 你不必把Recyclerview 放在ScrollView 里面,它实现了ScrollingView & NestedScrollingChild
  • 我想用水平和垂直滚动创建回收视图,这不是问题,所以我更新了问题。

标签: android


【解决方案1】:

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

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

}

【讨论】:

  • 你在哪里定义了你的viewpager?如果可以的话把你的整个片段
  • 我尝试了你的建议。 viewpager 视图还没有在这里膨胀,所以它不会工作。
  • 请告诉我你的第一个片段以及你在哪里定义了你的 viewpager
【解决方案2】:

尝试对您的浏览器进行以下更改,

  <android.support.v4.view.ViewPager
        android:id="@+id/CardTablepager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:minHeight="600dp"
        android:layout_weight="1"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

对于RecyclerView 设置以下属性:

    recyclerView.setHasFixedSize(true);
    recyclerView.setNestedScrollingEnabled(false);

【讨论】:

  • 不错的尝试,但仍然是相同的行为。
猜你喜欢
  • 1970-01-01
  • 2013-08-11
  • 1970-01-01
  • 2015-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-23
  • 1970-01-01
相关资源
最近更新 更多