Android实战之淘宝领券(三)
主界面分析与设计
- 修改颜色样式
- 首页界面上面的contain分析
- 提取基类
- MainActivity加入四个Fragment
- 底部导航栏的切换事件
修改颜色样式
colorPrimary、colorPrimaryDark、colorAccent
这三个颜色代表的是哪里的颜色呢?
一张图,大家都明白了把。
所以我们把颜色修改一下。
<color name="colorPrimary">#F5A623</color><color name="colorPrimaryDark">#F5A623</color><color name="colorAccent">#F5A623</color>
首页界面上面的contain分析
homeFragment 可以按下图解析
我们头部代码
<RelativeLayoutandroid:layout_width="match_parent"android:layout_height="40dp"android:background="@color/colorPrimaryDark"android:gravity="center_vertical"><TextViewandroid:layout_marginLeft="10dp"android:id="@+id/logo"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Luffy"android:textColor="@color/white"android:textSize="20sp" /><ImageViewandroid:paddingRight="10dp"android:id="@+id/scan_icon"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentRight="true"android:src="@mipmap/scan_white" /><EditTextandroid:id="@+id/home_search"android:paddingLeft="10dp"android:paddingRight="10dp"android:layout_marginLeft="10dp"android:layout_marginRight="10dp"android:layout_toRightOf="@+id/logo"android:layout_width="match_parent"android:layout_toLeftOf="@id/scan_icon"android:background="@drawable/shape_edit_box_bg"android:focusable="false"android:hint="@string/search_tips_text"android:textSize="14sp"android:layout_height="30dp" /></RelativeLayout>
TabLayout 和ViewPager
<com.google.android.material.tabs.TabLayoutandroid:id="@+id/home_indicator"android:layout_width="match_parent"android:layout_height="30dp"android:background="@color/colorPrimaryDark"app:tabIndicatorColor="@color/white"app:tabMode="scrollable"app:tabRippleColor="@color/colorTabNormal"app:tabSelectedTextColor="@color/colorSelected"app:tabTextColor="@color/colorTabNormal" /><androidx.viewpager.widget.ViewPagerandroid:id="@+id/home_pager"android:layout_width="match_parent"android:layout_height="match_parent" />
提取基类
因为我们有四个Fragment,所以为了避免代码重复,我们可以提取一个基类BaseFragment,让其他类继承。
@Overridepublic View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {View baseView =inflater.inflate(getRootViewId(),container,false);return baseView;}
//让子类返回一个ID,用于加载
protected abstract int getRootViewId();protected View loadRootView(LayoutInflater inflater, ViewGroup container) {return inflater.inflate(R.layout.fragment_base,container,false);}
这个时候我们建首页,精选,特惠,搜索。四个fragment继承BaseFragment
public class HomeFragment extends BaseFragment{@Overrideprotected int getRootViewId() {return R.layout.fragment_home;}
}
public class ChoicenessFragment extends BaseFragment{@Overrideprotected int getRootViewId() {return R.layout.fragment_choiceness;}
}
public class OnSellFragment extends BaseFragment{@Overrideprotected int getRootViewId() {return R.layout.fragment_on_sell;}
}
public class SearchFragment extends BaseFragment{@Overrideprotected int getRootViewId() {return R.layout.fragment_search;}
}
MainActivity加入四个Fragment
public abstract class MainActivity extends AppCompatActivity {//绑定@BindView(R.id.main_navigation_bar)BottomNavigationView mBar;//必须是public 或者 无修饰符。@Overrideprotected void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(getLayoutId());//第三方框架,用于初始化ViewButterKnife.bind(this);
// 初始化界面initView();
// 初始化事件initEvent();
// 初始化presentinitPresent();}@Overrideprotected void initView() {mHomeFragment = new HomeFragment();mRedPacketFragment = new OnSellFragment();mSearchFragment = new SearchFragment();mChoicenessFragment = new ChoicenessFragment();mFragmentManager = getSupportFragmentManager();switchFragment(mHomeFragment);}
}
底部导航栏的切换事件
- 设置导航栏item被选中的监听事件
根据选中的ID,来切换Fragment - 使用FragmentTransaction 把targetFragment都添加进去
如果添加有,就show,否则就add
如果lastFragment 上一个fragment存在并且,
targetFragment !=lastFragment;就隐藏上一个
@param targetFragment :添加/显示的Fragment
@Overrideprotected void initEvent() {//设置导航栏item被选中的监听事件//根据选中的ID,来切换FragmentmBar.setOnNavigationItemSelectedListener(item -> {switch (item.getItemId()) {case R.id.home:LogUtils.d(MainActivity.this, (item.getItemId() == R.id.home) + "被选中" + item.getTitle());switchFragment(mHomeFragment);break;case R.id.selected:LogUtils.d(MainActivity.this, (item.getItemId() == R.id.selected) + "被选中" + item.getTitle());switchFragment(mChoicenessFragment);break;case R.id.red_packet:LogUtils.d(MainActivity.this, (item.getItemId() == R.id.red_packet) + "被选中" + item.getTitle());switchFragment(mRedPacketFragment);break;case R.id.search:LogUtils.d(MainActivity.this, (item.getItemId() == R.id.search) + "被选中" + item.getTitle());switchFragment(mSearchFragment);break;}return true;});}private BaseFragment lastFragment = null;/*** 使用FragmentTransaction 把targetFragment都添加进去* 如果添加有,就show,否则就add* 如果lastFragment 上一个fragment存在并且,* targetFragment !=lastFragment;就隐藏上一个* @param targetFragment :添加/显示的Fragment*/private void switchFragment(BaseFragment targetFragment) {FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();if (!targetFragment.isAdded()) {fragmentTransaction.add(R.id.main_page_container, targetFragment);} else {fragmentTransaction.show(targetFragment);}if (lastFragment != null && lastFragment != targetFragment) {fragmentTransaction.hide(lastFragment);}lastFragment = targetFragment;
// fragmentTransaction.replace(R.id.main_page_container,targetFragment);fragmentTransaction.commit();}
Android实战之淘宝领券(三)相关推荐
- Python项目实战 —— 04. 淘宝用户行为分析
Python项目实战 Python项目实战--目录 Python项目实战 -- 04. 淘宝用户行为分析 一.背景 二.解题思路 三.数据分析 3.1 数据清洗 3.2 数据分析 3.2.1 用户整体 ...
- Python实战:淘宝自动抢购
Python实战:淘宝自动抢购 淘宝的限时抢购活动让许多消费者都感到头痛,往往在短时间内就被抢空了.有没有想过用Python写一个抢购程序呢?今天我们就来一起学习如何使用Python实现淘宝自动抢购. ...
- Android Studio 集成淘宝三方登录
满足下面条件1-- classpath 'com.android.tools.build:gradle:2.3.2' 版本不要高于3.02-- 通过阿里百川开通应用3-- 项目开通过电商能力4-- 项 ...
- Android Virtualview:淘宝、天猫又开源了一个动态化、高性能的UI框架
转载 Carson_Ho Android Virtualview:淘宝.天猫又开源了一个动态化.高性能的UI框架 前言 目录 1. 为什么要向 Tangram模型 加入 VirtualView 2. ...
- 数据分析实战之淘宝用户行为分析(3) -- 用户行为路径分析
数据分析实战之淘宝用户行为分析(3) -- 用户行为路径分析 淘宝行为分析Tableau可视化展示 数据分析实战之淘宝用户行为分析(5) - 数据分析报告 数据分析实战之淘宝用户行为分析(4) - 用 ...
- Android Virtualview:淘宝、天猫 又一个动态化、高性能的UI框架力作
Android Virtualview:淘宝.天猫 又一个动态化.高性能的UI框架力作 前言 淘宝.天猫一直致力于解决 页面动态化的问题 在2017年的4月发布了v1.0解决方案:Tangram模型 ...
- 阿里百川V6安全图,uniapp使用阿里百川SDK V6安全图唤醒淘宝APP 阿里百川新版本打开淘宝领券页面/渠道备案,uniapp对接淘宝联盟等等超级详细
阿里百川V6安全图,uniapp使用阿里百川SDK V6安全图唤醒淘宝APP 阿里百川新版本打开淘宝领券页面/渠道备案,uniapp对接淘宝联盟等等超级详细 此文章是针对插件市场1.6版本升级的介绍 ...
- GitHub 优秀的 Android 开源项目 淘宝技术牛p博客整理开发中最常用的GitHub上 优秀的 Android 开源项目整理(精品)...
原文地址为http://www.trinea.cn/android/android-open-source-projects-view/,作者Trinea 主要介绍那些不错个性化的View,包括Lis ...
- Python 淘宝系列(三): 模拟登陆成功后获取购物车信息
http://my.oschina.net/u/811744/blog/192604(本文的转载地址) ================================================ ...
- Android App打开淘宝(店铺或商品)
目录 准备工作 逻辑 具体实现 在官方淘宝开放平台查询打开淘宝APP文档,未果.各处查找资料,其中参考了<应用内打开京东.淘宝指定商品或店铺页面>:发现未安装淘宝app时在WebView ...
最新文章
- Ubuntu16.04 永久修改主机名 hostname
- 使用HttpWebRequest请求API接口以及其他网站资源
- 11 个简练的 Java 性能调优技巧
- ASP.NET MVC 使用防伪造令牌来避免CSRF攻击
- 分布式事物框架TCC-Transaction使用教程
- Mysql学习总结(50)——Oracle,mysql和SQL Server的区别
- html5标签属性大全_html5 文本相关标签
- 生活过得很苦 不知道什么时候才能解脱
- 【matlab安装】手把手图文并茂安装matlab2021(win10版)
- ccc加拿大计算机竞赛在线评测系统,加拿大CCC计算机竞赛
- 实验七 构件图和部署图
- 36氪开放日 —— 笔记
- ubuntu18.04安装PCL点云库踩坑指南
- Springboot毕设项目电商系统设计与实现t32la(java+VUE+Mybatis+Maven+Mysql)
- 计算机毕设(附源码)JAVA-SSM化妆品销售网站
- 爬虫结合批量下载评书、有声书、戏曲等的使用教程
- codeblocks编译出错问题的解答!(编译c++ 或者c程序)
- 东京工业大学 计算机 大学院,2020年东京工业大学计算机科学硕士申请条件
- Java中split方法详细讲解
- Linux常用指令整理