简体   繁体   English

当recyclerView不可见时,如何swipeToRefresh?

[英]How to swipeToRefresh when recyclerView is not visible?

I'm new in Android, so can't find the answer how to make SwipeToRefresh shown when RecyclerView is empty. 我是Android的新手,所以找不到找到如何在RecyclerView为空时显示SwipeToRefresh的答案。

Prerequisites: In code below I show TextView with "no data" text in case I got no data from server. 先决条件:在下面的代码中,如果我没有从服务器获得数据,我将显示“无数据”文本的TextView。 If there is some data from server, I init adapter and set it to RecyclerView. 如果有来自服务器的数据,我初始化适配器并将其设置为RecyclerView。

Problem: Can't refresh the page using SwipeToRefresh when TextView with "no data" is shown. 问题:当显示“无数据”的TextView时,无法使用SwipeToRefresh刷新页面。 If there is some data in recyclerView - swipeToRefresh works good. 如果recyclerView中有一些数据-swipeToRefresh效果很好。

Thank you in advance. 先感谢您。

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_view_tab"
            style="@style/RecyclerStyle"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:clipToPadding="false"
            android:paddingBottom="@dimen/padding_card_view"
            android:paddingLeft="@dimen/padding_card_view"
            android:paddingRight="@dimen/padding_card_view" />

        <ProgressBar
            android:id="@+id/pb_tab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:indeterminate="true"
            android:layout_gravity="center" />

        <TextView
            android:id="@+id/tv_no_data"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="@string/tab_the_list_is_empty"
            android:textColor="@color/hintTextColor"
            android:textSize="20sp"
            android:visibility="gone" />

        <ProgressBar
            android:id="@+id/pb_tab2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:indeterminate="true"
            style="?android:attr/progressBarStyle"
            android:layout_gravity="center_horizontal|bottom"
            android:visibility="gone" />

    </FrameLayout>

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


/**
 * Make request for get model
 */
@Override
public void getModel(int page, int positionTab) {
    mTvNoData.setVisibility(View.GONE);
    mPbTab.setVisibility(View.VISIBLE);
    mPresenter.getModel(context, page, positionTab);
}

/**
 * Get list model and load/refresh adapter
 */
@Override
public void getModelResponse(List<AllTasksModel> allTasksModels, boolean isLoading) {
    mIsLoading = isLoading;
    mListItems = allTasksModels;
    mPbTab.setVisibility(View.GONE);
    mPbTab2.setVisibility(View.GONE);
    if (mListItems.size() == 0) {
        mTvNoData.setVisibility(View.VISIBLE);
    } else {
        mAdapter = new TabJobsRecyclerAdapter(context, mListItems, this);
        mRecViewTab.setAdapter(mAdapter);
    }
}

Change 更改

From: 从:

@Override
public void getModelResponse(List<AllTasksModel> allTasksModels, boolean isLoading) {
    mIsLoading = isLoading;
    mListItems = allTasksModels;
    mPbTab.setVisibility(View.GONE);
    mPbTab2.setVisibility(View.GONE);
    if (mListItems.size() == 0) {
        mTvNoData.setVisibility(View.VISIBLE);
    } else {
        mAdapter = new TabJobsRecyclerAdapter(context, mListItems, this);
        mRecViewTab.setAdapter(mAdapter);
    }
}

To: 至:

@Override
public void getModelResponse(List<AllTasksModel> allTasksModels, boolean isLoading) {
    mIsLoading = isLoading;
    mListItems = allTasksModels;
    mPbTab.setVisibility(View.GONE);
    mPbTab2.setVisibility(View.GONE);
    mAdapter = new TabJobsRecyclerAdapter(context, mListItems, this);
    mRecViewTab.setAdapter(mAdapter);
    if (mListItems.size() == 0) {
        mTvNoData.setVisibility(View.VISIBLE);
    } else {
        mTvNoData.setVisibility(View.GONE);
    }
}

Basically I am setting the adapter no matter how many data is there, so that swipeToRefresh will work even when there are no items in the listView . 基本上,无论有多少数据,我都将设置适配器,以便即使在listView中没有任何项的情况下,swipeToRefresh也将起作用。 I have one suggestion though, try making the ListView as immediate child of SwipeRefreshLayout instead of having FrameLayout in the middle of view hierarchy. 不过,我有一个建议,请尝试使ListView作为SwipeRefreshLayout直接子代,而不是在视图层次结构的中间SwipeRefreshLayout FrameLayout

The swipe-to-refresh gesture is all based around the scrolling list view. 滑动刷新手势全部基于滚动列表视图。 When updates are entering at the top of the list, pulling the list down means you want to see the newest content. 在列表顶部输入更新时,将列表下拉即可表示您想查看最新内容。 So the pull-down gesture is natural for starting a refresh. 因此,下拉手势很自然地开始刷新。

You need to do one of two things: 您需要执行以下两项操作之一:

  1. Put your "No Data" TextView underneath your RecyclerView with no data in it, so the user can see the TextView and still swipe down on the empty RecyclerView . 将您的“无数据” TextView放在没有数据的RecyclerView 下方 ,以便用户可以看到TextView并向下滑动到空的RecyclerView

  2. Put an additional "Refresh" button next to the "No Data" TextView . 在“无数据” TextView旁边放置一个附加的“刷新”按钮。 This button would only be shown when the TextView is shown. 仅在显示TextView时显示此按钮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM