在一个片段的
onCreateView
方法内部:swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
refresh();
}
});
我的refresh()
方法如下:
public void {
//some action
swipeRefreshLayout.setRefreshing(false);
}
但有时候swipeRefreshLayout
不会停止刷新。当我调试代码时,它甚至没有进入refresh()
方法。
我该如何修复它?
编辑: 这里提供更清晰的代码:
public class VisaCardsFragment extends Fragment {
@Bind(R.id.swipe_refresh_layout)
SwipeRefreshLayout swipeRefreshLayout;
@Bind(R.id.recycler_view_visa_cards)
RecyclerView recyclerViewVisaCards;
@Bind(R.id.text_view_message)
TextView textViewMessage;
private List<Card> cards;
private List<VisaCard> visaCards;
private VisaCardsAdapter adapter;
public VisaCardsFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_visa_cards, container, false);
setHasOptionsMenu(true);
ButterKnife.bind(this, view);
visaCards = new ArrayList<>();
adapter = new VisaCardsAdapter(visaCards);
recyclerViewVisaCards.setAdapter(adapter);
recyclerViewVisaCards.setHorizontalScrollBarEnabled(true);
recyclerViewVisaCards.setLayoutManager(new LinearLayoutManager(VisaCardsFragment.this.getActivity(), LinearLayoutManager.VERTICAL, false));
cards = Card.getActiveCards();
if (cards.isEmpty()) {
showMessage("Cards unavailable");
return view;
}
swipeRefreshLayout.setRefreshing(true);
getVisaResponse(); // method containing server request (RESTful) to get visa cards (VisaCard) using the data of cards (Card)
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
refresh();
}
});
return view;
}
public void refresh() {
hideMessage();
cards = Card.getActiveCards();
if (cards.isEmpty()) {
clearList();
swipeRefreshLayout.setRefreshing(false);
showMessage("Visa cards not found");
return;
}
getVisaResponse();// method containing server request (RESTful) to get visa cards (VisaCard) using the data of cards (Card)
}
public void showMessage(String message) {
textViewMessage.setText(message);
textViewMessage.setVisibility(View.VISIBLE);
}
public void hideMessage() {
textViewMessage.setText("");
textViewMessage.setVisibility(View.GONE);
}
}