Android應(yīng)用中怎么添加一個(gè)聯(lián)網(wǎng)等待加載動(dòng)畫(huà)?很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。
一、Android帶紅點(diǎn)的底部導(dǎo)航攔
1.首先寫(xiě)底部導(dǎo)航欄的界面view_main_tab.xml.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/tab_layout" android:layout_width="match_parent" android:layout_height="56dp" android:layout_alignParentBottom="true" android:orientation="horizontal" android:background="#27282c" > <RelativeLayout android:id="@+id/rl_1" android:layout_width="0dp" android:layout_height="match_parent" android:layout_marginTop="4dp" android:layout_weight="1"> <RadioButton android:id="@+id/rb_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:button="@null" android:background="@null" android:checked="true" android:clickable="false" android:drawablePadding="5dp" android:drawableTop="@drawable/selector_tab_home" android:gravity="center" android:text="首頁(yè)" android:textColor="@drawable/tab_text_selector" android:textSize="10sp" /> <TextView android:id="@+id/tv_1" android:layout_width="16dp" android:layout_height="16dp" android:layout_alignRight="@id/rb_1" android:layout_alignTop="@id/rb_1" android:layout_marginTop="-6dp" android:layout_marginRight="-6dp" android:layout_gravity="right" android:background="@drawable/msg_num_shape" android:clickable="false" android:gravity="center" android:text="3" android:textColor="@color/white_1" android:textSize="10sp" /> </RelativeLayout> <RelativeLayout android:id="@+id/rl_2" android:layout_width="0dp" android:layout_height="match_parent" android:layout_marginTop="4dp" android:layout_weight="1" android:focusable="true"> <RadioButton android:id="@+id/rb_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:background="@null" android:button="@null" android:clickable="false" android:drawablePadding="5dp" android:drawableTop="@drawable/selector_tab_goods_divide" android:gravity="center" android:text="商品" android:textColor="@drawable/tab_text_selector" android:textSize="10sp" /> <TextView android:id="@+id/tv_2" android:layout_width="16dp" android:layout_height="16dp" android:layout_alignRight="@id/rb_2" android:layout_alignTop="@id/rb_2" android:layout_marginTop="-6dp" android:layout_marginRight="-6dp" android:layout_gravity="right" android:background="@drawable/msg_num_shape" android:clickable="false" android:gravity="center" android:text="3" android:textColor="@color/white_1" android:textSize="10sp" /> </RelativeLayout> <RelativeLayout android:id="@+id/rl_3" android:layout_width="0dp" android:layout_height="match_parent" android:layout_marginTop="4dp" android:layout_weight="1"> <RadioButton android:id="@+id/rb_3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:background="@null" android:button="@null" android:clickable="false" android:drawablePadding="5dp" android:drawableTop="@drawable/selector_tab_stock_list" android:gravity="center" android:text="進(jìn)貨單" android:textColor="@drawable/tab_text_selector" android:textSize="10sp" /> <TextView android:id="@+id/tv_3" android:layout_width="16dp" android:layout_height="16dp" android:layout_alignRight="@id/rb_3" android:layout_alignTop="@id/rb_3" android:layout_marginTop="-6dp" android:layout_gravity="right" android:background="@drawable/msg_num_shape" android:clickable="false" android:gravity="center" android:text="3" android:textColor="@color/white_1" android:textSize="10sp" /> </RelativeLayout> <RelativeLayout android:id="@+id/rl_4" android:layout_width="0dp" android:layout_height="match_parent" android:layout_marginTop="4dp" android:layout_weight="1"> <RadioButton android:id="@+id/rb_4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:background="@null" android:button="@null" android:clickable="false" android:drawablePadding="5dp" android:drawableTop="@drawable/selector_tab_member" android:gravity="center" android:text="會(huì)員" android:textColor="@drawable/tab_text_selector" android:textSize="10sp" /> <TextView android:id="@+id/tv_4" android:layout_width="16dp" android:layout_height="16dp" android:layout_alignRight="@id/rb_4" android:layout_alignTop="@id/rb_4" android:layout_marginTop="-6dp" android:layout_marginRight="-6dp" android:layout_gravity="right" android:background="@drawable/msg_num_shape" android:clickable="false" android:gravity="center" android:text="3" android:textColor="@color/white_1" android:textSize="10sp" /> </RelativeLayout> </LinearLayout>
2.修改底部導(dǎo)航欄的數(shù)字,在MainActivity中
/** * -1:表示沒(méi)有新消息 * -2:表示新消息用紅點(diǎn)的方式顯示 * 0-99:直接顯示數(shù)字 * >=100:用99+顯示 */ private void messageTips(int num, TextView tv) { if(num==-1){ tv.setVisibility(View.GONE); }else if(num==-2){ tv.setVisibility(View.VISIBLE); tv.setText(""); RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) tv.getLayoutParams(); layoutParams.height= DensityUtil.dip2px(this,10); layoutParams.width= DensityUtil.dip2px(this,10); tv.setLayoutParams(layoutParams); }else if(num>=0&&num<=99){ tv.setVisibility(View.VISIBLE); tv.setText(num+""); RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) tv.getLayoutParams(); layoutParams.height= DensityUtil.dip2px(this,16); layoutParams.width= DensityUtil.dip2px(this,16); tv.setLayoutParams(layoutParams); }else if(num>=100){ tv.setVisibility(View.VISIBLE); tv.setText("99+"); RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) tv.getLayoutParams(); layoutParams.height= DensityUtil.dip2px(this,16); layoutParams.width= DensityUtil.dip2px(this,16); tv.setTextSize(DensityUtil.sp2px(this,3)); tv.setLayoutParams(layoutParams); }else{ tv.setVisibility(View.GONE); } }
3.需要在fragment中修改MainActivity中的底部導(dǎo)航攔,所以,要在MainActivity中,寫(xiě)一些公用的方法。
/** * 在oneFragment中更新,底部導(dǎo)航欄的數(shù)字 * @param num */ public void updateOne(int num){ messageTips(num,tv_1); } /** * 在TwoFragment中更新,底部導(dǎo)航欄的數(shù)字 * @param num */ public void updateTwo(int num){ messageTips(num,tv_2); } /** * 在ThreeFragment中更新,底部導(dǎo)航欄的數(shù)字 * @param num */ public void updateThree(int num){ messageTips(num,tv_3); } /** * 在FourFragment中更新,底部導(dǎo)航欄的數(shù)字 * @param num */ public void updateFour(int num){ messageTips(num,tv_4); }
4.在fragment中修改底部導(dǎo)航攔,得到主頁(yè)面,調(diào)用主頁(yè)面的修改方法。
mActivity = (MainActivity) getActivity(); number++; mActivity.updateTwo(number);
二、activity加載動(dòng)畫(huà)。
1.activity中的加載動(dòng)畫(huà),要寫(xiě)一個(gè)BaseActivity。布局如下
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_base" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.hrobbie.loadingproject.activity.BaseActivity"> <android.support.v7.widget.Toolbar xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/tool_bar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" app:contentInsetStart="0.0dp" android:background="@color/colorPrimary" app:layout_scrollFlags="enterAlways|scroll" app:popupTheme="@style/AppTheme.PopupOverlay" /> <FrameLayout android:id="@+id/fl_content" android:layout_width="match_parent" android:layout_height="match_parent"> <include layout="@layout/loading_anim"/> </FrameLayout> </LinearLayout>
注意:id為fl_content的FrameLayout的布局里,包含了一個(gè)loading_anim的布局,這就是加載布局。加載布局,里面氛圍三個(gè)線性布局,分別是:加載中布局,加載錯(cuò)誤布局,沒(méi)有數(shù)據(jù)布局,其中加載失敗布局,還需要點(diǎn)擊重新加載。內(nèi)容如下:
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <!--加載中--> <LinearLayout android:id="@+id/ll_loading" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginBottom="90dp" android:gravity="center" android:orientation="vertical" > <ImageView android:id="@+id/iv_loading" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/loading_everyday" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="15dp" android:text="正在為您開(kāi)啟干貨推薦.." android:textColor="@color/colorTitle" android:textSize="14sp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="7dp" android:text="看的越多,推薦越準(zhǔn)" android:textColor="@color/colorSubtitle" android:textSize="12sp" android:visibility="visible" /> </LinearLayout> <!--加載失敗--> <LinearLayout android:id="@+id/ll_error_refresh" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" android:visibility="gone"> <ImageView android:id="@+id/img_err" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/load_err" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="15dp" android:text="加載失敗,點(diǎn)擊重試" android:textSize="15sp" /> </LinearLayout> <!--加載失敗--> <LinearLayout android:id="@+id/ll_no_data" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" android:visibility="gone"> <ImageView android:id="@+id/img_no_data" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/load_err" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="15dp" android:text="sorry,沒(méi)有您想要的數(shù)據(jù)" android:textSize="15sp" /> </LinearLayout> </FrameLayout>
2.Baseactivity的代碼太多,講一下主要的,重寫(xiě)setContentView方法,把新布局放入id為fl_content的布局中,調(diào)用getWindow()。setContentView(rootView);剩下的就跟普通個(gè)activity操作一樣了。
@Override public void setContentView(@LayoutRes int layoutResID) { View rootView = LayoutInflater.from(this).inflate(R.layout.activity_base,null,false); addView = LayoutInflater.from(this).inflate(layoutResID, null, false); //content FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); addView.setLayoutParams(params); fl_content = (FrameLayout) rootView.findViewById(R.id.fl_content); fl_content.addView(addView); getWindow().setContentView(rootView); initView(); showLoading(); }
3.新的activity只需集成BaseActivity,當(dāng)需要加載成功是,調(diào)用loadSuccess()方放,加載失敗時(shí)調(diào)用loadError(),失敗后重新加載,需要調(diào)用reLoading()重新加載,并調(diào)用onRefresh()重新加載數(shù)據(jù)。如果沒(méi)有數(shù)據(jù)調(diào)用noData()
三、fragment中加載動(dòng)畫(huà),把加載布局,放入fragment中,我暫時(shí)沒(méi)有好的辦法提出BaseFragment進(jìn)行統(tǒng)一加載。有一些注意事項(xiàng)。
1.viewpager進(jìn)行布局加載時(shí),最好能夠預(yù)加載一個(gè)屏幕的數(shù)據(jù)。
vp_main.setOffscreenPageLimit(3);//最好是一屏能顯示的fragment數(shù)-1。
2.在BaseFragment重寫(xiě)setUserVisibleHint方法,當(dāng)fragment可見(jiàn)時(shí),才聯(lián)網(wǎng)加載數(shù)據(jù)。
@Override public void setUserVisibleHint(boolean isVisibleToUser) { super.setUserVisibleHint(isVisibleToUser); if (getUserVisibleHint()){ isVisible=true; onVisible(); }else { isVisible=false; onInvisible(); } }
3.fragment繼承BaseFragment需要在onViewCreated中調(diào)用一下聯(lián)網(wǎng)加載方法,因?yàn)椋瑂etUserVisibleHint執(zhí)行比較靠前,頁(yè)面還沒(méi)有添加到布局,就加載數(shù)據(jù),會(huì)造成填充數(shù)據(jù)失敗,需要當(dāng)頁(yè)面完全添加到布局中,再聯(lián)網(wǎng)請(qǐng)求。
@Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); mActivity= (MainActivity) getActivity(); showLoading(); lazyLoad(); }
看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝您對(duì)億速云的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。