我在 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());
与屏幕上的布局无关,而是使用以前的引用,该引用使用 findViewById
从 v
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/