Android SwipeRefreshLayout如果子项不是ListView或ScrollView,如何实现canChildScrollUp

Android SwipeRefreshLayout如果子项不是ListView或ScrollView,如何实现canChildScrollUp,android,Android,您好,我有一个SwipeRefreshLayout,其中包含一个相对布局,其中包含一个滚动视图和另一个视图。当我向下滚动列表时,有时不会向上滚动,而是运行onRefresh功能。除非视图位于顶部,否则如何防止它运行此操作?我读过canChildScrollUp,但没有找到正确实施它的方法 这是我的布局 <?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.SwipeRefreshLayout

您好,我有一个SwipeRefreshLayout,其中包含一个相对布局,其中包含一个滚动视图和另一个视图。当我向下滚动列表时,有时不会向上滚动,而是运行onRefresh功能。除非视图位于顶部,否则如何防止它运行此操作?我读过canChildScrollUp,但没有找到正确实施它的方法

这是我的布局

<?xml version="1.0" encoding="utf-8"?>

<android.support.v4.widget.SwipeRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swipe_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <RelativeLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
         xmlns:app="http://schemas.android.com/apk/res-auto"
        >

<ScrollView 
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:scrollbars="none">

    <RelativeLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >



        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/profile"
            android:background="@drawable/homepage_image_1">        

             <com.alldaypa.alldaypa.customviews.CircularImageView

                android:layout_width="75dp"
                android:layout_height="75dp"
                app:border_color="#FFFFFF"
                app:border_width="8dp"
                app:shadow="false"
                android:id="@+id/profile_image"
                android:layout_alignParentTop="true"
                android:layout_alignParentLeft="true"
                android:layout_marginLeft="70dp"
                android:layout_marginTop="20dp"
                android:src="@drawable/homepage_noimage_male"
                android:clickable="true"  />

           <ImageView 
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_alignBottom="@+id/profile_image"
               android:layout_alignRight="@+id/profile_image"
               android:src="@drawable/homepage_profile_add"
               android:layout_marginRight="3dp"
               android:layout_marginBottom="3dp"
           />




            <TextView android:id="@+id/profile_name"
                android:text="Name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="20sp"
                android:layout_alignTop="@+id/profile_image"
                android:layout_toRightOf="@+id/profile_image"
                android:layout_marginTop="20dp"
                android:textColor="@color/darkgrey"
                android:layout_marginLeft="10dp"/>

            <TextView android:id="@+id/company"
                android:text="Company"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="16sp"
                android:layout_below="@+id/profile_name"
                android:layout_alignLeft="@+id/profile_name"
                android:textColor="@color/darkgrey"
                android:layout_marginTop="-2dp"/>


        </RelativeLayout>

            <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/availability"
            android:background="@color/white"
            android:padding="20dp"
            android:layout_below="@+id/profile">        


            <TextView android:id="@+id/profile_status_title"
            android:text="Your current availability status:"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="14sp"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:textColor="@color/darkgrey"/>

            <TextView android:id="@+id/profile_status"
            android:text="Available"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="32sp"
            android:layout_below="@+id/profile_status_title"
            android:layout_centerHorizontal="true"
            android:textColor="@color/green"
            android:layout_marginTop="-2dp"/>

            <TextView android:id="@+id/profile_status_reason"
            android:text="On: 01924 501370"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="24sp"
            android:layout_below="@+id/profile_status"
            android:layout_centerHorizontal="true"
            android:textColor="@color/pink"/>

        </RelativeLayout>


        <TextView
        android:id="@+id/messages"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/availability"
        android:ellipsize="end"
        android:lines="1"
        android:background="@color/pink"
        android:textColor="@color/white"
        android:textSize="14sp" 
        android:paddingLeft="10dp"
        android:paddingTop="5dp"
        android:gravity="left"
        android:text="Messages" />

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_below="@+id/messages"
            android:background="@color/midgrey">

             <com.alldaypa.alldaypa.customviews.SwipeListView
                xmlns:swipe="http://schemas.android.com/apk/res-auto"
                android:id="@+id/list"
                android:listSelector="#00000000"
                android:layout_width="fill_parent"
                android:layout_height="400dp"
                swipe:swipeFrontView="@+id/front"
                swipe:swipeBackView="@+id/back"
                swipe:swipeActionLeft="reveal"
                swipe:swipeMode="left"
                swipe:swipeCloseAllItemsWhenMoveList="true"
                swipe:swipeOpenOnLongPress="false"
                swipe:swipeAnimationTime="0"
                swipe:swipeOffsetLeft="200dp"
                swipe:swipeOffsetRight="0dp"
                android:focusable="false"
            />


                <ImageView android:layout_height="1dp"
                android:layout_width="match_parent"
                android:background="@color/searchbg"
                android:layout_below="@+id/list"
                android:layout_alignLeft="@+id/list"
                android:id="@+id/line"


                />

           <Button
                android:layout_width="match_parent"
                android:layout_height="60dp"
                android:background="@drawable/listitem"
                android:textColor="@color/darkgrey"
                android:textSize="18sp"
                android:text="View all messages"
                android:layout_below="@+id/line"
                android:gravity="center"
                android:layout_centerVertical="true"
                android:id="@+id/viewall"
                android:layout_centerHorizontal="true"
              />

        </RelativeLayout>
