添加滑动以刷新动态列表的布局

时间:2017-01-19 05:13:16

标签: android android-recyclerview swiperefreshlayout

在我的应用中,有一个指定路线列表。路线已从网络分配。列表在视图中下拉后已更新。但是,每次我拉下来,查看扩展。

这是我在应用滑动刷新之前的屏幕截图:

enter image description here

这是我应用滑动刷新后的屏幕截图:

enter image description here

每次下拉视图后,视图都会连续展开以进行刷卡刷新。

这就是我在布局文件中包含SwipeRefreshLayout的方式:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout     
    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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginEnd="10dp"
    android:layout_marginStart="10dp"
    android:layout_marginTop="10dp"
    android:background="?android:attr/selectableItemBackground"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".ui.activities.RoutesAssignActivity">

<android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/swipeToRefreshLayoutId"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/md_white_1000"
        android:scrollbars="vertical" />
</android.support.v4.widget.SwipeRefreshLayout>


</RelativeLayout>

这是我的Api电话:

void getRoutes() {

    Log.e("getRoutes", "=======getRoutes======== ");



    final ApiService service = RestClient.getClient();
    Call<ApiResponse> signedIn = service.getRoutesApi(getPreference().getToken());
    signedIn.enqueue(new Callback<ApiResponse>() {
        @Override
        public void onResponse(Response<ApiResponse> response) {
            if (response.isSuccess()) {
                ApiResponse result = response.body();

                //error handling
                if (result.getData() != null && result.getData().getRoutesList() != null) {
                    dialog.dismiss();
                    emptyHistory.setVisibility(View.GONE);
                    historyView.setVisibility(View.VISIBLE);
                    routesDatas = result.getData().getRoutesList();
                    setupRecyclerView(routesDatas);
                } else if (result.getMsg().equalsIgnoreCase("Invalid token or Token has expired")) {
                    getPreference().removeLoginPreferences();
                    Intent intent = new Intent(RoutesAssignActivity.this, LoginActivity.class);
                    finish();
                    intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                    overridePendingTransition(0, 0);
                    startActivity(intent);
                } else {
                    dialog.dismiss();
                    emptyHistory.setVisibility(View.VISIBLE);
                    historyView.setVisibility(View.GONE);
                }
            } else {
                // response received but request not successful (like 400,401,403 etc)
                //Handle errors
                dialog.dismiss();
                Snackbar.make(findViewById(android.R.id.content),
                        response.message(), Snackbar.LENGTH_LONG).show();
            }
        }

        @Override
        public void onFailure(Throwable t) {
            dialog.dismiss();
            showNetworkSettings();
            Log.e("Errors :: getRoutes", "=======Status Code ======== " + t.getMessage());
        }
    });
}

在此处分配路线获取更新:routesDatas = result.getData().getRoutesList();

onCreate方法中,我调用了getRoutes()方法:

if (getNetworkStatus()) {
        getRoutes();
    } else {
        showNetworkSettings();
    }

我将RecyclerView设置为:

void setupRecyclerView(List<RoutesList> routes) {
    dialog.dismiss();
    FontIconTypefaceHolder.init(getAssets(), "fonts/MaterialIcons-Regular.ttf");

    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    stickyHeaderListAdapter = new StickyHeaderListAdapter(routes, this);
    recyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(this).build());
    final StickyRecyclerHeadersDecoration headersDecor = new StickyRecyclerHeadersDecoration(stickyHeaderListAdapter);
    recyclerView.addItemDecoration(headersDecor);
    recyclerView.setAdapter(stickyHeaderListAdapter);
}

我以Swipe to Refresh方法应用onCreate,因此每次下拉时视图都会不断扩展。

if (getNetworkStatus()) {
        getRoutes();
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getRoutes();
                swipeRefreshLayout.setEnabled(false);
            }
        });
    } else {
        dialog.dismiss();
        showNetworkSettings();
    }

如何在此处实施Swipe to Refresh

1 个答案:

答案 0 :(得分:1)

每次刷新时都不要将适配器设置为 RecyclerView ,只需在适配器中创建一个方法,用于设置适配器中的项目并调用{{ 1}}。由于您的商品尺寸可能会有所不同,因此不需要notifyDataSetChanged()。同样在您的布局中,使用recyclerView.setHasFixedSize(true);作为每个小部件的高度。

喜欢:
在调用API之前match_parent,请调用onCreate()这是一个空列表。

Apapter 中创建类似

的方法
setupRecyclerView(new ArrayList<RoutesList>());

public void setItemsAndUpdateList(ArrayList<RoutesList> routes) { //clear your old list your_list.clear(); //Add all new Items your_list.addAll(routes); notifyDataSetChanged(); } 而不是致电getRoutes(),请致电setupRecyclerView(routesDatas);