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;
imp
《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享
ort 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 BaseActivity implements OnQueryTextListener{
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle drawerToggle;
private ViewPager mViewPager;
private PagerTabStrip pager_tab_strip;
private String[] tab_names; // 标签的名字
@Override
protected void init() {
// TODO Auto-generated method stub
}
//初始化ActionBar
@Override
protected void initActionBar() {
//添加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();
}
//初始化界面
@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 MainAdapter(getSupportFragmentManager()));
}
//可以继承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;
}
}
侧边详情栏DetailActivity.java
package com.ldw.market;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.view.MenuItem;
import android.widget.Toast;
public class DetailActivity extends BaseActivity{
@Override
protected void initView() {
setContentView(R.layout.activity_detail);
}
@Override
protected void initActionBar() {
super.initActionBar();
//获取到actionBar,使用getSupportActionBar,getActionBar是android.app下面的getActionBar
ActionBar actionBar = getSupportActionBar();
//图标的显示与隐藏,显示返回按钮
actionBar.setDisplayHomeAsUpEnabled(true);
}