/*Copyright ©2015 TommyLemon(https://github.com/TommyLemon)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.*/
package zuo.biao.library.base;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
import zuo.biao.library.R;
import zuo.biao.library.interfaces.ActivityPresenter;
import zuo.biao.library.interfaces.OnBottomDragListener;
import zuo.biao.library.manager.SystemBarTintManager;
import zuo.biao.library.manager.ThreadManager;
import zuo.biao.library.util.Log;
import zuo.biao.library.util.ScreenUtil;
import zuo.biao.library.util.StringUtil;
/**基础android.support.v4.app.FragmentActivity,通过继承可获取或使用 里面创建的 组件 和 方法
* *onFling内控制左右滑动手势操作范围,可自定义
* @author Lemon
* @see ActivityPresenter#getActivity
* @see #context
* @see #view
* @see #fragmentManager
* @see #setContentView
* @see #runUiThread
* @see #runThread
* @see #onDestroy
* @use extends BaseActivity, 具体参考 .DemoActivity 和 .DemoFragmentActivity
*/
public abstract class BaseActivity extends FragmentActivity implements ActivityPresenter, OnGestureListener {
private static final String TAG = "BaseActivity";
@Override
public Activity getActivity() {
return this; //必须return this;
}
/**
* 该Activity实例,命名为context是因为大部分方法都只需要context,写成context使用更方便
* @warn 不能在子类中创建
*/
protected BaseActivity context = null;
/**
* 该Activity的界面,即contentView
* @warn 不能在子类中创建
*/
protected View view = null;
/**
* 布局解释器
* @warn 不能在子类中创建
*/
protected LayoutInflater inflater = null;
/**
* Fragment管理器
* @warn 不能在子类中创建
*/
protected FragmentManager fragmentManager = null;
private boolean isAlive = false;
private boolean isRunning = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
context = (BaseActivity) getActivity();
isAlive = true;
fragmentManager = getSupportFragmentManager();
inflater = getLayoutInflater();
threadNameList = new ArrayList<String>();
BaseBroadcastReceiver.register(context, receiver, ACTION_EXIT_APP);
}
/**
* 默认标题TextView,layout.xml中用@id/tvBaseTitle绑定。子Activity内调用autoSetTitle方法 会优先使用INTENT_TITLE
* @see #autoSetTitle
* @warn 如果子Activity的layout中没有android:id="@id/tvBaseTitle"的TextView,使用前必须在子Activity中赋值
*/
@Nullable
protected TextView tvBaseTitle;
@TargetApi(Build.VERSION_CODES.KITKAT)
@Override
public void setContentView(int layoutResID) {
super.setContentView(layoutResID);
// 状态栏沉浸,4.4+生效 <<<<<<<<<<<<<<<<<
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
getWindow().setFlags(
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
);
}
SystemBarTintManager tintManager = new SystemBarTintManager(this);
tintManager.setStatusBarTintEnabled(true);
tintManager.setStatusBarTintResource(R.color.topbar_bg);//状态背景色,可传drawable资源
// 状态栏沉浸,4.4+生效 >>>>>>>>>>>>>>>>>
tvBaseTitle = findView(R.id.tvBaseTitle);//绑定默认标题TextView
}
//底部滑动实现同点击标题栏左右按钮效果<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
private OnBottomDragListener onBottomDragListener;
private GestureDetector gestureDetector;
/**设置该Activity界面布局,并设置底部左右滑动手势监听
* @param layoutResID
* @param listener
* @use 在子类中
* *1.onCreate中super.onCreate后setContentView(layoutResID, this);
* *2.重写onDragBottom方法并实现滑动事件处理
* *3.在导航栏左右按钮的onClick事件中调用onDragBottom方法
*/
public void setContentView(int layoutResID, OnBottomDragListener listener) {
setContentView(layoutResID);
onBottomDragListener = listener;
gestureDetector = new GestureDetector(this, this);//初始化手势监听类
try { //以防万一中间的值为 null 导致 throw NullPointerException
view = ((ViewGroup) getWindow().getDecorView().findViewById(android.R.id.content)).getChildAt(0);
} catch (Exception e) {
Log.e(TAG, "setContentView try {" +
"\nview = ((ViewGroup) getWindow().getDecorView().findViewById(android.R.id.content)).getChildAt(0);" +
"\n} catch (Exception e) {\n" + e.getMessage());
}
}
//底部滑动实现同点击标题栏左右按钮效果>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
/**
* 用于 打开activity以及activity之间的通讯(传值)等;一些通讯相关基本操作(打电话、发短信等)
*/
protected Intent intent = null;
/**
* 退出时之前的界面进入动画,可在finish();前通过改变它的值来改变动画效果
*/
protected int enterAnim = R.anim.fade;
/**
* 退出时该界面动画,可在finish();前通过改变它的值来改变动画效果
*/
protected int exitAnim = R.anim.right_push_out;
/**通过id查找并获取控件,使用时不需要强转
* @param id
* @return
*/
@SuppressWarnings("unchecked")
public <V extends View> V findView(int id) {
return (V) findViewById(id);
}
/**通过id查找并获取控件,并setOnClickListener
* @param id
* @param l
* @return
*/
public <V extends View> V findView(int id, OnClickListener l) {
V v = findView(id);
v.setOnClickListener(l);
return v;
}
/**通过id查找并获取控件,并setOnClickListener
* @param id
* @param l
* @return
*/
public <V extends View> V findViewById(int id, OnClickListener l) {
return findView(id, l);
}
//自动设置标题方法<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
/**自动把标题设置为上个Activity传入的INTENT_TITLE,建议在子类initView中使用
* *这个方法没有return,tvTitle = tvBaseTitle,直接用tvBaseTitle
* @must 在UI线程中调用
*/
protected void autoSetTitle() {
tvBaseTitle = autoSetTitle(tvBaseTitle);
}
/**自动把标题设置为上个Activity传入的INTENT_TITLE,建议在子类initView中使用
* @param tvTitle
* @return tvTitle 返回tvTitle是为了可以写成一行,如 tvTitle = autoSetTitle((TextView) findViewById(titleResId));
* @must 在UI线程中调用
*/
protected TextView autoSetTitle(TextView tvTitle) {
if (tvTitle != null && StringUtil.isNotEmpty(getIntent().getStringExtra(INTENT_TITLE), false)) {
tvTitle.setText(StringUtil.getCurrentString());
}
return tvTitle;
}
//自动�
评论0
最新资源