package com.zhy.weixin6.ui;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewConfiguration;
import android.view.Window;
/**
*
* @author kincai
*
*/
@SuppressLint("NewApi")
public class MainActivity extends FragmentActivity implements
OnPageChangeListener, OnClickListener {
private ViewPager mViewPager;
private List<Fragment> mTabs = new ArrayList<Fragment>();
private FragmentPagerAdapter mAdapter;
private String[] mTitles = new String[] { "First Fragment!",
"Second Fragment!", "Third Fragment!", "Fourth Fragment!" };
private List<ChangeColorIconWithTextView> mTabIndicator = new ArrayList<ChangeColorIconWithTextView>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setOverflowShowingAlways();//进行调用设置actionbar的显示图标
getActionBar().setDisplayShowHomeEnabled(false);//把actionbar上面的默认显示的菜单图标隐藏掉
initView();
initDatas();
mViewPager.setAdapter(mAdapter);
mViewPager.setOnPageChangeListener(this);
}
private void initDatas() {
for (String title : mTitles) {
//官方提示为fragmrnt传数据 使用bundle
TabFragment tabFragment = new TabFragment();
Bundle args = new Bundle();
args.putString(TabFragment.TITLE, title);
tabFragment.setArguments(args);
mTabs.add(tabFragment);
}
mAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
@Override
public int getCount() {
return mTabs.size();
}
@Override
public Fragment getItem(int position) {
return mTabs.get(position);
}
};
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private void initView() {
mViewPager = (ViewPager) findViewById(R.id.id_viewpager);
ChangeColorIconWithTextView one = (ChangeColorIconWithTextView) findViewById(R.id.id_indicator_one);
ChangeColorIconWithTextView two = (ChangeColorIconWithTextView) findViewById(R.id.id_indicator_two);
ChangeColorIconWithTextView three = (ChangeColorIconWithTextView) findViewById(R.id.id_indicator_three);
ChangeColorIconWithTextView four = (ChangeColorIconWithTextView) findViewById(R.id.id_indicator_four);
mTabIndicator.add(one);
mTabIndicator.add(two);
mTabIndicator.add(three);
mTabIndicator.add(four);
one.setOnClickListener(this);
two.setOnClickListener(this);
three.setOnClickListener(this);
four.setOnClickListener(this);
one.setIconAlpha(1.0f);//设置了默认的第一个颜色的颜色
}
@Override
public void onPageSelected(int arg0) {
}
/**
* positionOffset是设置颜色的
*/
@Override
public void onPageScrolled(int position, float positionOffset,
int positionOffsetPixels) {//从第1页到2页的时候 position 始终为0 positionOffset从0-1 从第2页到1页的时候 position 始终为0 positionOffset从1-0
// Log.e("TAG", "position = " position " , positionOffset = "
// positionOffset);
if (positionOffset > 0) {
ChangeColorIconWithTextView left = mTabIndicator.get(position);
ChangeColorIconWithTextView right = mTabIndicator.get(position 1);
left.setIconAlpha(1 - positionOffset);//变小
right.setIconAlpha(positionOffset);
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
@Override
public void onClick(View v) {
resetOtherTabs();//
switch (v.getId()) {
case R.id.id_indicator_one:
mTabIndicator.get(0).setIconAlpha(1.0f);
mViewPager.setCurrentItem(0, false);
break;
case R.id.id_indicator_two:
mTabIndicator.get(1).setIconAlpha(1.0f);
mViewPager.setCurrentItem(1, false);
break;
case R.id.id_indicator_three:
mTabIndicator.get(2).setIconAlpha(1.0f);
mViewPager.setCurrentItem(2, false);
break;
case R.id.id_indicator_four:
mTabIndicator.get(3).setIconAlpha(1.0f);
mViewPager.setCurrentItem(3, false);
break;
}
}
/**
*
*
* 这个方法是设置每个底部view的颜色
*/
private void resetOtherTabs() {
for (int i = 0; i < mTabIndicator.size(); i ) {
mTabIndicator.get(i).setIconAlpha(0);//不是绿色 之前的颜色咯
}
}
/**
* 打开菜单的时候 设置icon
* 使用反射
*/
@Override
public boolean onMenuOpened(int featureId, Menu menu) {
if (featureId == Window.FEATURE_ACTION_BAR && menu != null) {//这里也就是在actionbar打开菜单的时候
if (menu.getClass().getSimpleName().equals("MenuBuilder")) {//MenuBuilder是menu的实现类
try {
Method m = menu.getClass().getDeclaredMethod(//获取其中的一个方法setOptionalIconsVisible
"setOptionalIconsVisible", Boolean.TYPE);
m.setAccessible(true);
m.invoke(menu, true);
} catch (Exception e) {
}
}
}
return super.onMenuOpened(featureId, menu);
}
/**
*
* 把actionBar上默认的那个菜单图标改成我们自己的图标“ ”号的图标 在style里面我们先定义了显示的样式 这里再写个方法 才可以改变图标
*
* 不写样式只写这里 或者 只写样式 不写这里 都是不能改变图标
* 使用反射
*/
private void setOverflowShowingAlways() {
try {
// true if a permanent menu key is present, false otherwise.
ViewConfiguration config = ViewConfiguration.get(this);
Field menuKeyField = ViewConfiguration.class
.getDeclaredField("sHasPermanentMenuKey");//找到menukey
menuKeyField.setAccessible(true);
menuKeyField.setBoolean(config, false);
} catch (Exception e) {
e.printStackTrace();
}
}
}
评论