搭建环境
- 安装genymotion模拟器
- 安装WebServer
- 在sd卡的根目录下 防止WebInfos 文件夹
ActionBar
- 引用v7-appcompat
- Activity继承ActionBarActivity
- android:theme="@style/Theme.AppCompat.Light" >
搜索
-
yourapp:actionViewClass="android.support.v7.widget.SearchView"
-
Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); SearchView searchView = (SearchView) menu.findItem(R.id.action_search) .getActionView(); searchView.setOnQueryTextListener(this);// 搜索的监听 return true; }
-
// 当搜索提交的时候 @Override public boolean onQueryTextSubmit(String query) { Toast.makeText(getApplicationContext(), query, 0).show(); return true; } // 当搜索的文本发生变化 @Override public boolean onQueryTextChange(String newText) { //Toast.makeText(getApplicationContext(), newText, 0).show(); return true; }
返回按钮的处理
1
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
2
<activity android:name=".DetailActivity"
android:label="@string/app_detail"
android:parentActivityName="com.itheima.googleplay.MainActivity"
>
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.itheima.googleplay.MainActivity" />
</activity>
实现ActionBar Tab标签
1 在Drawable 目录下 写了一个标签的状态选择器
2 实现自定义主题
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme"
parent="@style/Theme.AppCompat.Light">
<item name="android:actionBarTabStyle">@style/MyActionBarTabs</item>
<!-- Support library compatibility -->
<item name="actionBarTabStyle">@style/MyActionBarTabs</item>
</style>
<!-- ActionBar tabs styles -->
<style name="MyActionBarTabs"
parent="@style/Widget.AppCompat.ActionBar.TabView">
<!-- tab indicator -->
<item name="android:background">@drawable/actionbar_tab_indicator</item>
<!-- Support library compatibility -->
<item name="background">@drawable/actionbar_tab_indicator</item>
</style>
</resources>
3 在代码里添加标签
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
Tab tab1=actionBar.newTab().setText("标签一").setTabListener(new MyTabListener());
actionBar.addTab(tab1);
svn
192.168.13.173 用户密码student 123 http://T440S-PC/svn/googlePlay/
DrawerLayout
实现抽屉效果
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:id="@+id/dl">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
/>
<FrameLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_tab"
android:layout_gravity="left">
</FrameLayout>
</android.support.v4.widget.DrawerLayout>
ActionBarDrawerToggle
控制抽屉的开关, 显示在actionBar 上面
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();
/** 处理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);
}
http://honx.in/i/VJtosc6vD0zqOc9G
切换ViewPager
<android.support.v4.view.ViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
>
<android.support.v4.view.PagerTabStrip
android:id="@+id/pager_tab_strip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="#ffffff"
android:textColor="#000"
android:paddingTop="4dp"
android:paddingBottom="4dp" />
</android.support.v4.view.ViewPager>
private class MainAdpater extends FragmentStatePagerAdapter{
public MainAdpater(FragmentManager fm) {
super(fm);
}
// 每个条目返回的fragment
// 0
@Override
public Fragment getItem(int position) {
if(position==0){
return new HomeFragment();
}else{
return new AppFragment();
}
}
// 一共有几个条目
@Override
public int getCount() {
return 4;
}
// 返回每个条目的标题
@Override
public CharSequence getPageTitle(int position) {
return "标签"+position;
}
}
抽取BaseActivity
public class BaseActivity extends ActionBarActivity {
// 管理运行的所有的activity
static List<BaseActivity> mActivities = new LinkedList<BaseActivity>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
synchronized (mActivities) {
mActivities.add(this);
}
init();
initView();
initActionBar();
}
@Override
protected void onDestroy() {
super.onDestroy();
synchronized (mActivities) {
mActivities.remove(this);
}
}
public void killAll() {
// 复制了一份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() {
}
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;
}
}
加载界面的架子
- 四种界面: 加载中, 加载错误,加载为空 ,加载成功
-
根据不同的状态去切换界面
public class HomeFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { FrameLayout frameLayout=new FrameLayout(getActivity()); init(); // 在FrameLayout中 添加4种不同的界面 showPage();// 根据不同的状态显示不同的界面 show();// 根据服务器的数据 切换状态 return frameLayout; }
移除父容器
public class ViewUtils {
public static void removeParent(View v){
// 先找到爹 在通过爹去移除孩子
ViewParent parent = v.getParent();
//所有的控件 都有爹 爹一般情况下 就是ViewGoup
if(parent instanceof ViewGroup){
ViewGroup group=(ViewGroup) parent;
group.removeView(v);
}
}
}