Fastandrutils 是一套整理修改整合的android開發經常使用的工具類,經常使用的自定義view控件等。 這樣能夠減小複製粘貼代碼,從而減小重複代碼,也不用爲了一個經常使用的功能去谷歌百度,讓代碼更簡潔,讓開發更高效。 同時但願您的添加完善,讓android開發變得更簡單。java
github地址,感興趣的話,不妨點贊支持下android
打完廣告,進入正題 git
在開發過程當中,最常的就是數據和界面間的交互,例如無數據時的界面展現,網絡不通時的界面展現,對於這些不是正常的數據,咱們都要作一些異常的展現界面,而不是一個空白界面,這樣作一些異常界面的處理,用戶體驗上會更好點。 源碼地址github
代碼有點多,就貼些關鍵的bash
private LinearLayout setdataLay;//設置數據佈局
private View emptyView;//空佈局
private ImageView emptyImg;//空佈局的ImageView
private TextView emptyTv;//空佈局的TextView
private Button emptyBt;//空佈局的Button
private Context context;
複製代碼
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="EmptyLayout">
<attr name="empty_layout" format="reference" />//空界面的layout文件
<attr name="empty_imageView" format="reference" />//空界面imageView的id
<attr name="empty_textView" format="reference" />//空界面textView的id
<attr name="empty_button" format="reference" />//空界面button的id
<attr name="include_layout" format="reference" />//數據展現界面的layout文件
</declare-styleable>
</resources>
複製代碼
private void initView(TypedArray array) {
int emptyViewId = array.getResourceId(FResourcesUtils.getStyleable("EmptyLayout_empty_layout"), FResourcesUtils.getLayoutResources("f_empty_layout"));
int emptyImgId = array.getResourceId(FResourcesUtils.getStyleable("EmptyLayout_empty_imageView"), FResourcesUtils.getIdResources("empty_img"));
int emptyTvId = array.getResourceId(FResourcesUtils.getStyleable("EmptyLayout_empty_textView"), FResourcesUtils.getIdResources("empty_tv"));
int emptyBtId = array.getResourceId(FResourcesUtils.getStyleable("EmptyLayout_empty_button"), FResourcesUtils.getIdResources("empty_bt"));
int dataViewId = array.getResourceId(FResourcesUtils.getStyleable("EmptyLayout_include_layout"), 0);
//獲取空佈局View
emptyView = View.inflate(context, emptyViewId, null);
//獲取空佈局的imageView
emptyImg = (ImageView) emptyView.findViewById(emptyImgId);
emptyTv = (TextView) emptyView.findViewById(emptyTvId);
emptyBt = (Button) emptyView.findViewById(emptyBtId);
setdataLay = new LinearLayout(context);//先添加一個LinearLayout
setdataLay.setOrientation(LinearLayout.VERTICAL);
setdataLay.setVisibility(GONE);
if (dataViewId != 0) {
addChildViewid(dataViewId);//把數據界面添加到LinearLayout裏
}
addView(setdataLay);
addView(emptyView);
}
複製代碼
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/empty_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxHeight="200dp"
android:maxWidth="200dp" />
<TextView
android:id="@+id/empty_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:textSize="16sp" />
<Button
android:id="@+id/empty_bt"
android:layout_width="120dp"
android:layout_height="40dp"
android:layout_marginTop="6dp" />
</LinearLayout>
複製代碼
省略了比較多方法,詳細看源碼網絡
/**
* ImagView 回調
*/
public interface ImgCallBack {
void setImg(ImageView img, int emptyType);
}
複製代碼
/**
* emptyView 回調
*/
public interface ViewCallBack {
void emptyViewCallBack(View view);
}
複製代碼
<cn.hotapk.fastandrutils.widget.FEmptyView
android:id="@+id/empty_lay"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:include_layout="@+layout/data_layout" />
複製代碼
FEmptyView 不能在xml下加子View 只能這樣添加數據界面app
app:include_layout="@+layout/data_layout"
複製代碼
在oncreat() 方法中ide
empty_lay = (FEmptyView) findViewById(R.id.empty_lay);
empty_lay.loadding("正在加載數據。。。");
empty_lay.loaddingFail("加載數據失敗。。。", "點擊刷新", new View.OnClickListener() {
@Override
public void onClick(View v) {
loadding();
}
}, new FEmptyView.ImgCallBack() {
@Override
public void setImg(ImageView img, int emptyType) {
img.setBackgroundResource(R.mipmap.loaddingfail);
if (img.getAnimation() != null)
img.getAnimation().cancel();
}
});
複製代碼
<cn.hotapk.fastandrutils.widget.FEmptyView
android:id="@+id/empty_lay"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
複製代碼
FEmptyView 不能在xml下加子View 只能在oncreat() 中這樣添加數據界面工具
empty_lay.addChildView(childView);
複製代碼
以後的使用第一種佈局
可自定義emptyView
<cn.hotapk.fastandrutils.widget.FEmptyView
android:id="@+id/empty_lay"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:empty_button="@+id/custom_empty_bt"//自定義layout的button的id
app:empty_imageView="@+id/custom_empty_img"//自定義layout的imageView的id
app:empty_textView="@+id/custom_empty_tv"//自定義layout的textView的id
app:empty_layout="@+layout/custom_empty_layout"//自定義的空 layout界面
app:include_layout="@+layout/data_layout" />//展現數據的layout
複製代碼
FEmptyView 一樣不能在xml下加子View 只能這樣添加數據界面
app:include_layout="@+layout/data_layout"
複製代碼
以後的使用同第一種
可深度自定義emptyView
<cn.hotapk.fastandrutils.widget.FEmptyView
android:id="@+id/empty_lay"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:empty_layout="@+layout/custom_empty_layout"//自定義的空 layout界面
app:include_layout="@+layout/data_layout" />//展現數據的layout
複製代碼
深度自定義的話 代碼中會返回自定義的emptyView
public View getEmptyView() {
return emptyView;
}
複製代碼
能夠findViewById獲取自定義的控件 以後的使用同第一種差很少
Android 自定義空數據提示界面 EmptyView 解說完畢
但願各位在使用中遇到什麼問題或建議能夠用如下聯繫方式進行反饋