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 解说完毕
但愿各位在使用中遇到什么问题或建议能够用如下联系方式进行反馈