</RelativeLayout>



</ScrollView>

<include  layout="@layout/elv_undo_popup" android:id="@+id/undo" android:visibility="gone"/> 
</RelativeLayout>

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

如果滚动视图位于或不位于顶部,则可能必须启用/禁用滑动布局

您可以查看此链接以获取灵感:

另一个可能对您有所帮助的示例:


我在使用水平滚动时遇到了另一个问题:

多亏了@Twibit,我找到了一种方法来实现这一点。诀窍是检测scrollview的位置,如果位置为0,则将SwiperFreshLayout设置为enabled或disabled

scrollView.getViewTreeObserver().addOnScrollChangedListener(new OnScrollChangedListener() {

            @Override
            public void onScrollChanged() {
                int scrollY = scrollView.getScrollY(); 
                if(scrollY == 0) swipeLayout.setEnabled(true);
                else swipeLayout.setEnabled(false);

            }
});
  • 扩展SwipeRefreshLayout并覆盖方法
    canChildScrollUp()
    ,该方法确定视图是否可以向上滚动

    //if you have several scrollable views,just iterate
    for (View view : ...) {
        if (view != null && view.isShown() && !canViewScrollUp(view)) {
            // If the view is shown, and can not scroll upwards, return false and start the 
            // gesture. 
            return false; 
        } 
    return true; 
    
  • 迭代浏览视图,并使用方法
    canviewcrollup()
    检查是否有任何视图无法向上滚动

    private static boolean canViewScrollUp(View view) {
        if (android.os.Build.VERSION.SDK_INT >= 14) {
            // For ICS and above we can call canScrollVertically() to determine this 
            return ViewCompat.canScrollVertically(view, -1);
        } else { 
            if (view instanceof AbsListView) {
            // Pre-ICS we need to manually check the first visible item and the child view's top 
            // value 
            final AbsListView listView = (AbsListView) view;
            return listView.getChildCount() > 0 &&
                (listView.getFirstVisiblePosition() > 0
                        || listView.getChildAt(0).getTop() < listView.getPaddingTop());
        } else { 
            // For all other view types we just check the getScrollY() value 
            return view.getScrollY() > 0;
        } 
    } 
    
    private静态布尔canViewScrollUp(视图){
    如果(android.os.Build.VERSION.SDK_INT>=14){
    //对于ICS及以上版本,我们可以调用canScrollVertical()来确定这一点
    返回ViewCompat.CanScrollVertical(视图,-1);
    }否则{
    if(查看AbsListView的实例){
    //在ICS之前,我们需要手动检查第一个可见项和子视图的顶部
    //价值观
    最终AbsListView listView=(AbsListView)视图;
    返回listView.getChildCount()>0&&
    (listView.getFirstVisiblePosition()>0
    ||getChildAt(0.getTop()0;
    } 
    } 
    

  • 简短版本:
    scrollView.getViewTreeObserver().addOnScrollChangedListener(新OnScrollChangedListener(){@Override public void onScrollChanged(){swipeLayout.setEnabled(scrollView.getScrollY()==0);};
    如果我们用ListView替换上面的ScrollView,原则上解决方案仍然有效,但是onScrollChanged()方法内部的检查应该是:ListView.getChildAt(0.getTop()==0回答得很好!动态修复了我的问题:)这太糟糕了,先生:)
    private static boolean canViewScrollUp(View view) {
        if (android.os.Build.VERSION.SDK_INT >= 14) {
            // For ICS and above we can call canScrollVertically() to determine this 
            return ViewCompat.canScrollVertically(view, -1);
        } else { 
            if (view instanceof AbsListView) {
            // Pre-ICS we need to manually check the first visible item and the child view's top 
            // value 
            final AbsListView listView = (AbsListView) view;
            return listView.getChildCount() > 0 &&
                (listView.getFirstVisiblePosition() > 0
                        || listView.getChildAt(0).getTop() < listView.getPaddingTop());
        } else { 
            // For all other view types we just check the getScrollY() value 
            return view.getScrollY() > 0;
        } 
    }