另辟蹊径,如何完美实现任何控件自动下拉刷新!!!!

进入一个页面自动下拉刷新,数据请求下来结束,这是今天产品提出来的一个要求。

一般来说,下拉刷新都是重写事件分发来实现动态下拉刷新(绝大多数框架这么实现),要么就是listview的头布局之类的。

很少有一开始就考虑到这种自动下拉刷新的需求的,我这也是。

其实如果产品一开始就提出来,网上还是有很多框架的,虽然兼容性不如传统老的下拉刷新框架,但是还是改吧改吧能用。

不过我不推荐,我们是有节操有理想的程序员,没有自己搞一个不就行了?


一开始我的想法是直接改了现有引用的下拉框架,结果发现很难改,(我半路接手的项目)按这个思路我思考了一天,

看懂一个完美的开源框架还是很费事的,而且还是基于事件分发来实现的。说实话特别特别痛苦,已经追到代码了,居然

没有滑动事件就不会有下拉事件,坑爹了,整个重写代码逻辑肯定是不行的,这个框架已经引用很多次了(整个项目的下拉刷新都是用的这个)

怎么办???


这里我换了一种思路来实现这个需求,我根本不用去找到实现这个下拉事件,我只要能获得这个下拉事件的数据展现出来不就行了?


基于这种思路,我首先取出了框架的头布局,将其设置到我需要实现自动下拉的布局,这里特别注意是插入到你引用的框架布局或者你自己写的下拉布局的前面位置

<RelativeLayout
    android:id="@+id/pull_to_refresh_header"
    android:background="@android:color/transparent"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:paddingBottom="15dip"
    android:paddingTop="10dip">
    <LinearLayout
        android:id="@+id/content"
        android:layout_centerInParent="true"
        android:layout_width="wrap_content"
        android:layout_height="20dp">
        <ProgressBar
            android:id="@+id/pull_to_refresh_progress"
            style="@android:style/Widget.ProgressBar.Small.Inverse"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:indeterminate="true"
            android:gravity="center"
            android:layout_gravity="center"
            android:visibility="gone"
            />
        <ImageView
            android:id="@+id/pull_to_refresh_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center"
            android:src="@drawable/ic_pulltorefresh_arrow"
            android:visibility="visible"/>
        <TextView
            android:id="@+id/pull_to_refresh_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center"
            android:layout_marginLeft="10dp"
            android:textColor="#CCCCCC"
            android:text="下拉刷新"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textStyle="bold"/>
    </LinearLayout>
</RelativeLayout>
于是就有了这种效果



这个头布局目前来说是写死的,下面白色的这一块就是我们需要下拉刷新的listview外面包裹了下拉刷新框架的布局。

现在关键来了如何实现自动下拉?这里我用了动画来实现!!!不错就是最普通的动画,完美的做出了下拉刷新的效果,当然肯定

会有一些逻辑判断在里面。下面贴出关键的动画,具体逻辑根据您的需求来。

/**
 * 下拉刷新动画
 * @param relativeLayout
 * @param pull_to_refresh_image
 * @param pull_to_refresh_text
 * @param pull_to_refresh_progress
 */
public void Animation(final RelativeLayout relativeLayout, final ImageView pull_to_refresh_image, final TextView pull_to_refresh_text, final ProgressBar pull_to_refresh_progress){

    AnimationSet set=new AnimationSet(true);
    TranslateAnimation trans=new TranslateAnimation(TranslateAnimation.RELATIVE_TO_SELF, 0f,
            TranslateAnimation.RELATIVE_TO_SELF, 0f,
            TranslateAnimation.RELATIVE_TO_SELF, 0f, TranslateAnimation.RELATIVE_TO_SELF, 2f);//以y轴原点进行计算
    trans.setDuration(1000);//三秒完成动画
    set.setFillAfter(true);
    set.addAnimation(trans);//增加动画
    trans.setAnimationListener(new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {

        }

        @Override
        public void onAnimationEnd(Animation animation) {
            AnimationSet set = new AnimationSet(true);
            RotateAnimation mFlipAnimation = new RotateAnimation(0, -180,
                    RotateAnimation.RELATIVE_TO_SELF, 0.5f,
                    RotateAnimation.RELATIVE_TO_SELF, 0.5f);
            mFlipAnimation.setDuration(250);
            set.setFillAfter(true);
            set.addAnimation(mFlipAnimation);
            pull_to_refresh_image.startAnimation(set);
        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    });
    final TranslateAnimation trans2=new TranslateAnimation(TranslateAnimation.RELATIVE_TO_SELF, 0f,
            TranslateAnimation.RELATIVE_TO_SELF, 0f,
            TranslateAnimation.RELATIVE_TO_SELF, 0f, TranslateAnimation.RELATIVE_TO_SELF, -2f);//以y轴原点进行计算
    trans2.setDuration(1000);//三秒完成动画
    trans2.setStartOffset(1400);
    set.setFillAfter(true);
    set.addAnimation(trans2);//增加动画
    trans2.setAnimationListener(new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {

        }

        @Override
        public void onAnimationEnd(Animation animation) {
            pull_to_refresh_text.setText("正在加载");
            pull_to_refresh_image.clearAnimation();
            relativeLayout.clearAnimation();
            pull_to_refresh_image.setVisibility(View.GONE);
            pull_to_refresh_progress.setVisibility(View.VISIBLE);

            getHuiShareListData("");//这里是我的网络请求事件
        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    });
    relativeLayout.startAnimation(set);
}
这是一个多种动画融合在一起的结果,要细心不然很容易达不到你想要的想过和报错,这一段大家可以直接考过去用

大家再在自己的实现需求的地方加一些判断就OK了,这就不细写了,每个项目逻辑都不一样最后实现这种效果.。

大体逻辑就是,进入activity先播放动画你下拉刷新是怎么样动画就是怎么样,然后请求网络,成功了之后把这个布局gone掉,自动下拉刷新就实现了



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值