版權聲明:本文爲博主原創文章,遵循 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