在片段事务期间,当swipeRefreshLayout上的setRefreshing(false)时,Android片段两次

时间:2016-05-12 21:52:50

标签: android android-fragments swiperefreshlayout

我的活动中有两个片段(我使用片段事务在它们之间导航):

  1. 主题列表
  2. 主题详情 - 成绩列表
  3. 主题片段布局:

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

    swipeRefreshLayout的监听器是活动。它会触发AsyncTask,它会下载新数据,通知片段数据已更改并隐藏swipeRefreshLayout。

    onPostExecute方法的问题部分:

    ...
    
    try {
        formatData();
    } catch (JSONException e) {
        e.printStackTrace();
    }
    
    View refresher = findViewById(R.id.refresher);
    ((SwipeRefreshLayout) refresher).setRefreshing(false);
    
    marksFragment.notifyUpdate(activity);
    subjectsFragment.notifyUpdate(activity);
    

    它通常有效,但不是在这种情况下:

    • 我拉动刷新数据
    • 我导航到主题细节(使用 片段交易)
    • 在调用期间调用setRefreshing方法 片段交易

    在这种情况下,主题片段只会挂在那里,两个片段都在活动中。

    SubjectFragment代码:

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ((SwipeRefreshLayout) view.findViewById(R.id.refresher)).setOnRefreshListener((SwipeRefreshLayout.OnRefreshListener) activity);
    }
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.subjects_fragment, null);
    }
    

    如何在refreshLayout上setRefreshing(false)并避免让主题片段两次?

3 个答案:

答案 0 :(得分:1)

我在SO上发现了一个类似的问题:

When switch fragment with SwipeRefreshLayout during refreshing, fragment freezes but actually still work

我使用appcompat-v7:23.3.0。这个答案对我有用:

  

这个问题似乎仍然发生在appcompat 22.1.1中。包皮   FrameLayout中的SwipeRefreshLayout为我解决了这个问题。

所以我的主题片段布局如下所示:

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

        <ListView
            android:id="@android:id/list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

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

答案 1 :(得分:0)

更改您的代码,参考答案stack overflow

  public class YourFragment extends Fragment {
    SwipeRefreshLayout swipeLayout;

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        String[] values = new String[] { "Android List View",
                "Adapter implementation",
                "Simple List View In Android",
                "Create List View Android",
                "Android Example",
                "List View Source Code",
                "List View Array Adapter",
                "Android Example List View",
                "Android List View",
                "Adapter implementation",
                "Simple List View In Android",
                "Create List View Android",
                "Android Example",
                "List View Source Code",
                "List View Array Adapter",
                "Android Example List View",
                "Android List View",
                "Adapter implementation",
                "Simple List View In Android",
                "Create List View Android",
                "Android Example",
                "List View Source Code",
                "List View Array Adapter",
                "Android Example List View"
        };

        final SwipeRefreshLayout swipeView = (SwipeRefreshLayout)view. findViewById(R.id.refresher);

        swipeView.setEnabled(false);
        ListView lView = (ListView) view.findViewById(R.id.your_list_view);
        ArrayAdapter<String> adp = new ArrayAdapter<String>(view.getContext(),
                android.R.layout.simple_list_item_1, android.R.id.text1, values);
        lView.setAdapter(adp);

        swipeView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                swipeView.setRefreshing(true);
                ( new Handler()).postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        swipeView.setRefreshing(false);

                    }
                }, 3000);
            }
        });

        lView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView absListView, int i) {

            }

            @Override
            public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                if (firstVisibleItem == 0)
                    swipeView.setEnabled(true);
                else
                    swipeView.setEnabled(false);
            }
        });

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.subjects_fragment, null);
    }


    }

答案 2 :(得分:0)

我面临同样的问题cout << ((zs & X) != 0) << endl; cout << ((zs & Y) != 0) << endl; cout << ((zs & Z) != 0) << endl; cout << (zs & NUM) << endl; 两次来电。

基本上原因不在于swipelayout.setOnRefresh当我的recyclerview到达终点时我正在加载一些数据而在swipelayout.setOnRefreshListener上我正在做同样的事情。

所以我做的是删除这个bug,我只是从swipelayout.setOnRefreshLisetener注释掉了加载方法。

swipelayout.setOnRefreshListener

这就是我在recyclelerview中所做的事情

 swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {

                if (mLatestAdapter.getItemCount() > 0) {

                    mLatestAdapter.removeAllItems();
                    mPresenter.resetPagination();
                   // mPresenter.loadMoreItems(false);//commentwed this line 


                }
            }
        });