【问题标题】:OnSwipeRefreshLayout doesn't stop refreshingOnSwipeRefreshLayout 不会停止刷新
【发布时间】:2018-08-28 23:16:32
【问题描述】:

我在片段中的 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();
    }
}

【问题讨论】:

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


    【解决方案1】:

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

    mSwipeRefreshLayout = new SwipeRefreshLayout(getContext());
    

    这与屏幕上的布局无关,而是使用先前使用 findViewById 的引用从 v 视图中获取引用,该视图将显示为片段的布局,所以

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

    【讨论】:

    • 谢谢!很奇怪我之前试过,但它一直说 mSwipeRefreshLayout 是空的。可能是因为我使用 getActivity() 而不是 v 来查找视图。我不知道这会如何改变它。
    【解决方案2】:

    只评论这一行

    mSwipeRefreshLayout = new SwipeRefreshLayout(getContext());
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多