查看寻呼机仅在第一次为空时

时间:2017-06-13 07:12:05

标签: android

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

代码:

主片段包含视图寻呼机:

    <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();          

    }

任何想法?

2 个答案:

答案 0 :(得分:1)

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

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

}

答案 1 :(得分:0)

尝试以下对viewpager的更改,

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