在Android Studio中设置刷新swiper处理程序

时间:2017-12-10 04:32:06

标签: android listview swiperefreshlayout

我在我的一个包含ListView的片段中使用SwipeRefreshLayout

    swipeLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipe_refresh);

    swipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {


        @Override
        public void onRefresh() {
            swipeLayout.setRefreshing(true);
            view.refreshDrawableState();
            swipeLayout.setRefreshing(false);
        }
    });

每当我滑动时,我这样做都会刷新我的listView,但没有任何更新。

我的布局实现如下:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/swipe_refresh"
    android:background="@drawable/bg"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:id="@+id/match_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:alpha="255"/>

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

为什么不起作用?

1 个答案:

答案 0 :(得分:0)

您需要将新列表添加到listview适配器。

private void refreshContent(){
   new Handler().postDelayed(new Runnable() {
      @Override
      public void run() {
         mAdapter = new ArrayAdapter<String>(MainActivity.this, 
         android.R.layout.simple_list_item_1, getNewTweets());
         mListView.setAdapter(mAdapter);
         mSwipeRefreshLayout.setRefreshing(false);
      });
}