【问题标题】:How to refresh SwipeRefreshLayout with two views inside如何用里面的两个视图刷新 SwipeRefreshLayout
【发布时间】:2015-12-09 08:46:02
【问题描述】:

我有 SwipeRefreshLayout 和两个孩子:RecyclerViewLinearLayout 所以我不得不自定义 RecyclerView :

recycler_view.setOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            // int topRowVerticalPosition = (recyclerView == null || recyclerView.getChildCount() == 0) ? 0 : recyclerView.getChildAt(0).getTop();
            // swipeRefreshLayout.setEnabled(topRowVerticalPosition >= 0);
            if (!defined) {
                toolbarHeight = ll_header.getMeasuredHeight() + 15;
                footerHeight = ll_footer.getMeasuredHeight() + 15;
                defined = true;
            }
            clipToolbarOffset();
            if ((toolbarOffset < toolbarHeight && dy > 0) || (toolbarOffset > 0 && dy < 0)) {
                toolbarOffset += dy;
            }
            if ((footerOffset < footerHeight && dy > 0) || (footerOffset > 0 && dy < 0)) {
                footerOffset += dy;
            }
            onMoved(toolbarOffset, footerOffset);
        }

        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            // super.onScrollStateChanged(recyclerView, newState);
            if (recyclerView.getAdapter() != null) {
                if (recyclerView.getAdapter().getItemCount() > 0) {
                    int currentPosition = ((RecyclerViewAdapter) recyclerView.getAdapter()).getCurrentPosition();
                    if (currentPosition > limit && currentPosition >= items.size() - 2) {
                        if (!isLoading) {
                            getContent();
                        }
                    }
                }
            }
            LinearLayoutManager llm = (LinearLayoutManager) recyclerView.getLayoutManager();
            if (recyclerView.getAdapter() == null) {
                swipeRefreshLayout.setEnabled(true);
                return;
            }
            swipeRefreshLayout.setEnabled(llm.findFirstCompletelyVisibleItemPosition() == 0);
        }
    });

LinearLayout 下拉时如何启用 SwipeRefreshLayout 的刷新?

【问题讨论】:

    标签: android pull-to-refresh swiperefreshlayout


    【解决方案1】:

    如果要在 LinearLayout 上启用 SwipeRefresh,则需要将 LinearLayout 放入 ScrollView:

    <android.support.v4.widget.SwipeRefreshLayout
            android:id="@+id/swipe_refresh_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        <ScrollView
            android:id="@+id/scrollView"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <LinearLayout
                android:id="@+id/linearLayout"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
    
                // insert layout components here ...
    
            </LinearLayout>
        </ScrollView>
    </android.support.v4.widget.SwipeRefreshLayout>
    

    【讨论】:

      【解决方案2】:

      试试这个代码sn-p

      recycler_view.addOnScrollListener(new RecyclerView.OnScrollListener() {
                  @Override
                  public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                      super.onScrollStateChanged(recyclerView, newState);
                  }
      
                  @Override
                  public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
      
                      swipeRefreshLayout.setEnabled(llm.findFirstCompletelyVisibleItemPosition() == 0);
                  }
              });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-12-04
        • 2023-03-12
        • 1970-01-01
        • 2021-12-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多