OnSwipeRefreshLayout不会停止刷新

时间:2018-08-28 23:16:32

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

我在RecyclerView的片段中有一个OnSwipeRefreshLayout。该片段实现了OnRefreshListener。我将片段中的方法称为刷新方法。我发现,即使没有错误,它也不会设置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>

片段:

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

2 个答案:

答案 0 :(得分:4)

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

mSwipeRefreshLayout = new SwipeRefreshLayout(getContext());

与屏幕上的布局无关,而是使用先前的引用,该引用使用findViewByIdv视图中获取引用,该视图将显示为片段的布局,因此

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

答案 1 :(得分:1)

仅对此行发表评论

mSwipeRefreshLayout = new SwipeRefreshLayout(getContext());

上一行创建了SwipeRefreshLayout的新实例,因此mSwipeRefreshLayout将引用另一个对象,而不是片段中的mSwipeRefreshLayout