Android的SwipeRefreshLayout如何实现canChildScrollUp如果孩子没有一个ListView或滚动型 [英] Android SwipeRefreshLayout how to implement canChildScrollUp if child is not a ListView or ScrollView

查看:243
本文介绍了Android的SwipeRefreshLayout如何实现canChildScrollUp如果孩子没有一个ListView或滚动型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含其中包含一个滚动视图和另一种观点认为目前当我向下滚动列表,有时不会滚动备份,而不是它运行onRefresh功能相对布局SwipeRefreshLayout。我如何prevent其运行这一点,除非该视图在顶部?我曾读到canChildScrollUp,但没有找到一种方法来正确地执行它。

我的继承人布局

 < XML版本=1.0编码=UTF-8&GT?;

< android.support.v4.widget.SwipeRefreshLayout
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@ + ID / swipe_container
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    >

    < RelativeLayout的
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
         的xmlns:程序=htt​​p://schemas.android.com/apk/res-auto
        >

<滚动型
     机器人:layout_width =match_parent
     机器人:layout_height =match_parent
     机器人:滚动条=无>

    < RelativeLayout的
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:方向=垂直>



        < RelativeLayout的
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:ID =@ + ID /简介
            机器人:背景=@可绘制/ homepage_image_1>

             < com.alldaypa.alldaypa.customviews.CircularImageView

                机器人:layout_width =75dp
                机器人:layout_height =75dp
                应用程序:border_color =#FFFFFF
                应用程序:border_width =8DP
                应用程序:阴影=假
                机器人:ID =@ + ID / profile_image
                机器人:layout_alignParentTop =真
                机器人:layout_alignParentLeft =真
                机器人:layout_marginLeft =70dp
                机器人:layout_marginTop =20dp
                机器人:SRC =@可绘制/ homepage_noimage_male
                机器人:可点击=真/>

           < ImageView的
               机器人:layout_width =WRAP_CONTENT
               机器人:layout_height =WRAP_CONTENT
               机器人:layout_alignBottom =@ + ID / profile_image
               机器人:layout_alignRight =@ + ID / profile_image
               机器人:SRC =@可绘制/ homepage_profile_add
               机器人:layout_marginRight =3DP
               机器人:layout_marginBottom =3DP
           />




            < TextView的机器人:ID =@ + ID / PROFILE_NAME
                机器人:文本=名称
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:TEXTSIZE =20SP
                机器人:layout_alignTop =@ + ID / profile_image
                机器人:layout_toRightOf =@ + ID / profile_image
                机器人:layout_marginTop =20dp
                机器人:文字颜色=@色/ darkgrey
                机器人:layout_marginLeft =10dp/>

            < TextView的机器人:ID =@ + ID /公司
                机器人:文本=公司
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:TEXTSIZE =16SP
                机器人:layout_below =@ + ID / PROFILE_NAME
                机器人:layout_alignLeft =@ + ID / PROFILE_NAME
                机器人:文字颜色=@色/ darkgrey
                机器人:layout_marginTop = -  2DP/>


        < / RelativeLayout的>

            < RelativeLayout的
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:ID =@ + ID /可用性
            机器人:背景=@色/白
            机器人:填充=20dp
            机器人:layout_below =@ + ID / profile文件>


            < TextView的机器人:ID =@ + ID / profile_status_title
            机器人:文本=你当前的可用状态
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:TEXTSIZE =14sp
            机器人:layout_alignParentTop =真
            机器人:layout_centerHorizo​​ntal =真
            机器人:文字颜色=@色/ darkgrey/>

            < TextView的机器人:ID =@ + ID / profile_status
            机器人:文本=可用
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:TEXTSIZE =32sp
            机器人:layout_below =@ + ID / profile_status_title
            机器人:layout_centerHorizo​​ntal =真
            机器人:文字颜色=@色/绿
            机器人:layout_marginTop = -  2DP/>

            < TextView的机器人:ID =@ + ID / profile_status_reason
            机器人:文本=打开:01924 501370
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:TEXTSIZE =24sp
            机器人:layout_below =@ + ID / profile_status
            机器人:layout_centerHorizo​​ntal =真
            机器人:文字颜色=@色/粉红色/>

        < / RelativeLayout的>


        <的TextView
        机器人:ID =@ + ID /邮件
        机器人:layout_width =match_parent
        机器人:layout_height =30dp
        机器人:layout_alignParentRight =真
        机器人:layout_below =@ + ID /可用性
        机器人:ellipsize =结束
        机器人:行=1
        机器人:背景=@色/粉
        机器人:文字颜色=@色/白
        机器人:TEXTSIZE =14sp
        机器人:以下属性来=10dp
        机器人:paddingTop =5DP
        机器人:重力=左
        机器人:文本=消息/>

        < RelativeLayout的
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =FILL_PARENT
            机器人:layout_below =@ + ID /邮件
            机器人:背景=@色/ midgrey>

             < com.alldaypa.alldaypa.customviews.SwipeListView
                的xmlns:刷卡=htt​​p://schemas.android.com/apk/res-auto
                机器人:ID =@ + ID /列表
                机器人:listSelector =#00000000
                机器人:layout_width =FILL_PARENT
                机器人:layout_height =400dp
                刷卡:swipeFrontView =@ + ID /正面
                刷卡:swipeBackView =@ + ID /后退
                刷卡:swipeActionLeft =兜底
                刷卡:swipeMode =左
                刷卡:swipeCloseAllItemsWhenMoveList =真
                刷卡:swipeOpenOnLong preSS =假
                轻扫:swipeAnimationTime =0
                刷卡:swipeOffsetLeft =200dp
                刷卡:swipeOffsetRight =0dp
                机器人:可聚焦=假
            />


                < ImageView的机器人:layout_height =1DP
                机器人:layout_width =match_parent
                机器人:背景=@色/ searchbg
                机器人:layout_below =@ + ID /列表
                机器人:layout_alignLeft =@ + ID /列表
                机器人:ID =@ + ID /线路


                />

           <按钮
                机器人:layout_width =match_parent
                机器人:layout_height =60dp
                机器人:背景=@可绘制/列表项
                机器人:文字颜色=@色/ darkgrey
                机器人:TEXTSIZE =18sp
                机器人:文本=查看所有邮件
                机器人:layout_below =@ + ID /线路
                机器人:重力=中心
                机器人:layout_centerVertical =真
                机器人:ID =@ + ID / viewall
                机器人:layout_centerHorizo​​ntal =真
              />

        < / RelativeLayout的>
