package com.example.mofangweixin; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; 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.PagerAdapter; import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager.OnPageChangeListener; import android.util.Log; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewConfiguration; import android.view.ViewGroup; import android.view.Window; import android.widget.Button; import android.widget.ImageButton; import android.widget.Toast; public class MainActivity extends FragmentActivity implements OnPageChangeListener, OnClickListener { private ViewPager mviewPager; private List<Fragment> mTabs = new ArrayList<Fragment>(); private String[] mTitles = new String[] { "First Fragment !", "Second Fragment !", "Third Fragment !", "Fourth Fragment !" }; private FragmentPagerAdapter mAdapter; private List<ChangeColorIconWithText> mTabIndicators = new ArrayList<ChangeColorIconWithText>(); private List<View> mviews; private View view1, view2, view3, view4; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setOverflowButtonAlways(); // 去掉标题栏的图标 getActionBar().setDisplayShowHomeEnabled(false); intiview(); intiData(); initEvent(); } /** * 初始化所有事件 */ private void initEvent() { mviewPager.setOnPageChangeListener(this); } private void intiData() { for (String title : mTitles) { // Fragment.setArguments(Bundle bundle)方法设置的bundle会保留下来。 // 所以尽量使用 Fragment.setArguments(Bundle bundle)方式来传递参数 TabFragment tabFragment = new TabFragment(); Bundle bundle = new Bundle(); bundle.putString(TabFragment.TITLE, title); tabFragment.setArguments(bundle); mTabs.add(tabFragment); } } private void intiview() { // TODO Auto-generated method stub mviewPager = (ViewPager) findViewById(R.id.id_viewpager); mviews = new ArrayList<View>(); LayoutInflater inflater = getLayoutInflater(); view1 = inflater.inflate(R.layout.layout1, null); view2 = inflater.inflate(R.layout.layout2, null); view3 = inflater.inflate(R.layout.layout3, null); view4 = inflater.inflate(R.layout.layout4, null); mviews.add(view1); mviews.add(view2); mviews.add(view3); mviews.add(view4); mviewPager.setAdapter(new Mypager(mviews)); // mviewPager.setOffscreenPageLimit(2); // mviewPager.setCurrentItem(0); // 自定义属性初始化 ChangeColorIconWithText one = (ChangeColorIconWithText) findViewById(R.id.id_indicator_one); mTabIndicators.add(one); ChangeColorIconWithText two = (ChangeColorIconWithText) findViewById(R.id.id_indicator_two); mTabIndicators.add(two); ChangeColorIconWithText three = (ChangeColorIconWithText) findViewById(R.id.id_indicator_three); mTabIndicators.add(three); ChangeColorIconWithText four = (ChangeColorIconWithText) findViewById(R.id.id_indicator_four); mTabIndicators.add(four); one.setOnClickListener(this); two.setOnClickListener(this); three.setOnClickListener(this); four.setOnClickListener(this); one.setIconAlpha(1.0f); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); // if (id == R.id.action_settings) { // return true; // } return super.onOptionsItemSelected(item); } @Override public void onClick(View v) { clickTab(v); } /** * 点击Tab按钮 * * @param v */ private void clickTab(View v) { resetOtherTabs(); switch (v.getId()) { case R.id.id_indicator_one: mTabIndicators.get(0).setIconAlpha(1.0f); mviewPager.setCurrentItem(0, false); break; case R.id.id_indicator_two: mTabIndicators.get(1).setIconAlpha(1.0f); mviewPager.setCurrentItem(1, false); break; case R.id.id_indicator_three: mTabIndicators.get(2).setIconAlpha(1.0f); mviewPager.setCurrentItem(2, false); break; case R.id.id_indicator_four: mTabIndicators.get(3).setIconAlpha(1.0f); mviewPager.setCurrentItem(3, false); break; } } /** * 重置其他的TabIndicator的颜色 */ private void resetOtherTabs() { for (int i = 0; i < mTabIndicators.size(); i++) { mTabIndicators.get(i).setIconAlpha(0); } } @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { // Log.e("TAG", "position = " + position + " ,positionOffset = " // + positionOffset); // 通过以上测试,可知左滑+1,右滑-1; if (positionOffset > 0) { ChangeColorIconWithText left = mTabIndicators.get(position); ChangeColorIconWithText right = mTabIndicators.get(position + 1); left.setIconAlpha(1 - positionOffset); right.setIconAlpha(positionOffset); } } @Override public void onPageSelected(int position) { // TODO Auto-generated method stub } @Override public void onPageScrollStateChanged(int state) { // TODO Auto-generated method stub } private class Mypager extends PagerAdapter implements OnClickListener { private List<View> views; public Mypager(List<View> views) { // TODO Auto-generated constructor stub this.views = views; } @Override public int getCount() { // TODO Auto-generated method stub return views.size(); } @Override public boolean isViewFromObject(View arg0, Object arg1) { // TODO Auto-generated method stub return arg0 == arg1; } @Override public void destroyItem(ViewGroup container, int position, Object object) { // TODO Auto-generated method stub container.removeView(views.get(position)); // Log.i("info", "views.get(position)"); } @Override public Object instantiateItem(ViewGroup container, int position) { // TODO Auto-generated method stub container.addView(views.get(position)); //当limit超过4viewepager时会出现BUG // mviewPager.setOffscreenPageLimit(4); /** * * 不知道什么原因本人在测试此处过程中总存在bug,故而try-catch解决问题 */ try { findViewById(R.id.imageview).setOnClickListener(this); Button btn11 = (Button) findViewById(R.id.button11); btn11.setOnClickListener(this); Button btn12 = (Button) findViewById(R.id.button12); btn12.setOnClickListener(this); Button btn13 = (Button) findViewById(R.id.button13); btn13.setOnClickListener(this); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); Log.i("info", "---->btn1------>>>"); } try { Button btn31 = (Button) findViewById(R.id.button31); btn31.setOnClickListener(this); Button btn32 = (Button) findViewById(R.id.button32); btn32.setOnClickListener(this); Button btn33 = (Button) findViewById(R.id.button33); btn33.setOnClickListener(this); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); Log.i("info", "---->btn3------>>>"); } try { Button btn41 = (Button) findViewById(R.id.button41); btn41.setOnClickListener(this); Button btn42 = (Button) findViewById(R.id.button42); btn42.setOnClickListener(this); Button btn43 = (Button) findViewById(R.id.button43); btn43.setOnClickListener(this); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); Log.i("info", "---->btn4------>>>"); } // findViewById(R.id.button31).setOnClickListener(this); // findViewById(R.id.button32).setOnClickListener(this); // findViewById(R.id.button33).setOnClickListener(this); // // findViewById(R.id.button41).setOnClickListener(this); // findViewById(R.id.button42).setOnClickListener(this); // findViewById(R.id.button43).setOnClickListener(this); return views.get(position); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.imageview: Toast.makeText(MainActivity.this, "正在加载个人信息11...", Toast.LENGTH_SHORT).show(); break; case R.id.button11: Toast.makeText(MainActivity.this, "正在加载个人信息11...", Toast.LENGTH_SHORT).show(); break; case R.id.button12: Toast.makeText(MainActivity.this, "微博页面正在打开12..", Toast.LENGTH_SHORT).show(); break; case R.id.button13: Toast.makeText(MainActivity.this, "关于页面正在打开13...", Toast.LENGTH_SHORT).show(); break; case R.id.button31: Toast.makeText(MainActivity.this, "正在加载个人信息31...", Toast.LENGTH_SHORT).show(); break; case R.id.button32: Toast.makeText(MainActivity.this, "微博页面正在打开32..", Toast.LENGTH_SHORT).show(); break; case R.id.button33: Toast.makeText(MainActivity.this, "关于页面正在打开33...", Toast.LENGTH_SHORT).show(); break; case R.id.button41: Toast.makeText(MainActivity.this, "正在加载个人信息41...", Toast.LENGTH_SHORT).show(); break; case R.id.button42: Toast.makeText(MainActivity.this, "微博页面正在打开42..", Toast.LENGTH_SHORT).show(); break; case R.id.button43: Toast.makeText(MainActivity.this, "关于页面正在打开43...", Toast.LENGTH_SHORT).show(); break; } } } /** * 4.0.4以下的版本不能正常显示。 * * 当menu显示的位置不对时用反射机制改变其位置,使在点击时出现在点击的下边 */ private void setOverflowButtonAlways() { try { ViewConfiguration config = ViewConfiguration.get(this); Field menuKey = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey"); menuKey.setAccessible(true); menuKey.setBoolean(config, false); } catch (Exception e) { e.printStackTrace(); } } // 添加menu图标 @Override public boolean onMenuOpened(int featureId, Menu menu) { if (featureId == Window.FEATURE_ACTION_BAR && menu != null) { if (menu.getClass().getSimpleName().equals("MenuBuilder")) { try { Method m = menu.getClass().getDeclaredMethod("setOptionalIconsVisible", Boolean.TYPE); m.setAccessible(true); m.invoke(menu, true); } catch (Exception e) { e.printStackTrace(); } } } return super.onMenuOpened(featureId, menu); } }
最近下载更多
最近浏览更多
anjuw521 LV1
7月10日
2636804923 LV6
6月19日
AIWU1234 LV2
6月7日
1941549176 LV4
6月4日
XJH939 LV1
5月22日
啦啦啦啦啦普 LV1
5月22日
求学的熊猫 LV11
5月6日
121516 LV3
4月28日
微信网友_6522007290040320 LV2
4月15日
wxb666 LV3
3月13日