SwipeRefreshLayout没有停止刷新

时间:2018-06-22 06:28:32

标签: android

片段的onCreateView内部方法:

swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            refresh();
        }
});

我的refresh()方法类似于:

public void {
    //some action

    swipeRefreshLayout.setRefreshing(false);
}

但是有时候swipeRefresLayout并没有停止刷新。当我调试代码时,它甚至都不会使用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);
}

}

4 个答案:

答案 0 :(得分:3)

我想回答自己的问题,以便如果有人犯同样的错误,则可以找到解决方案。

我的问题是有时swipeRefresLayout并没有停止刷新。当我调试代码时,它甚至都不会使用refresh()方法。正如您看到的那样,它并不总是如此。

好的,什么时候发生的? cards列表为空时发生了。

...

cards = Card.getActiveCards();
if (cards.isEmpty()) {
    showMessage("Cards unavailable");
    return view;
}

//some code here

swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
    @Override
    public void onRefresh() {
        refresh();
    }
});

...

cards列表为空时,它返回包含view而没有swipeRefreshLayout的{​​{1}}。因此,当您刷新时,因为没有refreshListener,所以没有任何反应。

解决方案是仅在返回listener之前设置listener,如下所示:

view

答案 1 :(得分:0)

  <?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swipe_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ListView
        android:id="@+id/listaNoticias"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="6dp"
        android:layout_marginRight="6dp"
        android:layout_marginTop="5dp" >
    <ListView>

</android.support.v4.widget.SwipeRefreshLayout>

这是实现的Java代码;

     public class classname extends Fragment implements OnRefreshListener {
    SwipeRefreshLayout swipeLayout;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    // Inflate the layout for this fragment
    view = inflater.inflate(R.layout.classname, container, false);

    swipeLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipe_container);
    swipeLayout.setOnRefreshListener(this);


@Override
public void onRefresh() {
   //action
   //and set false to stop.
}

答案 2 :(得分:0)

SwipeRefreshLayout.OnRefreshListener仅在检测到滑动手势时才会触发,请参见docs

  

在滑动手势触发刷新时调用。

但是在代码中,您可以通过调用以下内容开始刷新:

swipeRefreshLayout.setRefreshing(true);

因此刷新指示器仅从此处开始刷新,并且不会触发SwipeRefreshLayout.OnRefreshListener。如果您想在Fragment开始时开始刷新,只需在refresh()之后致电setRefreshing(true)

swipeRefreshLayout.setRefreshing(true);
refresh();

或者,如果您要刷新直到用户做出滑动手势,只需删除此行即可。

swipeRefreshLayout.setRefreshing(true);

当您检测到滑动手势时,您的SwipeRefreshLayout.OnRefreshListener将被触发。

答案 3 :(得分:0)

您可以使用以下方法设置运行swipeRefreshLayout的时间:

        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    loadPosts(); //This is where I want to update my data
                    swipeRefreshLayout.setRefreshing(false);
                }
            }, 1000); // This is how you can choose when it will end

        }

    });