从另一个活动刷新SwipeRefreshLayout

时间:2019-02-15 11:54:15

标签: java android swiperefreshlayout

活动“ SubmitListActivity”包含一个SwipeRefreshLayout,需要从“ SettingsActivity”中刷新。


activity_submitlist.xml

<android.support.constraint.ConstraintLayout 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:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SubmitlistActivity">



<android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipeContainerSubmitList"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/submitList"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingBottom="0dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </android.support.v4.widget.SwipeRefreshLayout>

SubmitlistActivity.java

 // this works!
 submitListRefreshLayout = (SwipeRefreshLayout)findViewById(R.id.swipeContainerSubmitList);
 submitListRefreshLayout.setRefreshing(true);

SettingActivity.java

// This throws an error
LayoutInflater inflator = LayoutInflater.from(getApplicationContext());
View layout = inflator.inflate(R.layout.activity_submitlist, null);
SwipeRefreshLayout submitList = layout.findViewById(R.id.swipeContainerSubmitList);
submitList.setRefreshing(true);

引发错误:

android.view.InflateException: Binary XML file line #10: Binary XML file line #10: Error inflating class <unknown>
    Caused by: android.view.InflateException: Binary XML file line #10: Error inflating class <unknown>
    Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.newInstance0(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:430)
        at android.view.LayoutInflater.createView(LayoutInflater.java:645)

第一季度:是否可以在SettingsActivity中刷新swipeRefreshLayout?

第2季度:按下后退按钮时,SubmitListActivity再次滑入->我可以在SubmitListActivity中放置一种方法来捕获此问题并将逻辑放在后面吗?

1 个答案:

答案 0 :(得分:0)

  

从另一个活动刷新SwipeRefreshLayout

在另一个活动中调用一个活动根本不是一个好习惯。对于您的问题,请遵循以下解决方案。

在您的submitListRefreshLayout.setRefreshing(true);方法内的SubmitlistActivity中调用onRestartActivity(),如下所示。

 @Override
    protected void onRestart() {
        super.onRestart();
        submitListRefreshLayout.setRefreshing(true);
    }

注意:-每当您从settingActivity返回到SubmitlistActivity时,onRestart()都会在调用。