Fragment+Framelayout 小复习 实现简单的 tab
来源:互联网 发布:什么是网络金融 编辑:程序博客网 时间:2024/05/29 18:02
把基础的都快忘了 汗
有时候一些需求需要很多的页面 一般的处理是
1.一些页面放到一个activitiy 缺点:业务逻辑和页面处理逻辑会在乱不堪
2.一个页面一个activitiy 缺点:增大activitiy的开销
也是在api 11以上有了fragment
具体做法如下:
在布局中放一个盛放fragment的容器 这里这容器是framlayout
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="a" android:text="Button" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="b" android:text="Button" /> </LinearLayout> <FrameLayout android:id="@+id/myframe" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > </FrameLayout></LinearLayout>
然后
在fragmentTransaction处理替换这个View 以达到显示多个页面的目的 而处理逻辑交个FragmentA b c d就OK
package com.example.testfragment;import android.os.Bundle;import android.support.v4.app.FragmentActivity;import android.support.v4.app.FragmentManager;import android.support.v4.app.FragmentTransaction;import android.view.View;public class MainActivity extends FragmentActivity {private FragmentTransaction fragmentTransaction;private FragmentManager fragmentManager;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);fragmentManager = getSupportFragmentManager();}public void a(View view) {AFragment fragment = new AFragment();fragmentTransaction = fragmentManager.beginTransaction();fragmentTransaction.replace(R.id.myframe, fragment);fragmentTransaction.commit();}public void b(View view) {BFragment fragment = new BFragment();fragmentTransaction = fragmentManager.beginTransaction();fragmentTransaction.replace(R.id.myframe, fragment);fragmentTransaction.commit();}}
package com.example.testfragment;import android.os.Bundle;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.TextView;public class AFragment extends Fragment {@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {TextView textView = new TextView(getActivity());textView.setText("Fragment A");return textView;}}
package com.example.testfragment;import android.os.Bundle;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.TextView;public class BFragment extends Fragment{@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {TextView textView = new TextView(getActivity());textView.setText("Fragment B");return textView;}}
890.62 KB, 下载次数: 0, 下载积分: e币 -2 元
- Fragment+Framelayout 小复习 实现简单的 tab
- FrameLayout布局实现简单tab
- Fragment实现的Tab标签
- 使用Framelayout+Fragment+tab方式展示首页时,个别fragment有时会出现空白情况的解决
- framelayout中fragment的切换
- framelayout中fragment的切换
- framelayout中fragment的切换
- FragmentTabHost实现Fragment的tab效果
- 使用Fragment实现类似Tab的需求
- Fragment + RadioGroup 实现类似于Tab的效果
- FrameLayout的简单运用
- fragment实现android tab
- Fragment实现tab功能
- 使用fragment实现tab
- Fragment实现Tab
- Fragment实现Tab
- fragment实现Tab效果
- tab+fragment实现主页
- 马云:政治改革跟你一点关系都没有 不要动不动就谈
- SQL语句中的通配符
- android:java.lang.OutOfMemoryError: bitmap size exceeds VM budget---缩略图
- ios 用到的数据
- 开发常用下载地址收藏
- Fragment+Framelayout 小复习 实现简单的 tab
- vector C++ 详细用法
- 获得手机信息工具类
- vmware中的bridge、nat区别
- 希尔排序(C语言实现)
- 编制求解定积分的通用函数
- WCF笔记(9)群聊程序
- 模拟“地雷游戏”
- ViewPager中ViewPager的使用