android - OnSwipeRefreshLayout 不会停止刷新

标签 android android-layout android-fragments android-recyclerview swiperefreshlayout

我在 fragment 中的 RecyclerView 上有一个 OnSwipeRefreshLayout。该 fragment 实现了 OnRefreshListener。我从 fragment 中调用一个方法作为刷新方法。我发现它不会设置 OnRefreshlistener,即使没有错误。在应用程序中,它不会停止刷新,因为永远不会设置 OnRefreshListener。

xml:

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

<android.support.v7.widget.RecyclerView
    android:id="@+id/channel_list"
    android:scrollbars="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />

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

fragment :

public static class tabFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener {

    public static final String ARG_OBJECT = "object";
    public int tabPosition=0;
    RecyclerView mRecyclerView;
    RecyclerView.LayoutManager mLayoutManager;
    ChannelAdapter mAdapter;
    SwipeRefreshLayout mSwipeRefreshLayout;


    public void updateData(){
        mAdapter.retrieveData();
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        savedInstanceState = getArguments();
        tabPosition = savedInstanceState.getInt(ARG_OBJECT);

    }

    public void refreshData(){
        mAdapter.retrieveData();
        mSwipeRefreshLayout.setRefreshing(false);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        final View v = inflater.inflate(R.layout.page_main, container, false);
        mSwipeRefreshLayout = v.findViewById(R.id.SwipeRefresher);
        mSwipeRefreshLayout = new SwipeRefreshLayout(getContext());
        mSwipeRefreshLayout.setOnRefreshListener(this);
        mSwipeRefreshLayout.setColorSchemeColors(getResources().getColor(R.color.color_accent));


        mRecyclerView = v.findViewById(R.id.channel_list);
        mRecyclerView.setHasFixedSize(true);
        mLayoutManager = new LinearLayoutManager(this.getContext());
        mRecyclerView.setLayoutManager(mLayoutManager);
        mAdapter = new ChannelAdapter(tabPosition, new ChannelAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(String channel, int tabPosition) {
                switch (tabPosition) {
                    case 0:
                        Intent intent = new Intent(getActivity(), Messenger.class);
                        intent.putExtra("currentChannel", channel);
                        getActivity().startActivity(intent);
                        break;
                    case 1:
                        //todo: display channel information probably
                        break;
                    case 2:
                        //request user info
                        break;
                }

            }
        });

        mRecyclerView.setAdapter(mAdapter);
        return v;
    }


    @Override
    public void onRefresh() {
        refreshData();
    }
}

最佳答案

您正在创建一个单独的对象

mSwipeRefreshLayout = new SwipeRefreshLayout(getContext());

与屏幕上的布局无关,而是使用以前的引用,该引用使用 findViewByIdv View 中获取引用,该 View 将显示为布局 fragment 如此

    mSwipeRefreshLayout = v.findViewById(R.id.SwipeRefresher);
    // mSwipeRefreshLayout = new SwipeRefreshLayout(getContext()); remove this
    mSwipeRefreshLayout.setOnRefreshListener(this);

关于android - OnSwipeRefreshLayout 不会停止刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52067386/

相关文章:

java - 在 Android 中更改主题 (Android Studio)

java - 系统默认下方的 Android 自定义应用栏

java - 尝试重新打开已关闭的对象: SQLiteQuery

android - Lint 报告 StringBuilder 影响性能

java - 如何使用 JNI 在 C++ 中创建 Java 对象?

java - setText 不适用于纵向布局(android)

android - View 什么时候获得它的边界?

android - 如何在 Fragment 中应用 RecyclerView

java - 将 fragment 添加到 Activity 中

android - 具有渐变 alpha 颜色渐变、黑色细边框和平铺背景的自定义 SeekBar