类似于微信功能点击跳转到相对应的界面,应用到hide,show
来源:互联网 发布:悠悠奔途 知乎 编辑:程序博客网 时间:2024/05/02 15:46
实现点击导航任意按钮,跳转到相对应的界面,实现方法可以用viewpage+fragment,还有就是用到hide,show,一般倾向于第二种,比较节省空间
xml界面:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <FrameLayout android:id="@+id/layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" > </FrameLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:onClick="myfrag" android:text="我的" /> <Button android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:onClick="wxfrag" android:text="微信" /> <Button android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:onClick="frfrag" android:text="好友" /> <Button android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:onClick="findfrag" android:text="发现" /> </LinearLayout></LinearLayout>创建四个fragment界面,重写onCreateView()加载布局,此处省略
主代码:
/* * 页面转换改良代码,避免切换页面时,前一个页面被销毁, * 改良后,切换页面时,只是把前一个页面隐藏起来,需要时在显示出来 */public class MainActivity extends FragmentActivity {private MyFrag f1;private WeiXin f2;private Friend f3;private Find f4;// 设置变量private Fragment currentFragement;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);if (f1 == null) {f1 = new MyFrag();}setFrag(f1);}// 监听方法public void myfrag(View v) {// 防止多次创建对象,重复添加,只创建一次if (f1 == null) {f1 = new MyFrag();}setFrag(f1);}public void wxfrag(View v) {if (f2 == null) {f2 = new WeiXin();}setFrag(f2);}public void frfrag(View v) {if (f3 == null) {f3 = new Friend();}setFrag(f3);}public void findfrag(View v) {if (f4 == null) {f4 = new Find();}setFrag(f4);}// 动态加载fragment方法public void setFrag(Fragment fr) {// 通过管理器获得事务FragmentTransaction action = getSupportFragmentManager().beginTransaction();// 隐藏前一个fragmentif (currentFragement != null) {action.hide(currentFragement);}// 判断fragment是否被添加过,一个fragment只能提交一次,所以当第二次点击同一个// button时,要判断一下是否添加过,如果添加过就不在添加,如果没添加过就添加if (!fr.isAdded()) {action.add(R.id.layout, fr);} else {action.show(fr);}// 提交action.commit();// 记录当前的fragmentcurrentFragement = fr;}}
许多的App都会用到此功能,以上代码就可以实现点击任意按钮跳转到相对应的界面,其实很简单
0 0
- 类似于微信功能点击跳转到相对应的界面,应用到hide,show
- Androd中 listView点击每一个 Item里面的状态,跳转到相对应的不同 界面
- app根据URL跳转到AppStore相对应的软件
- 点击每个省份的并跳转到各自的界面
- MFC登录界面,跳转到功能界面
- 百度消息推送点击跳转到指定界面的实现
- 利用photoView实现可点击放大到全屏显示的控件,效果类似于微信朋友圈点开看大图
- Vue.js通过api数据点击跳到相对应的页数
- android 点击数字跳转到电话界面
- android:微信分享到回话 ,无法跳转到微信界面
- 关于日历点击跳转以及短信点击跳转到指定联系人界面的一些记录
- 自己的应用跳转到应用宝评分界面
- 类似于微信通讯录的界面Demo
- 类似于微信通讯录的界面Demo
- UITextView设置超链接,点击跳转到应用内的webView
- 应用跳转到系统设置的WiFi界面
- 应用跳转到系统设置里的各种设置界面
- Android跳转到应用安装界面的方法
- java程序员面试笔试题(网络部分2)
- excel vba 导出lua配置文件
- Intellj IDEA光标变成了insert光标状态
- 2016年31款轻量高效的开源JavaScript插件和库
- 移动端开发真机调试神器——browser-sync
- 类似于微信功能点击跳转到相对应的界面,应用到hide,show
- swoole安装 以及简单的聊天功能
- 清空任务栈问题
- webmagic的设计机制及原理-如何开发一个Java爬虫
- Android:WebView与Javascript交互(相互调用参数、传值)
- TabLayout-ViewPager填充Fragment和懒加载
- @requestmapping
- Homebrew使用
- MySQL check the manual that corresponds to your MySQL server version for the right syntax错误