安卓项目实战之刷新显示数据变化动画

效果:

原文查看:https://blog.csdn.net/calvin_zhou/article/details/79253709
在这里插入图片描述
一般的App在刷新之后会显示本次刷新,增加了多少内容或更新,对于新闻类或直播类,比较常见。那么,这个效果如何实现呢?
下拉刷新,得到服务器的最新数据后,会得到数据集合的大小与原来的比较,得到所需的值或者获取更新数目的这个工作由服务器来完成。
接下来就是通过动画来显示数据,动画可以自己实现,本次借助于第三方类库ViewAnimator实现炫酷的特效,刷新使用SmartRefresh。

实现步骤:

1.添加依赖

//刷新-SmartRefreshLayout
compile 'com.scwang.smartrefresh:SmartRefreshLayout:1.0.4-7'
//流畅的Android动画库
compile 'com.github.florent37:viewanimator:1.0.5@aar'

2.activity代码:

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.rl_top_toast)
    RelativeLayout rlTopToast;
    @BindView(R.id.tv_toast)
    TextView tvToast;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        setListener();
    }

    private void setListener() {
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                showToast(new Random().nextInt(10) + 1);
                refreshlayout.finishRefresh();
            }
        });
    }

    private void showToast(int num) {
       // 此处使用String.format为单个占位符赋值
        tvToast.setText(String.format(getResources().getString(R.string.live_toast), num + ""));
        rlTopToast.setVisibility(View.VISIBLE);
        ViewAnimator.animate(rlTopToast)
                .newsPaper()
                .duration(2000)
                .start()
                .onStop(new AnimationListener.Stop() {
                    @Override
                    public void onStop() {
                        ViewAnimator.animate(rlTopToast)
                                .bounceOut()
                                .duration(2000)
                                .start();
                    }
                });
    }
}

3.layout布局文件如下:

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

<com.scwang.smartrefresh.layout.SmartRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/refreshLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:srlEnableLoadmore="true">

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

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Hello World!" />

        <RelativeLayout
            android:id="@+id/rl_top_toast"
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:background="#D6E9F6"
            android:visibility="gone">

            <TextView
                android:id="@+id/tv_toast"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:text="@string/live_toast"
                android:textColor="#3393D5"
                android:textSize="12sp" />
        </RelativeLayout>

    </RelativeLayout>
</com.scwang.smartrefresh.layout.SmartRefreshLayout>
<string name="live_toast">已为您推荐了%1$s条新资讯</string>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值