国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android帶數字或紅點的底部導航攔和聯網等待加載動畫示例

2019-12-12 03:31:39
字體:
來源:轉載
供稿:網友

Android帶數字或紅點的底部導航攔和聯網等待加載動畫

首先展示一下截圖效果,下載地址在文章最后

一、Android帶紅點的底部導航攔

1.首先寫底部導航欄的界面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"><RelativeLayoutandroid:id="@+id/rl_1"android:layout_width="0dp"android:layout_height="match_parent"android:layout_marginTop="4dp"android:layout_weight="1"><RadioButtonandroid: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="首頁"android:textColor="@drawable/tab_text_selector"android:textSize="10sp" /><TextViewandroid: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><RelativeLayoutandroid:id="@+id/rl_2"android:layout_width="0dp"android:layout_height="match_parent"android:layout_marginTop="4dp"android:layout_weight="1"android:focusable="true"><RadioButtonandroid: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" /><TextViewandroid: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><RelativeLayoutandroid:id="@+id/rl_3"android:layout_width="0dp"android:layout_height="match_parent"android:layout_marginTop="4dp"android:layout_weight="1"><RadioButtonandroid: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="進貨單"android:textColor="@drawable/tab_text_selector"android:textSize="10sp" /><TextViewandroid: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><RelativeLayoutandroid:id="@+id/rl_4"android:layout_width="0dp"android:layout_height="match_parent"android:layout_marginTop="4dp"android:layout_weight="1"><RadioButtonandroid: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="會員"android:textColor="@drawable/tab_text_selector"android:textSize="10sp" /><TextViewandroid: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.修改底部導航欄的數字,在MainActivity中

 /** * -1:表示沒有新消息 * -2:表示新消息用紅點的方式顯示 * 0-99:直接顯示數字 * >=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中的底部導航攔,所以,要在MainActivity中,寫一些公用的方法。

 /** * 在oneFragment中更新,底部導航欄的數字 * @param num */public void updateOne(int num){messageTips(num,tv_1);}/** * 在TwoFragment中更新,底部導航欄的數字 * @param num */public void updateTwo(int num){messageTips(num,tv_2);}/** * 在ThreeFragment中更新,底部導航欄的數字 * @param num */public void updateThree(int num){messageTips(num,tv_3);}/** * 在FourFragment中更新,底部導航欄的數字 * @param num */public void updateFour(int num){messageTips(num,tv_4);}

4.在fragment中修改底部導航攔,得到主頁面,調用主頁面的修改方法。

mActivity = (MainActivity) getActivity();number++;mActivity.updateTwo(number);

二、activity加載動畫。

1.activity中的加載動畫,要寫一個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"/><FrameLayoutandroid: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的布局里,包含了一個loading_anim的布局,這就是加載布局。加載布局,里面氛圍三個線性布局,分別是:加載中布局,加載錯誤布局,沒有數據布局,其中加載失敗布局,還需要點擊重新加載。內容如下:

<?xml version="1.0" encoding="utf-8"?><FrameLayoutxmlns: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"><ImageViewandroid:id="@+id/iv_loading"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/loading_everyday" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="15dp"android:text="正在為您開啟干貨推薦.."android:textColor="@color/colorTitle"android:textSize="14sp" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="7dp"android:text="看的越多,推薦越準"android:textColor="@color/colorSubtitle"android:textSize="12sp"android:visibility="visible" /></LinearLayout><!--加載失敗--><LinearLayoutandroid:id="@+id/ll_error_refresh"android:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center"android:orientation="vertical"android:visibility="gone"><ImageViewandroid:id="@+id/img_err"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/load_err" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="15dp"android:text="加載失敗,點擊重試"android:textSize="15sp" /></LinearLayout><!--加載失敗--><LinearLayoutandroid:id="@+id/ll_no_data"android:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center"android:orientation="vertical"android:visibility="gone"><ImageViewandroid:id="@+id/img_no_data"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/load_err" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="15dp"android:text="sorry,沒有您想要的數據"android:textSize="15sp" /></LinearLayout></FrameLayout>

2.Baseactivity的代碼太多,講一下主要的,重寫setContentView方法,把新布局放入id為fl_content的布局中,調用getWindow()。setContentView(rootView);剩下的就跟普通個activity操作一樣了。

@Overridepublic 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);//contentFrameLayout.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,當需要加載成功是,調用loadSuccess()方放,加載失敗時調用loadError(),失敗后重新加載,需要調用reLoading()重新加載,并調用onRefresh()重新加載數據。如果沒有數據調用noData()

三、fragment中加載動畫,把加載布局,放入fragment中我暫時沒有好的辦法提出BaseFragment進行統一加載。有一些注意事項。

1.viewpager進行布局加載時,最好能夠預加載一個屏幕的數據。

 vp_main.setOffscreenPageLimit(3);//最好是一屏能顯示的fragment數-1。

2.在BaseFragment重寫setUserVisibleHint方法,當fragment可見時,才聯網加載數據。

@Overridepublic void setUserVisibleHint(boolean isVisibleToUser) {super.setUserVisibleHint(isVisibleToUser);if (getUserVisibleHint()){isVisible=true;onVisible();}else {isVisible=false;onInvisible();}}

3.fragment繼承BaseFragment需要在onViewCreated中調用一下聯網加載方法,因為,setUserVisibleHint執行比較靠前,頁面還沒有添加到布局,就加載數據,會造成填充數據失敗,需要當頁面完全添加到布局中,再聯網請求。

 @Overridepublic void onViewCreated(View view, @Nullable Bundle savedInstanceState) {super.onViewCreated(view, savedInstanceState);mActivity= (MainActivity) getActivity();showLoading();lazyLoad();}

下載地址:LoadingProject_jb51.rar

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 江西省| 定安县| 饶河县| 诸城市| 类乌齐县| 巴林右旗| 隆昌县| 绥阳县| 武城县| 南雄市| 北京市| 荥阳市| 双峰县| 乌海市| 铜川市| 陇南市| 临西县| 翁牛特旗| 永靖县| 北海市| 彭州市| 大关县| 三明市| 郎溪县| 尖扎县| 新泰市| 南汇区| 潢川县| 印江| 邻水| 宣恩县| 天等县| 海安县| 和龙市| 波密县| 杨浦区| 余庆县| 铁力市| 漯河市| 越西县| 隆化县|