知识点
1.抽取BaseActivity 管理所有activity 方便退出
public class BaseActivity extends ActionBarActivity {
// 管理运行的所有的activity
public final static List<BaseActivity> mActivities = new LinkedList<BaseActivity>();//增删快
//使用广播也可以,如果当前Activity想退出可以,sendReceiver
// private KillAllReceiver receiver;
// private class KillAllReceiver extends BroadcastReceiver{
//
// @Override
// public void onReceive(Context context, Intent intent) {
// finish();
// }
// }
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// receiver=new KillAllReceiver();
// IntentFilter filter=new IntentFilter("com.itheima.google.killall");
// registerReceiver(receiver, filter);
//解决同步问题,不可以添加的时候删除
synchronized (mActivities) {
mActivities.add(this);
}
init();//定义几个方法子类去实现,代码不臃肿,这个是初始化view前的操作
initView();
initActionBar();
}
@Override
protected void onDestroy() {
super.onDestroy();
synchronized (mActivities) {
mActivities.remove(this);
}
// if(receiver!=null){
// unregisterReceiver(receiver);
// receiver=null;
// }
}
public void killAll() {
// 复制了一份mActivities 集合,因为不能在操作mActivities 的时候去遍历,就是这样复制的
List<BaseActivity> copy;
synchronized (mActivities) {
copy = new LinkedList<BaseActivity>(mActivities);
}
for (BaseActivity activity : copy) {
activity.finish();
}
// 杀死当前的进程
android.os.Process.killProcess(android.os.Process.myPid());
}
protected void initActionBar() {
}
protected void initView() {
}
protected void init() {
}
}
2.MainActivity
public class MainActivity extends BaseActivity implements
OnQueryTextListener {
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle drawerToggle;
private ViewPager mViewPager;
private PagerTabStrip pager_tab_strip;
private String[] tab_names; // 标签的名字
@Override
protected void initActionBar() {
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
drawerToggle = new ActionBarDrawerToggle(this,
mDrawerLayout, R.drawable.ic_drawer_am, R.string.open_drawer,
R.string.close_drawer){
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
Toast.makeText(getApplicationContext(), "抽屉关闭了", 0).show();
}
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
Toast.makeText(getApplicationContext(), "抽屉打开了", 0).show();
}
};
mDrawerLayout.setDrawerListener(drawerToggle);
// 让开关和actionbar建立关系
drawerToggle.syncState();
}
@Override
protected void init() {
tab_names = UiUtils.getStringArray(R.array.tab_names);
}
@Override
protected void initView() {
setContentView(R.layout.activity_main);
mDrawerLayout = (DrawerLayout) findViewById(R.id.dl);
mViewPager=(ViewPager) findViewById(R.id.vp);
pager_tab_strip=(PagerTabStrip) findViewById(R.id.pager_tab_strip);
// 设置标签下划线的颜色
pager_tab_strip.setTabIndicatorColor(getResources().getColor(R.color.indicatorcolor));
mViewPager.setAdapter(new MainAdpater(getSupportFragmentManager()));
//sendBroadcast(new Intent("com.itheima.google.killall"));
//Log.i(tag, msg)
}
private class MainAdpater extends FragmentStatePagerAdapter{
public MainAdpater(FragmentManager fm) {
super(fm);
}
// 每个条目返回的fragment
// 0
@Override
public Fragment getItem(int position) {
// 通过Fragment工厂 生产Fragment
return FragmentFactory.createFragment(position);
}
// 一共有几个条目
@Override
public int getCount() {
return tab_names.length;
}
// 返回每个条目的标题
@Override
public CharSequence getPageTitle(int position) {
return tab_names[position];
}
}
@SuppressLint("NewApi")
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
// 如果运行的环境 (部署到什么版本的手机 )大于3.0
if (android.os.Build.VERSION.SDK_INT > 11) {
SearchView searchView = (SearchView) menu.findItem(
R.id.action_search).getActionView();
searchView.setOnQueryTextListener(this);// 搜索的监听
}
return true;
}
/** 处理actionBar菜单条目的点击事件 */
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_search) {
Toast.makeText(getApplicationContext(), "搜索", 0).show();
}
return drawerToggle.onOptionsItemSelected(item)|super.onOptionsItemSelected(item);
}
// 当搜索提交的时候
@Override
public boolean onQueryTextSubmit(String query) {
Toast.makeText(getApplicationContext(), query, 0).show();
return true;
}
// 当搜索的文本发生变化
@Override
public boolean onQueryTextChange(String newText) {
return true;
}
}
3.UiUtils
public class UiUtils {
/**
* 获取到字符数组
* @param tabNames 字符数组的id
*/
public static String[] getStringArray(int tabNames) {
return getResource().getStringArray(tabNames);
}
public static Resources getResource() {
return BaseApplication.getApplication().getResources();
}
/** dip转换px */
public static int dip2px(int dip) {
final float scale = getResource().getDisplayMetrics().density;
return (int) (dip * scale + 0.5f);
}
/** pxz转换dip */
public static int px2dip(int px) {
final float scale = getResource().getDisplayMetrics().density;
return (int) (px / scale + 0.5f);
}
}
这样定义数组:string.xml里
<string-array name="tab_names">
<item>首页</item>
<item>应用</item>
<item>游戏</item>
<item>专题</item>
<item>分类</item>
<item>排行</item>
</string-array>
4.FragmentFactory
public class FragmentFactory {
private static Map<Integer, Fragment> mFragments = new HashMap<Integer, Fragment>();
public static Fragment createFragment(int position) {
Fragment fragment = null;
fragment = mFragments.get(position); //在集合中取出来Fragment
if (fragment == null) { //如果再集合中没有取出来 需要重新创建
if (position == 0) {
fragment = new HomeFragment();
} else if (position == 1) {
fragment = new AppFragment();
} else if (position == 2) {
fragment = new GameFragment();
} else if (position == 3) {
fragment = new SubjectFragment();
} else if (position == 4) {
fragment = new CategoryFragment();
} else if (position == 5) {
fragment = new TopFragment();
}
if (fragment != null) {
mFragments.put(position, fragment);// 把创建好的Fragment存放到集合中缓存起来
}
}
return fragment;
}
}
通过这样获取上下文
/**
* 代表当前应用程序
* @author itcast
*
*/
public class BaseApplication extends Application {
private static BaseApplication application;
@Override
public void onCreate() {
super.onCreate();
application=this;
}
public static Context getApplication() {
return application;
}
}