OnSwipeRefreshLayout 不会停止刷新
OnSwipeRefreshLayout doesn't stop refreshing
我在片段中的 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();
}
}
您正在创建一个单独的对象
mSwipeRefreshLayout = new SwipeRefreshLayout(getContext());
这与屏幕上的布局无关,而是使用之前使用 findViewById
的引用从 v
视图获取引用,该视图将显示为片段的布局,因此
mSwipeRefreshLayout = v.findViewById(R.id.SwipeRefresher);
// mSwipeRefreshLayout = new SwipeRefreshLayout(getContext()); remove this
mSwipeRefreshLayout.setOnRefreshListener(this);
评论这一行
mSwipeRefreshLayout = new SwipeRefreshLayout(getContext());
以上行创建了一个新的 SwipeRefreshLayout 实例,因此 mSwipeRefreshLayout 将引用另一个对象,而不是片段中的 mSwipeRefreshLayout
我在片段中的 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();
}
}
您正在创建一个单独的对象
mSwipeRefreshLayout = new SwipeRefreshLayout(getContext());
这与屏幕上的布局无关,而是使用之前使用 findViewById
的引用从 v
视图获取引用,该视图将显示为片段的布局,因此
mSwipeRefreshLayout = v.findViewById(R.id.SwipeRefresher);
// mSwipeRefreshLayout = new SwipeRefreshLayout(getContext()); remove this
mSwipeRefreshLayout.setOnRefreshListener(this);
评论这一行
mSwipeRefreshLayout = new SwipeRefreshLayout(getContext());
以上行创建了一个新的 SwipeRefreshLayout 实例,因此 mSwipeRefreshLayout 将引用另一个对象,而不是片段中的 mSwipeRefreshLayout