版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处连接和本声明。
本文连接:https://blog.csdn.net/chang1611/article/details/100162947
最近在作一款app,因为数据加载量比较大,因此加载时长可能比其余app加载速度稍微慢点, 因此加载动画就少不了,可是加载动画感受效果不是很理想,最后就作了数据预加载处理,大体思路就是这样的,当数据没有加载出来时候,显示一个布局,布局大体和数据加载完成以后的效果一致,而后给view加上动画效果,一个超棒的数据加载动画就搞定了,再也不啰嗦,直接上代码。android
1.空布局app
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white">ide
<LinearLayout
android:layout_width="@dimen/dp_0"
android:layout_height="@dimen/dp_75"
android:layout_alignParentLeft="true"
android:layout_marginTop="@dimen/dp_5"
android:layout_marginBottom="@dimen/dp_5"
android:layout_weight="7"
android:gravity="center_vertical"
android:orientation="vertical">布局
<TextView
android:id="@+id/txt_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="@dimen/dp_4"
android:background="@color/eeeeee"
android:ellipsize="end"
android:lineSpacingExtra="@dimen/dp_7"
android:maxLines="2"
android:text=""
android:textColor="@color/black"
android:textFontWeight="10"
android:textSize="@dimen/sp_16" />动画
<TextView
android:id="@+id/txt_time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dp_4"
android:layout_marginRight="@dimen/dp_4"
android:background="@color/eeeeee"
android:maxLines="1"
android:text=""
android:textColor="@color/bfbfbf"
android:textSize="@dimen/sp_12" />.net
<TextView
android:id="@+id/txt_time2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dp_4"
android:layout_marginRight="@dimen/dp_4"
android:background="@color/eeeeee"
android:maxLines="1"
android:text=""
android:textColor="@color/bfbfbf"
android:textSize="@dimen/sp_12" />
</LinearLayout>xml
<com.jjb.weduywb.widget.RoundImage
android:id="@+id/iv_big"
android:layout_width="@dimen/dp_0"
android:layout_height="@dimen/dp_75"
android:layout_alignParentRight="true"
android:layout_marginTop="@dimen/dp_5"
android:layout_marginRight="@dimen/dp_4"
android:layout_marginBottom="@dimen/dp_5"
android:layout_weight="3"
android:background="@color/eeeeee"
android:scaleType="centerCrop"
android:visibility="visible" />
</LinearLayout>
2.adapter定义两个变量一个表示有数据的布局,一个表示无数据的布局blog
private static final int TYPE_ONLY_FONT = 0;//文字ip
private static final int EMPTY_VIEW = -1;//空布局
3.重写adaper中的getItemViewType的方法utf-8
@Override
public int getItemViewType(int position) {
if (xx) {
return TYPE_ONLY_FONT;
}else{
return EMPTY_VIEW;
}
}
4.在adapter中
onCreateViewHolder判断加载布局
if (viewType == TYPE_ONLY_FONT ) {
有数据的布局
} else if (viewType == EMPTY_VIEW) {
无数据的布局
}
5.
onBindViewHolder在他中加载数据
if (holder instanceof 有数据HoldView) {
有数据HoldView
} else if (holder instanceof 无数据HoldView) {
无数据HoldView
}
6.
getItemCount
中判断数据是否为空人而后加载数据,若是为空默认加载上几条数据,否则加载效果没法出现
7.空数据中页面显示
private class NoItemHoldView extends RecyclerView.ViewHolder {
TextView txtTitle;
TextView txtTime;
TextView txtTime2;
RoundImage imageView;
public NoItemHoldView(View itemView) {
super(itemView);
txtTitle = itemView.findViewById(R.id.txt_title);
txtTime = itemView.findViewById(R.id.txt_time);
txtTime2 = itemView.findViewById(R.id.txt_time2);
imageView = itemView.findViewById(R.id.iv_big);
alphaAnimation1.setDuration(1000);
alphaAnimation1.setRepeatCount(Animation.INFINITE);
alphaAnimation1.setRepeatMode(Animation.RESTART);
txtTitle.setAnimation(alphaAnimation1);
txtTime.setAnimation(alphaAnimation1);
// txtTime2.setAnimation(alphaAnimation1);
imageView.setAnimation(alphaAnimation1);
alphaAnimation1.start();
} } 到此一个漂亮的加载效果就出现了 ———————————————— 版权声明:本文为CSDN博主「小南胡」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处连接及本声明。 原文连接:https://blog.csdn.net/chang1611/article/details/100162947