Android SwipeRefreshLayout - 向下滑动以刷新但不向下移动视图下拉

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23341735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:01:58  来源:igfitidea点击:

SwipeRefreshLayout - swipe down to refresh but not move the view pull down

androidandroid-layoutswiperefreshlayout

提问by Jongz Puangput

Is it possible ?? as a Title said

是否可以 ??正如标题所说

swipe down to refresh Layout but stick the Layout not move it down along swipe gusture

向下滑动以刷新布局但坚持布局而不是沿着滑动手势向下移动

Thank you - I'm using SwipeRefreshLayout

谢谢 - 我正在使用 SwipeRefreshLayout

回答by Cabezas

You try this way

你试试这个方法

listView.setOnScrollListener(new OnScrollListener() {

@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}

@Override
public void onScroll(AbsListView view, int firstVisibleItem,
        int visibleItemCount, int totalItemCount) {
    boolean enable = false;
    if(listView != null && listView.getChildCount() > 0){
        // check if the first item of the list is visible
        boolean firstItemVisible = listView.getFirstVisiblePosition() == 0;
        // check if the top of the first item is visible
        boolean topOfFirstItemVisible = listView.getChildAt(0).getTop() == 0;
        // enabling or disabling the refresh layout
        enable = firstItemVisible && topOfFirstItemVisible;
    }
    swipeRefreshLayout.setEnabled(enable);
}});

This code doesn't work if listView has paddingTop

如果 listView 有 paddingTop,则此代码不起作用

回答by Jongz Puangput

After my trial and error, I found one solution from

经过反复试验,我找到了一个解决方案

http://www.dailydevbook.de/android-swiperefreshlayout-without-overscroll/

http://www.dailydevbook.de/android-swiperrefreshlayout-without-overscroll/

For people who can implement SwipeRefreshLayout, in order to achieve it

对于可以实现 SwipeRefreshLayout 的人,为了实现它



STEP 1: DOWNLOADandroid-support v4 (Open Source) from github

第 1:从 github下载android-support v4(开源)

STEP 2: COPYfollowing java class to your project src

第 2:将以下 java 类复制到您的项目 src

  • SwipeRefreshLayout
  • SwipeProgressBar
  • BakedBezierInterpolator
  • 滑动刷新布局
  • 滑动进度条
  • 烘焙贝塞尔插值器

note1- (Refactor SwipeRefreshLayout to mySwipeRefreshLayout to prevent confusing with original) note2- (Fix these classes and use the source from each other instead of v4)

note1-(将 SwipeRefreshLayout 重构为 mySwipeRefreshLayout 以防止与原始版本混淆) note2-(修复这些类并使用彼此的源而不是 v4)

STEP 3: UPDATE CODEuse

第 3更新代码使用

  • mySwipeRefreshLayout instead of
  • SwipeRefreshLayout
  • mySwipeRefreshLayout而不是
  • 滑动刷新布局

STEP 4: UPDATE LAYOUTuse

第 4更新布局使用

  • com.yourpackage.mySwipeRefreshLaout instead of
  • android.support.v4.widget.SwipeRefreshLayout
  • com.yourpackage.mySwipeRefreshLaout而不是
  • android.support.v4.widget.SwipeRefreshLayout

STEP 5: In your mySwipeRefreshLayout.java, find and changeto a following code

第 5 步:在您的 mySwipeRefreshLayout.java 中,找到并更改为以下代码

private  void  updateContentOffsetTop ( int  targetTop) {
        final  int  currentTop = mTarget.getTop ();
        if  (targetTop> mDistanceToTriggerSync) {
            targetTop = ( int ) mDistanceToTriggerSync;
        } else  if  (targetTop < 0 ) {
            targetTop = 0 ;
        }
        // SetTargetOffsetTopAndBottom (targetTop - currentTop);
        setTargetOffsetTopAndBottom ( 0 ); // MOD: Prevent Scroll Down Animation
    }

回答by Alex Vasilkov

Thanks to Jongz Puangputfor the workaround, but I want to suggest another solution, that I find simplier.

感谢Jongz Puangput的解决方法,但我想建议另一种解决方案,我觉得更简单。

In order to prevent pull down of the SwipeRefreshLayout's child you can provide your own child view with overriden offsetTopAndBottommethod like this:

为了防止下拉 SwipeRefreshLayout 的子视图,您可以使用如下覆盖offsetTopAndBottom方法提供自己的子视图:

public class FixedRelativeLayout extends RelativeLayout {

    ...

    @Override
    public void offsetTopAndBottom(int offset) {
        // Ignoring movement from SwipeRefreshLayout
        super.offsetTopAndBottom(0);
    }

}

That works for me, hope it'll help anybody.

这对我有用,希望它可以帮助任何人。

回答by Noman

I was also facing same issue. try this:

我也面临同样的问题。尝试这个:

guidesList.setOnScrollListener(new AbsListView.OnScrollListener() {  
    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int   visibleItemCount, int totalItemCount) {

        int topRowVerticalPosition = (guidesList == null || 
            guidesList.getChildCount() == 0) ? 0 : guidesList.getChildAt(0).getTop();

        swipeContainer.setEnabled(firstVisibleItem == 0 && topRowVerticalPosition >= 0);
    }
});

回答by Angga Ari Wijaya

You can find out the recycler view currently at the top and by knowing that you can disable or enable SwipeRefreshLayout, let me give an example

您可以找到当前位于顶部的回收器视图,并且知道您可以禁用或启用 SwipeRefreshLayout,让我举个例子

let's say you use linear layout manager or grid then you can find out the recycler position from there

假设您使用线性布局管理器或网格,然后您可以从那里找到回收站的位置

        LinearLayoutManager mLayoutManager;
        if (mColumnCount <= 1) {
            mLayoutManager = new LinearLayoutManager(getBaseContext());
        } else {
            mLayoutManager = new GridLayoutManager(getBaseContext(), mColumnCount);
        }

        recyclerAdapter = new RecyclerViewAdapter(allData);
        mRecycler.setAdapter(recyclerAdapter);
        mRecycler.setLayoutManager(layoutManager);

        if (mLayoutManager.findFirstCompletelyVisibleItemPosition() == 0) {
            swipeRefreshLayout.setEnabled(true);
        }
        else{
            swipeRefreshLayout.setEnabled(false);
        }

so your recycler scroll will work fine and if reach the top the swipeRefreshLayout handle the refresh event...

所以你的回收器滚动会正常工作,如果到达顶部 swipeRefreshLayout 处理刷新事件......