首页
网站首页
公司简介
资讯中心
推荐内容
返回顶部
左侧菜单栏折叠展开效果,首页管理
发布时间:2020-02-15 21:05
浏览次数:

新萄京手机版,在开发过程中,有时候业务要求需要对移动端和PC端分别处理,如果是移动端,那么返回页面A,如果是PC端,那么返回页面B,而判断浏览器是移动端还是PC端,我们一般通过user-agent判断,服务端和前端js都可以获取到user-agent并作出判断,这里分享的是服务端做这个处理。java代码:

我见过很多种,写也过很多种 - 首页管理 Fragment ,这里我觉得有必要整理一下思路,为依然不是很顺手的朋友提供下思路

分享一个常见于管理后台的左侧菜单栏折叠展开的效果,基于jquery,效果图如下:

葡京国际娱乐,先说点题外话,最近报名了简书的日更计划,所以每天都会写一篇文章,字数不会太多,但是内容都会经过认真准备的,希望大家也能从中学到一些有用的知识,一起进步。

 /** * 判断是否是手机浏览器 * @param request * @return */ public static Boolean isMoblieBrowser(HttpServletRequest request) { Boolean isMoblie = false; String[] mobileAgents = { "iphone", "android", "phone", "mobile", "wap", "netfront", "java", "opera mobi", "opera mini", "ucweb", "windows ce", "symbian", "series", "webos", "sony", "blackberry", "dopod", "nokia", "samsung", "palmsource", "xda", "pieplus", "meizu", "midp", "cldc", "motorola", "foma", "docomo", "up.browser", "up.link", "blazer", "helio", "hosin", "huawei", "novarra", "coolpad", "webos", "techfaith", "palmsource", "alcatel", "amoi", "ktouch", "nexian", "ericsson", "philips", "sagem", "wellcom", "bunjalloo", "maui", "smartphone", "iemobile", "spice", "bird", "zte-", "longcos", "pantech", "gionee", "portalmmm", "jig browser", "hiptop", "benq", "haier", "^lct", "320x320", "240x320", "176x220", "w3c ", "acs-", "alav", "alca", "amoi", "audi", "avan", "benq", "bird", "blac", "blaz", "brew", "cell", "cldc", "cmd-", "dang", "doco", "eric", "hipt", "inno", "ipaq", "java", "jigs", "kddi", "keji", "leno", "lg-c", "lg-d", "lg-g", "lge-", "maui", "maxo", "midp", "mits", "mmef", "mobi", "mot-", "moto", "mwbp", "nec-", "newt", "noki", "oper", "palm", "pana", "pant", "phil", "play", "port", "prox", "qwap", "sage", "sams", "sany", "sch-", "sec-", "send", "seri", "sgh-", "shar", "sie-", "siem", "smal", "smar", "sony", "sph-", "symb", "t-mo", "teli", "tim-", "tosh", "tsm-", "upg1", "upsi", "vk-v", "voda", "wap-", "wapa", "wapi", "wapp", "wapr", "webc", "winw", "winw", "xda", "xda-", "Googlebot-Mobile" }; String ua = request.getHeader("user-agent"); if (org.apache.commons.lang3.StringUtils.isNotBlank { for (String mobileAgent : mobileAgents) { if (ua.toLowerCase().indexOf(mobileAgent) >= 0) { isMoblie = true; break; } } } return isMoblie; }

1. Fragment 切换方式

首页的 Fragment 是要求无缝切换的,换言之就是不能卡,要一瞬间完成,有时产品还要加上一点小动画效果

这里我们推荐 show/hide 这套组合,我们不用维护 currentFragment ,逻辑复杂, 还容易出 bug ,最好的思路是遍历 FragmentManager ,看看谁当前显示者,然后 hind,show 我们想要的就好,简单干净

 @Override protected void initListener() { super.initListener(); navigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(@NonNull MenuItem item) { switch (item.getItemId { case R.id.menu_library: switchFragment(libraryFragment); break; case R.id.menu_welfare: if (welfareFragment == null) { welfareFragment = new WelfareFragment(); } switchFragment(welfareFragment); break; case R.id.menu_my: if (myFragment == null) { myFragment = new MyFragment(); } switchFragment(myFragment); break; } return true; } });

 private void switchFragment(Fragment fragment) { List<Fragment> fragments = fm.getFragments(); for (Fragment f : fragments) { if (!f.equals && !f.isHidden { fm.beginTransaction.commit(); } } if (fragment.isAdded { fm.beginTransaction().show.commit(); } else { fm.beginTransaction().add(R.id.container, fragment).commit(); } }

新萄京手机版 1

今天介绍的是谷歌推荐的 MVP新奥门葡京娱乐场手机, 写法,它的项目地址是 android-architecture

这里将常用到的手机浏览器ua关键词列举出来放在一个数组里,然后获取当前浏览器的ua,判断当前浏览器的ua是否包含上面数组里的某一个,若找到了包含的元素,则是手机浏览器。测试方法代码:

2. 第一个 Fragment 的处理

第一个 Fragment 我们直接在 onCreate 中初始化,addFragment 进去

 @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(bindLayout; libraryFragment = new LibraryFragment(); addFragment(R.id.rootView, libraryFragment); }

 private void addFragment(int layoutID, Fragment fragment) { if (fragment.isAdded { fm.beginTransaction().show.commit(); } else { fm.beginTransaction().add(layoutID, fragment).commit(); } }

一:页面结构:

我对它的主要代码结构做了一个截图,今天就下面这种图来认识一下规范的MVP写法。

 @RequestMapping public ModelAndView test(HttpServletRequest request) { ModelAndView mv = new ModelAndView(); System.out.println("是否手机浏览器:"+isMoblieBrowser; if(isMoblieBrowser{ mv.setViewName; }else{ mv.setViewName; } return mv; }

3. 首页异常关闭处理

AC 若是由系统回收,屏幕旋转或是异常关闭,会保存 app 中各种信息的,AC 堆栈数据,Fragment 堆栈数据。这里说首页,首页要是这样被关闭然后由系统再起来,那么就会恢复之前我们在首页添加的所有 Fragment 页面,并且这样页面都在显示,也就是所谓的穿透 bug

样子大概是这样的:

新萄京手机版 2

若是这种情况,我们需要处理 onSaveInstanceState 和 onRestoreInstanceState 2个方法

3.1 onCreate 里面 savedInstanceState == null,表示正常启动

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //初始化 init(); //根据传入的Bundle对象判断Activity是正常启动还是销毁重建 if(savedInstanceState == null){ //设置第一个Fragment默认选中 setFragment; } }

3.2 维护一个 fragmentId 或是一个 tag

3.3 onSaveInstanceState 中保存当前显示的 Fragment 标记,这个可以是上面自己记录的fragmentId ,也可以遍历 FragmentManage,找到其中 !isHidden(),获取其 TAG

@Override protected void onSaveInstanceState(Bundle outState) { //通过onSaveInstanceState方法保存当前显示的fragment outState.putInt("fragment_id",fragmentId); super.onSaveInstanceState; }

3.4 onRestoreInstanceState 获取所有的 Fragment ,然后显示标记的 ,hidden 其他的

@Override protected void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); FragmentManager mFragmentManager = getSupportFragmentManager(); //通过FragmentManager获取保存在FragmentTransaction中的Fragment实例 mClothesFragment = (ClothesFragment)mFragmentManager .findFragmentByTag("clothes_fragment"); mFoodFragment = (FoodFragment)mFragmentManager .findFragmentByTag("food_fragment"); mHotelFragment = (HotelFragment)mFragmentManager .findFragmentByTag("hotel_fragment"); //恢复销毁前显示的Fragment setFragment(savedInstanceState.getInt("fragment_id")); }

具体可以参考:

  • Android实践| Fragment实现底部导航栏
<!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8"> <title></title></head><body><div > <ul> <li > <a href="javascript:;">首页</a> </li> <li > <a href="javascript:;">菜单1</a> <ul> <li > <a href="javascript:;">子菜单1-1</a> </li> <li > <a href="javascript:;">子菜单1-2</a> </li> <li > <a href="javascript:;">子菜单1-3</a> </li> <li > <a href="javascript:;">子菜单1-4</a> </li> </ul> </li> <li > <a href="javascript:;">菜单2</a> <ul> <li > <a href="javascript:;">子菜单2-1</a> </li> <li > <a href="javascript:;">子菜单2-2</a> </li> <li > <a href="javascript:;">子菜单2-3</a> </li> </ul> </li> <li > <a href="javascript:;">菜单3</a> <ul> <li > <a href="javascript:;">子菜单3-1</a> </li> <li > <a href="javascript:;">子菜单3-2</a> </li> </ul> </li> </ul></div></body></html>

新萄京手机版 3代码结构

友情链接: 网站地图
Copyright © 2015-2019 http://www.nflfreepicks.net. 新葡萄京娱乐场网址有限公司 版权所有