< / RelativeLayout的>



< /滚动型>

<包括布局=@布局/ elv_undo_popup机器人:ID =@ + ID /撤销机器人:知名度=水涨船高/>
< / RelativeLayout的>

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

和继承人在那里我已经实现了我的课

  swipeLayout =(SwipeRefreshLayout)v.findViewById(R.id.swipe_container);
        swipeLayout.setColorSchemeColors(Color.parseColor(#ed037c),Color.parseColor(#c40053),Color.parseColor(#FFFFFF),Color.parseColor(#51af50));

        swipeLayout.setOnRefreshListener(新SwipeRefreshLayout.OnRefreshListener(){
            @覆盖公共无效onRefresh(){
                swipeLayout.setRefreshing(真正的);
                Log.d(刷卡,刷新数);
                (新处理程序())。postDelayed(新可运行(){
                    @覆盖公共无效的run(){

                        updateLocalData();

                    }
                    },3000);
                }
            });
 

解决方案

由于@Twibit我找到了一种方法,使这项工作。关键是要检测的滚动型的位置,如果该位置为0化妆设置SwipeRefreshLayout启用其他禁用。

  scrollView.getViewTreeObserver()。addOnScrollChangedListener(新OnScrollChangedListener(){

            @覆盖
            公共无效onScrollChanged(){
                INT scrollY = scrollView.getScrollY();
                如果(scrollY == 0)swipeLayout.setEnabled(真正的);
                否则swipeLayout.setEnabled(假);

            }
});
 

Hi i have a SwipeRefreshLayout that contains a relative layout which contains a scroll view and another view currently when i scroll down the list sometimes wont scroll back up instead it runs the onRefresh function. How do i prevent it from running this unless the view is at the top? i had read about canChildScrollUp but not found a way to implement it correctly.

heres my layout

<?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>

and heres where i have implemented my class

swipeLayout = (SwipeRefreshLayout)v.findViewById(R.id.swipe_container);
        swipeLayout.setColorSchemeColors(Color.parseColor("#ed037c"), Color.parseColor("#c40053"),Color.parseColor("#ffffff"), Color.parseColor("#51af50"));

        swipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { 
            @Override public void onRefresh() { 
                swipeLayout.setRefreshing(true);
                Log.d("Swipe", "Refreshing Number"); 
                ( new Handler()).postDelayed(new Runnable() { 
                    @Override public void run() { 

                        updateLocalData();

                    } 
                    }, 3000); 
                }
            });

解决方案

thanks to @Twibit i found a way to make this work. the trick is to detect the position of the scrollview if the position is 0 make set the SwipeRefreshLayout to be enabled else disabled.

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

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

            }
});

这篇关于Android的SwipeRefreshLayout如何实现canChildScrollUp如果孩子没有一个ListView或滚动型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