本文主要是介绍课工场 “微服私访”项目学习(四),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
ViewPager+Fragemnt实现基本的界面构造
封装了,顶部导航栏和底部导航栏,mainActivity中间嵌套ViewPager
项目源码地址
http://www.kgc.cn/bbs/post/153705.shtml
BaseFragment
public class BaseFragment extends Fragment {protected Activity mActivity;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);mActivity = getActivity();}@Overridepublic void startActivity(Intent intent) {super.startActivity(intent);getActivity().overridePendingTransition(R.anim.activity_in, 0);}}
代码中的动画
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" android:duration="600"><alpha
android:fromAlpha="0.0"android:toAlpha="1.0"android:duration="600"android:fillBefore="true"android:fillAfter="true"/><scale
android:duration="600"android:fromXScale="4.0"android:toXScale="1.0"android:fromYScale="4.0"android:toYScale="1.0"android:pivotX="50%"android:pivotY="50%"android:fillBefore="true"/>
</set>
Activity 的基类
public class BaseFragmentActivity extends FragmentActivity {protected Context mContext;protected Activity mActivity;private TextView title_bar_back,title_bar_name;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);mContext = this;mActivity = this;requestWindowFeature(Window.FEATURE_NO_TITLE); // 隐藏标题栏}@Overridepublic void finish() {super.finish();}@Overridepublic void startActivity(Intent intent) {super.startActivity(intent);}@SuppressLint("NewApi")@Overridepublic void setContentView(int layoutResID) {super.setContentView(layoutResID);if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) {getWindow().setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);}if (VERSION.SDK_INT >= VERSION_CODES.ICE_CREAM_SANDWICH) {View v = findViewById(R.id.root_layout);if (v != null) {v.setFitsSystemWindows(true);}}if(VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) { Window window = getWindow(); window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE); window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); window.setStatusBarColor(Color.TRANSPARENT);} title_bar_back = (TextView) findViewById(R.id.title_bar_back);title_bar_name = (TextView) findViewById(R.id.title_bar_name);if(title_bar_back != null){title_bar_back.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {finish();}});}}@Overrideprotected void onStart() {super.onStart();}@Overrideprotected void onStop() {super.onStop();}public void setTitleName(String str){if(title_bar_name != null){title_bar_name.setText(str);}}
}
通过android不同版本,用不同的方法进行顶部栏的渲染
public class BaseFragmentActivity extends FragmentActivity {protected Context mContext;protected Activity mActivity;private TextView title_bar_back,title_bar_name;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);mContext = this;mActivity = this;requestWindowFeature(Window.FEATURE_NO_TITLE); // 隐藏标题栏}@Overridepublic void finish() {super.finish();}@Overridepublic void startActivity(Intent intent) {super.startActivity(intent);}@SuppressLint("NewApi")@Overridepublic void setContentView(int layoutResID) {super.setContentView(layoutResID);if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) {getWindow().setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);}if (VERSION.SDK_INT >= VERSION_CODES.ICE_CREAM_SANDWICH) {View v = findViewById(R.id.root_layout);if (v != null) {v.setFitsSystemWindows(true);}}if(VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) { Window window = getWindow(); window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE); window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); window.setStatusBarColor(Color.TRANSPARENT);} title_bar_back = (TextView) findViewById(R.id.title_bar_back);title_bar_name = (TextView) findViewById(R.id.title_bar_name);if(title_bar_back != null){title_bar_back.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {finish();}});}}@Overrideprotected void onStart() {super.onStart();}@Overrideprotected void onStop() {super.onStop();}public void setTitleName(String str){if(title_bar_name != null){title_bar_name.setText(str);}}
}
mainactivity
public class MainActivity extends BaseFragmentActivity implements View.OnClickListener {private ViewPager viewPager_content;private TextView txt_menu_bottom_home;private TextView txt_menu_bottom_shop;private TextView txt_menu_bottom_visit;private TextView txt_menu_bottom_train;private TextView txt_menu_bottom_me;private final int TAB_HOME = 0;private final int TAB_SHOP = 1;private final int TAB_VISIT = 2;private final int TAB_TRAIN = 3;private final int TAB_ME = 4;private int IsTab;private HomeFragment homeFragment;private ShopFragment shopFragment;private VisitFragment visitFragment;private MeFragment meFragment;private TrainFragment trainFragment;private FragmentAdapter adapter;private ImageView title_bar_more, title_bar_change;private String userid;//用户idprivate Boolean isLoad;//是否登录@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initID();//初始化绑定组件idinitView();//初始化视图}/*** 初始化视图*/private void initView() {isLoad = false;homeFragment = new HomeFragment();shopFragment = new ShopFragment();visitFragment = new VisitFragment();trainFragment = new TrainFragment();meFragment = new MeFragment();adapter = new FragmentAdapter(getSupportFragmentManager());viewPager_content.setAdapter(adapter);setSelected(txt_menu_bottom_home);viewPager_content.setCurrentItem(TAB_HOME, false);setTitleName("首页");}/*** 初始化控件加载*/private void initID() {TextView title_bar_back = (TextView) findViewById(R.id.title_bar_back);title_bar_back.setVisibility(View.GONE);viewPager_content = (ViewPager) findViewById(R.id.viewPager_content);txt_menu_bottom_home = (TextView) findViewById(R.id.txt_menu_bottom_home);txt_menu_bottom_shop = (TextView) findViewById(R.id.txt_menu_bottom_shop);txt_menu_bottom_visit = (TextView) findViewById(R.id.txt_menu_bottom_visit);txt_menu_bottom_train = (TextView) findViewById(R.id.txt_menu_bottom_train);txt_menu_bottom_me = (TextView) findViewById(R.id.txt_menu_bottom_me);title_bar_more = (ImageView) findViewById(R.id.title_bar_more);title_bar_change = (ImageView) findViewById(R.id.title_bar_change);txt_menu_bottom_home.setOnClickListener(this);txt_menu_bottom_shop.setOnClickListener(this);txt_menu_bottom_visit.setOnClickListener(this);txt_menu_bottom_train.setOnClickListener(this);txt_menu_bottom_me.setOnClickListener(this);title_bar_more.setOnClickListener(this);title_bar_change.setOnClickListener(this);//ViewPager滑动监听,切换界面viewPager_content.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {@Overridepublic void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}@Overridepublic void onPageSelected(int position) {Log.i("main_viewpager", "position--" + position);switch (position) {case TAB_HOME://点击首页模块执行IsTab = 1;jumpHomeFragment();break;case TAB_SHOP://点击巡店模块执行IsTab = 2;jumpShopFragment();break;case TAB_VISIT://点击拜访模块执行IsTab = 3;jumpVisitsFragment();break;case TAB_TRAIN://点击培训模块执行IsTab = 4;jumpTrainFragment();break;case TAB_ME://点击个人中心模块执行IsTab = 5;title_bar_more.setVisibility(View.GONE);title_bar_change.setVisibility(View.GONE);setSelected(txt_menu_bottom_me);viewPager_content.setCurrentItem(TAB_ME, false);setTitleName("个人中心");break;}}@Overridepublic void onPageScrollStateChanged(int state) {}});}@Overridepublic void onClick(View v) {
// if (!isLoad) {
// Toast.makeText(mContext, R.string.please_login, Toast.LENGTH_SHORT).show();
// }switch (v.getId()) {case R.id.txt_menu_bottom_home://点击首页模块执行IsTab = 1;title_bar_more.setVisibility(View.GONE);title_bar_change.setVisibility(View.GONE);setSelected(txt_menu_bottom_home);viewPager_content.setCurrentItem(TAB_HOME, false);setTitleName("首页");break;case R.id.txt_menu_bottom_shop://点击巡店模块执行IsTab = 2;title_bar_more.setVisibility(View.VISIBLE);title_bar_change.setVisibility(View.VISIBLE);setSelected(txt_menu_bottom_shop);viewPager_content.setCurrentItem(TAB_SHOP, false);setTitleName("巡店");break;case R.id.txt_menu_bottom_visit://点击拜访模块执行IsTab = 3;title_bar_more.setVisibility(View.VISIBLE);title_bar_change.setVisibility(View.GONE);setSelected(txt_menu_bottom_visit);viewPager_content.setCurrentItem(TAB_VISIT, false);setTitleName("拜访");break;case R.id.txt_menu_bottom_train://点击培训模块执行IsTab = 4;title_bar_more.setVisibility(View.GONE);title_bar_change.setVisibility(View.GONE);setSelected(txt_menu_bottom_train);viewPager_content.setCurrentItem(TAB_TRAIN, false);setTitleName("培训");break;case R.id.txt_menu_bottom_me://点击个人中心模块执行IsTab = 5;title_bar_more.setVisibility(View.GONE);title_bar_change.setVisibility(View.GONE);setSelected(txt_menu_bottom_me);viewPager_content.setCurrentItem(TAB_ME, false);setTitleName("个人中心");break;case R.id.title_bar_more:if (isLoad) {if (IsTab == 2) {//新建巡店Toast.makeText(mActivity, "新建巡店敬请期待", Toast.LENGTH_SHORT).show();} else if (IsTab == 3) {//新建拜访Toast.makeText(mActivity, "新建拜访暂未开放", Toast.LENGTH_SHORT).show();}} else {Toast.makeText(mContext, R.string.please_login, Toast.LENGTH_SHORT).show();}break;default:break;}}//当选中的时候变色,改变底部文字颜色public void setSelected(TextView textView) {txt_menu_bottom_home.setSelected(false);txt_menu_bottom_shop.setSelected(false);txt_menu_bottom_visit.setSelected(false);txt_menu_bottom_train.setSelected(false);txt_menu_bottom_me.setSelected(false);textView.setSelected(true);}/*** 模块Fragment适配器*/public class FragmentAdapter extends FragmentPagerAdapter {private final int TAB_COUNT = 5;public FragmentAdapter(FragmentManager fm) {super(fm);}@Overridepublic Fragment getItem(int id) {switch (id) {case TAB_HOME:return homeFragment;case TAB_SHOP:return shopFragment;case TAB_VISIT:return visitFragment;case TAB_TRAIN:return trainFragment;case TAB_ME:return meFragment;default:return null;}}@Overridepublic int getCount() {return TAB_COUNT;}}@Overrideprotected void onDestroy() {super.onDestroy();SharePreUtil.SetShareString(mContext, "userid", "");//Activity死亡清空id保存}@Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) {//监听返回键,如果当前界面不是首界面,或没切换过界面,切到首界面if (keyCode == KeyEvent.KEYCODE_BACK) {if (IsTab != 1) {IsTab = 1;jumpHomeFragment();return true;}}return super.onKeyDown(keyCode, event);}/*** 显示主界面Fragemnt*/private void jumpHomeFragment() {title_bar_more.setVisibility(View.GONE);title_bar_change.setVisibility(View.GONE);setSelected(txt_menu_bottom_home);viewPager_content.setCurrentItem(TAB_HOME, false);setTitleName("首页");}/*** 切换巡店Fragment,提供给HomeFragment的培训查看调用*/public void jumpShopFragment() {//title_bar_more.setVisibility(View.VISIBLE);title_bar_change.setVisibility(View.VISIBLE);setSelected(txt_menu_bottom_shop);viewPager_content.setCurrentItem(TAB_SHOP, false);setTitleName("巡店");}/*** 显示培训TrainFragment,提供给HomeFragment的培训查看调用*/public void jumpTrainFragment() {title_bar_more.setVisibility(View.GONE);title_bar_change.setVisibility(View.GONE);setSelected(txt_menu_bottom_train);viewPager_content.setCurrentItem(TAB_TRAIN, false);setTitleName("培训");}/*** 显示拜访VisitFragment,提供给新建拜访完成后调用*/public void jumpVisitsFragment() {title_bar_more.setVisibility(View.VISIBLE);title_bar_change.setVisibility(View.GONE);setSelected(txt_menu_bottom_visit);viewPager_content.setCurrentItem(TAB_VISIT, false);setTitleName("拜访");}
}
感觉本节没什么重点,都是些比较简单的东西吧代码粘贴出来,方便以后用
下一张,我们进行首页轮播图的加载,如果下午在单位看完了,晚上回家就休息下,感觉最近好累呀,坚持一天一个章节
这篇关于课工场 “微服私访”项目学习(四)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!