首页>代码>android高仿微信6.0主界面源码>/MOFANGWEIXIN/src/com/example/mofangweixin/MainActivity.java
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);
	}
}
最近下载更多
齐吊大神  LV6 2023年4月25日
jiazhihan  LV5 2022年5月19日
张恺祺  LV6 2022年5月18日
bingpine  LV6 2022年5月8日
511076827  LV33 2022年4月11日
KCBERG  LV4 2021年12月5日
刘地带  LV11 2021年11月24日
保持淡定  LV2 2021年6月29日
admi1234  LV8 2021年6月28日
大连有个马猴  LV4 2021年6月19日
最近浏览更多
wxb666  LV3 3月13日
666ing  LV2 2023年12月27日
小安同学  LV7 2023年11月2日
17609102982 2023年8月22日
暂无贡献等级
panwill  LV5 2023年8月18日
红枫-qaq 2023年8月8日
暂无贡献等级
暂无贡献等级
ddd11111  LV1 2023年6月16日
qazwer11  LV5 2023年5月22日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友