在Android App 开发中需要进行频繁的网络访问,在不同的业务逻辑以及页面中需要频繁的生成 ProgressBar 来进行调用与展示,想想就是一件麻烦的事情…
来一波图 :
hide
SO 我们需要做的是:
1.创建一个BaseActivity让之后的Activity继承此Activity
2.自定义一个简单的View用于显示我们需要的ProgressBar
3.将自定义View 加载到BaseActivity中设置显示与隐藏方法
4.进行简单的网络访问
step 1: 创建BaseActivity
package com.key.blog.base;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
/**
* created by key on 2019/5/16
*/
public abstract class BaseActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initView();
initData();
}
public abstract void initView();
public abstract void initData();
}
emmm… 新建一个抽象类BaseActivity 定义两个抽象方法将页面加载以及数据展示交由子类处理。
step2: 自定义View
1).引用一个第三方ProgressBar库,项目地址添加链接描述
implementation 'me.zhanghai.android.materialprogressbar:library:1.4.2'
package com.key.blog.widget;
import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import com.key.blog.R;
import me.zhanghai.android.materialprogressbar.MaterialProgressBar;
/**
* created by key on 2019/5/16
*/
public class BGProgressBar extends LinearLayout {
private Context context;
private AttributeSet attrs;
private int defStyleAttr;
private MaterialProgressBar materialProgressBar;
public BGProgressBar(Context context) {
super(context);
this.context = context;
init();
}
public BGProgressBar(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
this.context = context;
this.attrs = attrs;
init();
}
public BGProgressBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.context = context;
this.attrs = attrs;
this.defStyleAttr = defStyleAttr;
init();
}
public void init(){
View inflate = inflate(context, R.layout.item_progress_bar, this);
materialProgressBar = inflate.findViewById(R.id.dynamic_progress);
}
}
自定义View继承自LinearLayout 重写3个构造方法并加载布局
3)自定义BGProgressBar布局文件
item_progress_bar.xml
资源文件
color.xml
#008577
#00574B
#D81B60
#99000000
#00FFFFFF
drawable文件下 的 progress_bar_bg.xml
自定义View的布局文件 在根布局中设置宽高充满整个布局,并将其居中显示
step3: 将自定义View绑定到BaseActivity
package com.key.blog.base;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import com.key.blog.widget.BGProgressBar;
/**
* created by key on 2019/5/16
*/
public abstract class BaseActivity extends AppCompatActivity {
private BGProgressBar mBGProgressBar;
private ViewGroup mRoot;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initView();
initData();
}
@Override
protected void onStart() {
super.onStart();
}
@Override
protected void onResume() {
super.onResume();
mBGProgressBar = new BGProgressBar(this);
mRoot = findViewById(android.R.id.content);
mRoot.addView(mBGProgressBar);
hide();
}
public abstract void initView();
public abstract void initData();
protected void show(){
if( mBGProgressBar != null){
if(mBGProgressBar.getVisibility() != View.VISIBLE){
mBGProgressBar.setVisibility(View.VISIBLE);
}
}
}
protected void hide(){
if( mBGProgressBar != null){
mBGProgressBar.setVisibility(View.GONE);
}
}
}
在onResume方法中生成控件并添加到根布局中设置初始状态为隐藏状态
step4: try
创建一个Activity 继承 BaseActivity 并发起网络请求,请求用OKHttp 哦!!!!!!
OKHttp项目地址 添加链接描述
implementation 'com.squareup.okhttp3:okhttp:3.12.1'
implementation 'com.squareup.okio:okio:2.1.0'
package com.key.blog.page;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.key.blog.R;
import com.key.blog.base.BaseActivity;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class MainActivity extends BaseActivity {
private Button try_bt;
private TextView try_result;
@SuppressLint("HandlerLeak")
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
switch (msg.what){
case 0:
loadError();
break;
case 1:
loadData();
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void initView() {
setContentView(R.layout.activity_main);
try_bt = findViewById(R.id.try_bt);
try_result = findViewById(R.id.try_result);
try_bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
connectBaidu();
}
});
}
@Override
public void initData() {
}
private void connectBaidu() {
show();
OkHttpClient okHttpClient = new OkHttpClient();
Request request = new Request.Builder().url("https://www.baidu.com").build();
Call call = okHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
handler.sendEmptyMessage(0);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
handler.sendEmptyMessageDelayed(1,2000);
}
});
}
private void loadError() {
hide();
try_result.setText("FAIL");
}
private void loadData() {
hide();
try_result.setText("SUCCESS");
}
}
activity_main.xml
国际惯例访问通过okhttp访问百度,延迟两秒放送消息给主线程,我们只需要在显示的地方调用show(),需要隐藏的地方调用hide().
打完睡觉。。。。。。兄弟们,轻喷。我只是一个刚毕业的菜鸟!!!!!
欢迎指出错误。