gpt4 book ai didi

android - SwipeRefreshLayout 阻止水平滚动的 RecyclerView

转载 作者:太空狗 更新时间:2023-10-29 15:45:32 25 4
gpt4 key购买 nike

我的设置很简单:

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

<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="220dp"/>

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

onCreate()的内容:

layoutManager = new LinearLayoutManager( this );
layoutManager.setOrientation( LinearLayoutManager.HORIZONTAL );
topTopicRecyclerView.setLayoutManager( layoutManager );

现在,当我向左或向右滑动 recyclerView 并且滑动角度不是完全水平时,SwipeRefreshLayout 会跳入并接管滚动控件。这会导致 recyclerView 内部出现恼人的视觉“打嗝”。

如果 SwipeRefreshLayout 被禁用,一切都很好。

那么,如何才能停用 SwipeRefreshLayout 对 RecyclerView 区域的滚动控制?

最佳答案

根据 this discussion about SRL and HorizontalScrollView ,我为 SwipeRefreshLayout 创建了副本:

public class OnlyVerticalSwipeRefreshLayout extends SwipeRefreshLayout {

private int touchSlop;
private float prevX;
private boolean declined;

public OnlyVerticalSwipeRefreshLayout( Context context, AttributeSet attrs ) {
super( context, attrs );
touchSlop = ViewConfiguration.get( context ).getScaledTouchSlop();
}

@Override
public boolean onInterceptTouchEvent( MotionEvent event ) {
switch( event.getAction() ){
case MotionEvent.ACTION_DOWN:
prevX = MotionEvent.obtain( event ).getX();
declined = false; // New action
break;

case MotionEvent.ACTION_MOVE:
final float eventX = event.getX();
float xDiff = Math.abs( eventX - prevX );
if( declined || xDiff > touchSlop ){
declined = true; // Memorize
return false;
}
break;
}
return super.onInterceptTouchEvent( event );
}
}

以及在 XML 中的用法:

<com.commons.android.OnlyVerticalSwipeRefreshLayout
android:id="@+id/swiperefresh"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<tags/>

</com.commons.android.OnlyVerticalSwipeRefreshLayout>

关于android - SwipeRefreshLayout 阻止水平滚动的 RecyclerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34136178/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com