193 lines
6.8 KiB
Java
193 lines
6.8 KiB
Java
package com.gh.gamecenter;
|
||
|
||
import android.content.Context;
|
||
import android.content.Intent;
|
||
import android.os.Bundle;
|
||
import android.support.annotation.MenuRes;
|
||
import android.support.annotation.StringRes;
|
||
import android.support.v4.app.Fragment;
|
||
import android.support.v7.widget.Toolbar;
|
||
import android.text.TextUtils;
|
||
import android.view.Menu;
|
||
import android.view.MenuItem;
|
||
import android.view.MotionEvent;
|
||
import android.view.View;
|
||
import android.widget.TextView;
|
||
|
||
import com.gh.base.BaseActivity;
|
||
import com.gh.gamecenter.normal.NormalFragment;
|
||
import com.gh.gamecenter.normal.ToolbarController;
|
||
import com.lightgame.OnTitleClickListener;
|
||
|
||
import java.util.List;
|
||
|
||
import butterknife.BindView;
|
||
|
||
/**
|
||
* Created by khy on 17/10/17.
|
||
*/
|
||
|
||
public abstract class NormalActivity extends BaseActivity implements ToolbarController, Toolbar.OnMenuItemClickListener {
|
||
|
||
@BindView(R.id.normal_toolbar)
|
||
Toolbar mNormalToolbar;
|
||
@BindView(R.id.normal_title)
|
||
TextView mNormalTitle;
|
||
|
||
private Fragment mTargetFragment;
|
||
|
||
public static final String NORMAL_FRAGMENT_NAME = "normalFragmentName";
|
||
public static final String NORMAL_FRAGMENT_BUNDLE = "normalFragmentBundle";
|
||
|
||
// 针对部分跳转不符合NormalActivity规则的额外处理,主要绑定NormalFragment
|
||
protected Intent provideNormalIntent() {
|
||
return null;
|
||
}
|
||
|
||
protected static Intent getTargetIntent(Context context, Class<? extends NormalActivity> cls, Class<? extends NormalFragment> t, Bundle bundle) {
|
||
Intent intent = new Intent(context, cls);
|
||
intent.putExtra(NORMAL_FRAGMENT_NAME, t.getCanonicalName());
|
||
intent.putExtra(NORMAL_FRAGMENT_BUNDLE, bundle);
|
||
return intent;
|
||
}
|
||
|
||
protected static Intent getTargetIntent(Context context, Class<? extends NormalActivity> cls, Class<? extends NormalFragment> t) {
|
||
Intent intent = new Intent(context, cls);
|
||
intent.putExtra(NORMAL_FRAGMENT_NAME, t.getCanonicalName());
|
||
return intent;
|
||
}
|
||
|
||
|
||
public static void startFragmentNewTask(Context context, Class<? extends NormalFragment> t, Bundle bundle) {
|
||
Intent intent = new Intent(context, NormalActivity.class);
|
||
intent.putExtra(NORMAL_FRAGMENT_NAME, t.getCanonicalName());
|
||
intent.putExtra(NORMAL_FRAGMENT_BUNDLE, bundle);
|
||
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||
context.startActivity(intent);
|
||
}
|
||
|
||
public static void startFragment(Context context, Class<? extends NormalFragment> t, Bundle bundle) {
|
||
Intent intent = new Intent(context, NormalActivity.class);
|
||
intent.putExtra(NORMAL_FRAGMENT_NAME, t.getCanonicalName());
|
||
intent.putExtra(NORMAL_FRAGMENT_BUNDLE, bundle);
|
||
context.startActivity(intent);
|
||
}
|
||
|
||
|
||
@Override
|
||
protected int getLayoutId() {
|
||
return R.layout.activity_normal;
|
||
}
|
||
|
||
@Override
|
||
protected void onCreate(Bundle savedInstanceState) {
|
||
super.onCreate(savedInstanceState);
|
||
if (getIntent() != null) {
|
||
if (savedInstanceState == null) {
|
||
handleIntent(getIntent());
|
||
} else {
|
||
for (Fragment fragment : getSupportFragmentManager().getFragments()) {
|
||
if (fragment instanceof NormalFragment) mTargetFragment = fragment;
|
||
}
|
||
}
|
||
}
|
||
// setSupportActionBar(mNormalToolbar); // 替换actionBar后 toolBar无法控制
|
||
mNormalToolbar.setNavigationIcon(R.drawable.ic_bar_back);
|
||
mNormalToolbar.setNavigationOnClickListener(new View.OnClickListener() {
|
||
@Override
|
||
public void onClick(View view) {
|
||
onBackPressed();
|
||
}
|
||
});
|
||
mNormalTitle.setOnClickListener(view -> {
|
||
final List<Fragment> fragmentList = getSupportFragmentManager().getFragments();
|
||
for (Fragment fragment : fragmentList) {
|
||
if (fragment instanceof OnTitleClickListener) {
|
||
((OnTitleClickListener) fragment).onTitleClick();
|
||
}
|
||
}
|
||
});
|
||
}
|
||
|
||
@Override
|
||
protected void onNewIntent(Intent intent) {
|
||
super.onNewIntent(intent);
|
||
if (getIntent() != null) handleIntent(intent);
|
||
}
|
||
|
||
private void handleIntent(Intent intent) {
|
||
String fraName = intent.getStringExtra(NORMAL_FRAGMENT_NAME);
|
||
Bundle bundle = intent.getBundleExtra(NORMAL_FRAGMENT_BUNDLE);
|
||
if (TextUtils.isEmpty(fraName)) {
|
||
if (provideNormalIntent() == null) {
|
||
return;
|
||
}
|
||
fraName = provideNormalIntent().getStringExtra(NORMAL_FRAGMENT_NAME);
|
||
if (bundle == null) bundle = getIntent().getExtras();
|
||
if (TextUtils.isEmpty(fraName)) return;
|
||
}
|
||
mTargetFragment = Fragment.instantiate(this, fraName, bundle);
|
||
getSupportFragmentManager().beginTransaction().replace(R.id.normal_content, mTargetFragment).commitNowAllowingStateLoss();
|
||
}
|
||
|
||
@Override
|
||
public void setNavigationTitle(@StringRes int res) {
|
||
if (mNormalTitle == null) return;
|
||
mNormalTitle.setText(res);
|
||
}
|
||
|
||
@Override
|
||
public void setNavigationTitle(String res) {
|
||
if (mNormalTitle == null) return;
|
||
if (res != null)
|
||
mNormalTitle.setText(res);
|
||
}
|
||
|
||
@Override
|
||
public void setToolbarMenu(@MenuRes int res) {
|
||
if (mNormalToolbar == null) return;
|
||
mNormalToolbar.inflateMenu(res);
|
||
mNormalToolbar.setOnMenuItemClickListener(this);
|
||
|
||
Menu menu = mNormalToolbar.getMenu();
|
||
for (int i = 0; i < menu.size(); i++) {
|
||
MenuItem menuItem = menu.getItem(i);
|
||
// menu设置actionLayout后,无法捕捉点击事件,以icon为tag,如果icon is null 手动设置menuItem点击事件
|
||
if (menuItem != null && menuItem.getIcon() == null) {
|
||
if (menuItem.getActionView() != null) {
|
||
menuItem.getActionView().setOnClickListener((v) -> this.onMenuItemClick(menuItem));
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
@Override
|
||
public MenuItem getMenuItem(int res) {
|
||
if (mNormalToolbar == null) return null; //后续页面做好判断
|
||
return mNormalToolbar.getMenu().findItem(res);
|
||
}
|
||
|
||
@Override
|
||
public boolean onMenuItemClick(MenuItem menuItem) {
|
||
if (mTargetFragment instanceof NormalFragment) {
|
||
((NormalFragment) mTargetFragment).onMenuItemClick(menuItem);
|
||
}
|
||
return false;
|
||
}
|
||
|
||
@Override
|
||
public void onBackPressed() {
|
||
if (mTargetFragment instanceof NormalFragment && !((NormalFragment) mTargetFragment).onBackPressed()) {
|
||
super.onBackPressed();
|
||
}
|
||
}
|
||
|
||
@Override
|
||
public boolean dispatchTouchEvent(MotionEvent ev) {
|
||
if (mTargetFragment instanceof NormalFragment) {
|
||
((NormalFragment) mTargetFragment).onTouchEvent(ev);
|
||
}
|
||
return super.dispatchTouchEvent(ev);
|
||
}
|
||
}
|