之前的实现需要自己创建Tab,自己添加监听来实现tab和ViewPager的切换比较复杂,现在对其进行改进,布局文件进行改进
<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">
<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>
<!-- 抽屉 -->
<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>
逻辑代码,只需要在ViewPager的监听MainAdapter中添加设置标题即可
// 返回每个条目的标题,设置tab的标签
@Override
public CharSequence getPageTitle(int position) {
return "标签" + position;
}
逻辑代码MainActivity.java
package com.ldw.market;
import com.ldw.market.fragment.AppFragment;
import com.ldw.market.fragment.HomeFragment;
import junit.framework.AssertionFailedError;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.PagerTabStrip;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBar.Tab;
import android.support.v7.app.ActionBar.TabListener;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.SearchView.OnQueryTextListener;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity implements OnQueryTextListener{
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle drawerToggle;
private ViewPager mViewPager;
private PagerTabStrip pager_tab_strip;
private String[] tab_names; // 标签的名字
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
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 MainAdapter(getSupportFragmentManager()));
//添加tab标签名
ActionBar actionBar = getSupportActionBar();
//让拉出显示详情的图片显示,可以拉出详情
actionBar.setDisplayHomeAsUpEnabled(true);
//让按键点击有效
actionBar.setHomeButtonEnabled(true);
//ActionBarDrawerToggle是ActionBar控制抽屉,
//参数1:当前actionBar的Activity,参数2:控制的抽屉,参数3:按钮的图片,参数4和5是描述
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();
}
};
//设置监听DrawerListener
mDrawerLayout.setDrawerListener(drawerToggle);
// 让开关和actionbar建立关系
drawerToggle.syncState();
}
//可以继承PageAapter实现ViePager翻页现在继承FragmentStatePagerAdapter可以实现fragement
private class MainAdapter extends FragmentStatePagerAdapter{
public MainAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
//每一个条目返回的fragment
@Override
public Fragment getItem(int position) {
if(position == 0){
return new HomeFragment();
}else{
return new AppFragment();
}
}
//条目的总数
@Override
public int getCount() {
// TODO Auto-generated method stub
return 4;
}
// 返回每个条目的标题,设置tab的标签
@Override
public CharSequence getPageTitle(int position) {
return "标签" + position;
}
}
//初始化ActionBar的界面
@SuppressLint("NewApi") @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.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;
}
public void click(View v){
Intent intent = new Intent(getApplicationContext(), DetailActivity.class);
startActivity(intent);
}
/** 处理actionBar菜单条目的点击事件 */
@Override
public boolean onOptionsItemSelected(MenuItem item) {
System.out.println("所有");
//当前点击的条目是search
if (item.getItemId() == R.id.action_search) {
Toast.makeText(getApplicationContext(), "搜索", 0).show();
}
//优先处理drawerToggle的点击,drawerToggle出不不了交给父类
return drawerToggle.onOptionsItemSelected(item)|super.onOptionsItemSelected(item);
}
// 当搜索提交的时候
@Override
public boolean onQueryTextChange(String arg0) {
Toast.makeText(getApplicationContext(), arg0, 0).show();
return false;
}
// 当搜索的文本发生变化
@Override
public boolean onQueryTextSubmit(String arg0) {
Toast.makeText(getApplicationContext(), arg0, 0).show();
return false;
}
}