Skip to content

Commit

Permalink
重构基类
Browse files Browse the repository at this point in the history
  • Loading branch information
kymjs committed Oct 10, 2015
1 parent 899d381 commit f90a23b
Show file tree
Hide file tree
Showing 7 changed files with 332 additions and 414 deletions.
194 changes: 187 additions & 7 deletions KJFrame/kjframe/src/main/java/org/kymjs/kjframe/KJActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,149 @@
package org.kymjs.kjframe;

import android.app.Activity;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentActivity;
import android.view.View;

import org.kymjs.kjframe.ui.FrameActivity;
import org.kymjs.kjframe.ui.AnnotateUtil;
import org.kymjs.kjframe.ui.I_BroadcastReg;
import org.kymjs.kjframe.ui.I_KJActivity;
import org.kymjs.kjframe.ui.I_SkipActivity;
import org.kymjs.kjframe.ui.KJActivityStack;
import org.kymjs.kjframe.ui.KJFragment;
import org.kymjs.kjframe.ui.SupportFragment;
import org.kymjs.kjframe.utils.KJLoger;

import java.lang.ref.SoftReference;


/**
* Activity's framework,the developer shouldn't extends it <br>
* <b>创建时间</b> 2014-3-1 <br>
* <b>最后修改时间</b> 2014-10-17<br>
*
* @author kymjs (https://github.com/kymjs)
* @version 1.8
*/
public abstract class KJActivity extends FrameActivity {

public static int DESTROY = 0;
public static int STOP = 2;
public static int PAUSE = 1;
public static int RESUME = 3;
public abstract class KJActivity extends FragmentActivity implements
View.OnClickListener, I_BroadcastReg, I_KJActivity, I_SkipActivity {

public static final int WHICH_MSG = 0X37210;

public Activity aty;

protected KJFragment currentKJFragment;
protected SupportFragment currentSupportFragment;
private ThreadDataCallBack callback;
private KJActivityHandle threadHandle = new KJActivityHandle(this);

/**
* Activity状态
*/
public int activityState = DESTROY;

/**
* 一个私有回调类,线程中初始化数据完成后的回调
*/
private interface ThreadDataCallBack {
void onSuccess();
}


private static class KJActivityHandle extends Handler {
private final SoftReference<KJActivity> mOuterInstance;

KJActivityHandle(KJActivity outer) {
mOuterInstance = new SoftReference<>(outer);
}

// 当线程中初始化的数据初始化完成后,调用回调方法
@Override
public void handleMessage(android.os.Message msg) {
if (msg.what == WHICH_MSG && mOuterInstance.get() != null) {
mOuterInstance.get().callback.onSuccess();
}
}
}

/**
* 如果调用了initDataFromThread(),则当数据初始化完成后将回调该方法。
*/
protected void threadDataInited() {
}

/**
* 在线程中初始化数据,注意不能在这里执行UI操作
*/
@Override
public void initDataFromThread() {
callback = new ThreadDataCallBack() {
@Override
public void onSuccess() {
threadDataInited();
}
};
}

@Override
public void initData() {
}

@Override
public void initWidget() {
}

// 仅仅是为了代码整洁点
private void initializer() {
new Thread(new Runnable() {
@Override
public void run() {
initDataFromThread();
threadHandle.sendEmptyMessage(WHICH_MSG);
}
}).start();
initData();
initWidget();
}

/**
* listened widget's click method
*/
@Override
public void widgetClick(View v) {
}

@Override
public void onClick(View v) {
widgetClick(v);
}

@SuppressWarnings("unchecked")
protected <T extends View> T bindView(int id) {
return (T) findViewById(id);
}

@SuppressWarnings("unchecked")
protected <T extends View> T bindView(int id, boolean click) {
T view = (T) findViewById(id);
if (click) {
view.setOnClickListener(this);
}
return view;
}

@Override
public void registerBroadcast() {
}

@Override
public void unRegisterBroadcast() {
}


/***************************************************************************
* print Activity callback methods
***************************************************************************/
Expand All @@ -48,6 +167,11 @@ protected void onCreate(Bundle savedInstanceState) {
aty = this;
KJActivityStack.create().addActivity(this);
KJLoger.state(this.getClass().getName(), "---------onCreat ");

setRootView(); // 必须放在annotate之前调用
AnnotateUtil.initBindView(this);
initializer();
registerBroadcast();
super.onCreate(savedInstanceState);
}

Expand Down Expand Up @@ -86,6 +210,7 @@ protected void onRestart() {

@Override
protected void onDestroy() {
unRegisterBroadcast();
activityState = DESTROY;
KJLoger.state(this.getClass().getName(), "---------onDestroy ");
super.onDestroy();
Expand Down Expand Up @@ -147,4 +272,59 @@ public void showActivity(Activity aty, Class<?> cls, Bundle extras) {
intent.setClass(aty, cls);
aty.startActivity(intent);
}

/**
* 用Fragment替换视图
*
* @param resView 将要被替换掉的视图
* @param targetFragment 用来替换的Fragment
*/
public void changeFragment(int resView, KJFragment targetFragment) {
if (targetFragment.equals(currentKJFragment)) {
return;
}
FragmentTransaction transaction = getFragmentManager()
.beginTransaction();
if (!targetFragment.isAdded()) {
transaction.add(resView, targetFragment, targetFragment.getClass()
.getName());
}
if (targetFragment.isHidden()) {
transaction.show(targetFragment);
targetFragment.onChange();
}
if (currentKJFragment != null && currentKJFragment.isVisible()) {
transaction.hide(currentKJFragment);
}
currentKJFragment = targetFragment;
transaction.commit();
}

/**
* 用Fragment替换视图
*
* @param resView 将要被替换掉的视图
* @param targetFragment 用来替换的Fragment
*/
public void changeFragment(int resView, SupportFragment targetFragment) {
if (targetFragment.equals(currentSupportFragment)) {
return;
}
android.support.v4.app.FragmentTransaction transaction = getSupportFragmentManager()
.beginTransaction();
if (!targetFragment.isAdded()) {
transaction.add(resView, targetFragment, targetFragment.getClass()
.getName());
}
if (targetFragment.isHidden()) {
transaction.show(targetFragment);
targetFragment.onChange();
}
if (currentSupportFragment != null
&& currentSupportFragment.isVisible()) {
transaction.hide(currentSupportFragment);
}
currentSupportFragment = targetFragment;
transaction.commit();
}
}
Loading

0 comments on commit f90a23b

Please sign in to comment.