在加载网络数据的时候,我们要给用户一个友好的加载数据的界面,提示正在加载、加载失败和加载成功的功能,其实原理很简单,只是在显示View的上面盖了一层自定义的View,覆盖View的话当然是用RelativeLayout啦,其实思想是在AndroidChina里面看见的,打造不一样的EmptyView,现在再次记录一下,做个笔记。
效果图还是直接看主代码吧
EmptyViewLayout.java import android.annotation.SuppressLint; import android.content.Context; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ProgressBar; import android.widget.RelativeLayout; @SuppressLint("NewApi") public class EmptyViewLayout extends RelativeLayout { private ImageView failure;//加载失败的图片 private ProgressBar progressBar;//正在loading的pb private View bindView;// 绑定的View,即要显示的View public EmptyViewLayout(Context context) { super(context); initView(context); } public EmptyViewLayout(Context context, AttributeSet attrs) { super(context, attrs); initView(context); } private void initView(Context context) { View view = LayoutInflater.from(context).inflate(R.layout.empty_view, null); failure = (ImageView) view.findViewById(R.id.loading_failure); progressBar = (ProgressBar) view.findViewById(R.id.loading_ing); /* * 这一步添加param必须得要,很多人会遇到inflater添加进来的布局 * 显示的时候只能显示包容的布局,根本不能match整个布局,然后又说, * 我布局文件是match的啊,然后一直找不到问题的解决方法, * 我感觉问题应该是出在自定义布局add这个xml的时候需要重新设置宽高吧 * 所以,我们在自定义控件需要添加xml布局的时候,记得add这个布局的时候 * 也设置一下param,问题就会解决了 */ LinearLayout.LayoutParams param = new LinearLayout.LayoutParams( new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); addView(view, param); } /** * 总共有三种状态显示: * 一种是正在加载状态, * 一种是加载失败, * 一种是加载成功 */ public void Loading() { setVisibility(View.VISIBLE); if (bindView != null) { progressBar.setVisibility(View.VISIBLE); failure.setVisibility(View.GONE); } } public void succees() { setVisibility(View.GONE); if (bindView != null) { bindView.setVisibility(View.VISIBLE); } } public void failure() { setVisibility(View.VISIBLE); if (bindView != null) { failure.setVisibility(View.VISIBLE); progressBar.setVisibility(View.GONE); } } // 绑定加载 的view public void bindView(View view) { this.bindView = view; } /* * 利用反射机制,响应对方需要响应的方法 */ public void buttonClick(final Object base, final String method) { failure.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { try { Method m = base.getClass().getDeclaredMethod(method); m.setAccessible(true); m.invoke(base, null); } catch (Exception e) { e.printStackTrace(); } } }); } } empty_view.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <ImageView android:id="@+id/loading_failure" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:src="@drawable/failure" android:visibility="gone" /> <ProgressBar android:id="@+id/loading_ing" android:layout_width="130dp" android:layout_height="200dp" android:layout_centerInParent="true" android:indeterminateDrawable="@drawable/empty_progressbar" /> </RelativeLayout> ProgressBar的布局动画empty_progressbar.xml <?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="http://schemas.android.com/apk/res/android" > <item android:drawable="@drawable/bg_default_layout_loading1" android:duration="150"> </item> <item android:drawable="@drawable/bg_default_layout_loading2" android:duration="150"> </item> </animation-list> activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.emptyview.MainActivity" > <com.example.emptyview.EmptyViewLayout android:id="@+id/emptyView" android:layout_width="match_parent" android:layout_height="match_parent" > </com.example.emptyview.EmptyViewLayout> <LinearLayout android:id="@+id/content" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ff0000" android:orientation="vertical" android:visibility="gone" > <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:text="加载成功" android:textColor="#fff" android:textSize="30dp" /> </LinearLayout> </RelativeLayout> MainActivity.java import java.util.Random; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.widget.LinearLayout; import android.widget.TextView; public class MainActivity extends Activity { private EmptyViewLayout empty; private LinearLayout linear; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); empty = (EmptyViewLayout) findViewById(R.id.emptyView); linear = (LinearLayout) findViewById(R.id.content); empty.bindView(linear);// 绑定要显示的View // 加载失败后点击的时候执行onload方法(比如onload里面放的是加载网络数据啊) empty.buttonClick(this, "onload"); onload(); } public void onload() { empty.Loading();//empty布局显示正在加载 new Handler().postDelayed(new Runnable() { @Override public void run() { //用随机数来判定 Random r = new Random(); int res = r.nextInt(3); if (res == 0) { empty.succees();//加载成功 } else { empty.failure();//加载失败 } } }, 2000); } }感觉还行吧,算是给自己做个笔记了,上面那个链接可能说的更明白一点,希望能帮助到大家
相关资源:EmptyViewDemo