带有tabview冲突的SwipeRefreshView和片段不刷新

时间:2018-12-18 09:35:59

标签: android

我正在尝试在我的APP上添加刷新视图。问题是每当我尝试在选项卡之间切换时。 SwipeRefresh激活。我尝试将其移至代码的几个部分,但仍然可以做到这一点。我尝试将SwipeRefreshlayout移到我的一个片段上,并且使用空指针崩溃。并且即使我刷新该片段也无法识别它。

我的最新尝试是将整个containt_main.xml作为我的滑动刷新。并尝试在标签之间移动时再次激活

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v4.widget.SwipeRefreshLayout  xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/refresh">

    <android.support.v4.widget.DrawerLayout
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/main_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:fitsSystemWindows="true"
    >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?android:attr/actionBarSize"
                android:background="@color/colorPrimary"
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                android:elevation="4dp" />

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">



                <android.support.v4.view.ViewPager
                android:id="@+id/container"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_behavior="@string/appbar_scrolling_view_behavior" >
                <android.support.design.widget.TabLayout
                    android:id="@+id/tabs"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:tabGravity="fill"
                    app:tabMode="scrollable"
                    app:tabTextAppearance="@android:style/TextAppearance.Widget.TabWidget" />

            </android.support.v4.view.ViewPager>

            </RelativeLayout>

         <FrameLayout
             android:id="@+id/fragment_container"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:layout_marginTop="20dp"/>
        </LinearLayout>
        <android.support.design.widget.NavigationView
            android:id="@+id/navigation_view"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            app:headerLayout="@layout/nav_header"
            app:menu="@menu/drawer_menu"
          />

    </android.support.v4.widget.DrawerLayout >
        </android.support.v4.widget.SwipeRefreshLayout>

我的片段XML。

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="MainActivity"
    android:layout_below="@+id/cardview">


    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
  />
</LinearLayout>

此代码块在我的mainactivity Reader中。class读取XML xml并将其保存在数据库中。

  final SwipeRefreshLayout swipeRefreshLayout = findViewById(R.id.refresh);
        swipeRefreshLayout.setColorSchemeResources(R.color.colorPrimary,R.color.colorPrimaryDark,R.color.colorAccent);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                swipeRefreshLayout.setRefreshing(true);
                (new Handler()).postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        swipeRefreshLayout.setRefreshing(false);
                        Reader fromthepast = new Reader(getApplicationContext(), "RSSLINK (censored)", "fromthepast");


                        fromthepast.execute();


                    }
                },3000);
            }
        });

    }

我在片段上放了我的刷新监听器。

@Override
public void onRefresh() {
    getFragmentManager()
            .beginTransaction()
            .detach(this)
            .attach(this)
            .commit();

}

错误日志

> E/AndroidRuntime: FATAL EXCEPTION: main
>     Process: com.lemesosblog.lemesosblog, PID: 27482
>     java.lang.NullPointerException: Attempt to invoke virtual method 'void
> android.support.v4.widget.SwipeRefreshLayout.setColorSchemeResources(int[])'
> on a null object reference
>         at com.lemesosblog.lemesosblog.Fragment.fromthepast_Fragment.onCreateView(fromthepast_Fragment.java:46)
>         at android.support.v4.app.Fragment.performCreateView(Fragment.java:2346)
>         at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1428)
>         at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1759)
>         at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1827)
>         at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:797)
>         at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2596)
>         at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2383)
>         at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2338)
>         at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:2215)
>         at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:649)
>         at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:145)
>         at android.support.v4.view.ViewPager.populate(ViewPager.java:1238)
>         at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:663)
>         at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:625)
>         at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:606)
>         at android.support.design.widget.TabLayout$ViewPagerOnTabSelectedListener.onTabSelected(TabLayout.java:2170)
>         at android.support.design.widget.TabLayout.dispatchTabSelected(TabLayout.java:1165)
>         at android.support.design.widget.TabLayout.selectTab(TabLayout.java:1158)
>         at android.support.design.widget.TabLayout.selectTab(TabLayout.java:1128)
>         at android.support.design.widget.TabLayout$Tab.select(TabLayout.java:1427)
>         at android.support.design.widget.TabLayout$TabView.performClick(TabLayout.java:1537)
>         at android.view.View$PerformClick.run(View.java:24828)
>         at android.os.Handler.handleCallback(Handler.java:789)
>         at android.os.Handler.dispatchMessage(Handler.java:98)
>         at android.os.Looper.loop(Looper.java:164)
>         at android.app.ActivityThread.main(ActivityThread.java:6798)
>         at java.lang.reflect.Method.invoke(Native Method)
>         at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
>         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
> D/OSTracker: OS Event: crash Application terminated.

1 个答案:

答案 0 :(得分:0)

请勿将SwipeRefreshLayout用作父项

您必须为每个SwipeRefreshLayout添加RecyclerView

fragment.xml

<android.support.v4.widget.SwipeRefreshLayout  
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/refresh1">

    <android.support.v7.widget.RecyclerView
      android:id="@+id/recyclerView1"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_alignParentTop="true"/>

</android.support.v4.widget.SwipeRefreshLayout>