课工场 “微服私访”项目学习(四)

2023-11-04 00:21

本文主要是介绍课工场 “微服私访”项目学习(四),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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("拜访");}
}

感觉本节没什么重点,都是些比较简单的东西吧代码粘贴出来,方便以后用
下一张,我们进行首页轮播图的加载,如果下午在单位看完了,晚上回家就休息下,感觉最近好累呀,坚持一天一个章节

这篇关于课工场 “微服私访”项目学习(四)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/341914

相关文章

javafx 如何将项目打包为 Windows 的可执行文件exe

《javafx如何将项目打包为Windows的可执行文件exe》文章介绍了三种将JavaFX项目打包为.exe文件的方法:方法1使用jpackage(适用于JDK14及以上版本),方法2使用La... 目录方法 1:使用 jpackage(适用于 JDK 14 及更高版本)方法 2:使用 Launch4j(

Docker集成CI/CD的项目实践

《Docker集成CI/CD的项目实践》本文主要介绍了Docker集成CI/CD的项目实践,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学... 目录一、引言1.1 什么是 CI/CD?1.2 docker 在 CI/CD 中的作用二、Docke

SpringBoot项目引入token设置方式

《SpringBoot项目引入token设置方式》本文详细介绍了JWT(JSONWebToken)的基本概念、结构、应用场景以及工作原理,通过动手实践,展示了如何在SpringBoot项目中实现JWT... 目录一. 先了解熟悉JWT(jsON Web Token)1. JSON Web Token是什么鬼

手把手教你idea中创建一个javaweb(webapp)项目详细图文教程

《手把手教你idea中创建一个javaweb(webapp)项目详细图文教程》:本文主要介绍如何使用IntelliJIDEA创建一个Maven项目,并配置Tomcat服务器进行运行,过程包括创建... 1.启动idea2.创建项目模板点击项目-新建项目-选择maven,显示如下页面输入项目名称,选择

Jenkins中自动化部署Spring Boot项目的全过程

《Jenkins中自动化部署SpringBoot项目的全过程》:本文主要介绍如何使用Jenkins从Git仓库拉取SpringBoot项目并进行自动化部署,通过配置Jenkins任务,实现项目的... 目录准备工作启动 Jenkins配置 Jenkins创建及配置任务源码管理构建触发器构建构建后操作构建任务

Nginx、Tomcat等项目部署问题以及解决流程

《Nginx、Tomcat等项目部署问题以及解决流程》本文总结了项目部署中常见的four类问题及其解决方法:Nginx未按预期显示结果、端口未开启、日志分析的重要性以及开发环境与生产环境运行结果不一致... 目录前言1. Nginx部署后未按预期显示结果1.1 查看Nginx的启动情况1.2 解决启动失败的

HarmonyOS学习(七)——UI(五)常用布局总结

自适应布局 1.1、线性布局(LinearLayout) 通过线性容器Row和Column实现线性布局。Column容器内的子组件按照垂直方向排列,Row组件中的子组件按照水平方向排列。 属性说明space通过space参数设置主轴上子组件的间距,达到各子组件在排列上的等间距效果alignItems设置子组件在交叉轴上的对齐方式,且在各类尺寸屏幕上表现一致,其中交叉轴为垂直时,取值为Vert

Ilya-AI分享的他在OpenAI学习到的15个提示工程技巧

Ilya(不是本人,claude AI)在社交媒体上分享了他在OpenAI学习到的15个Prompt撰写技巧。 以下是详细的内容: 提示精确化:在编写提示时,力求表达清晰准确。清楚地阐述任务需求和概念定义至关重要。例:不用"分析文本",而用"判断这段话的情感倾向:积极、消极还是中性"。 快速迭代:善于快速连续调整提示。熟练的提示工程师能够灵活地进行多轮优化。例:从"总结文章"到"用

这15个Vue指令,让你的项目开发爽到爆

1. V-Hotkey 仓库地址: github.com/Dafrok/v-ho… Demo: 戳这里 https://dafrok.github.io/v-hotkey 安装: npm install --save v-hotkey 这个指令可以给组件绑定一个或多个快捷键。你想要通过按下 Escape 键后隐藏某个组件,按住 Control 和回车键再显示它吗?小菜一碟: <template

【前端学习】AntV G6-08 深入图形与图形分组、自定义节点、节点动画(下)

【课程链接】 AntV G6:深入图形与图形分组、自定义节点、节点动画(下)_哔哩哔哩_bilibili 本章十吾老师讲解了一个复杂的自定义节点中,应该怎样去计算和绘制图形,如何给一个图形制作不间断的动画,以及在鼠标事件之后产生动画。(有点难,需要好好理解) <!DOCTYPE html><html><head><meta charset="UTF-8"><title>06