android学习——用fragment仿微信主页
来源:互联网 发布:淘宝付款显示交易关闭 编辑:程序博客网 时间:2024/06/06 11:47
上一篇写完登陆界面,用户在登陆后进入主页,这里要写一个类似微信主页的功能,下面介绍具体步骤。
首先了解一下fragment:
Fragment是Activity的一个界面的一个组成部分,Activity的界面可以完全有不同的Fragment组成,Fragment拥有自己的生命周期和接收、处理用户的事件,可以适应各种不同尺寸的屏幕。
Fragment的生命周期:
可以看到Fragment比Activity多了几个额外的生命周期回调方法:
onAttach(Activity)
当Fragment与Activity发生关联时调用。
onCreateView(LayoutInflater, ViewGroup,Bundle)
创建该Fragment的视图
onActivityCreated(Bundle)
当Activity的onCreate方法返回时调用
onDestoryView()
与onCreateView想对应,当该Fragment的视图被移除时调用
onDetach()
与onAttach相对应,当Fragment与Activity关联被取消时调用
注意:除了onCreateView,其他的所有方法如果你重写了,必须调用父类对于该方法的实现。
Fragment家族常用的API
Fragment常用的三个类:
android.app.Fragment 主要用于定义Fragment
android.app.FragmentManager 主要用于在Activity中操作Fragment
android.app.FragmentTransaction 保证一些列Fragment操作的原子性,熟悉事务这个词,一定能明白~
a、获取FragmentManage的方式:
getFragmentManager() // v4中,getSupportFragmentManager
b、主要的操作都是FragmentTransaction的方法
FragmentTransaction transaction = fm.benginTransatcion();//开启一个事务
transaction.add()
往Activity中添加一个Fragment
transaction.remove()
从Activity中移除一个Fragment,如果被移除的Fragment没有添加到回退栈(回退栈后面会详细说),这个Fragment实例将会被销毁。
transaction.replace()
使用另一个Fragment替换当前的,实际上就是remove()然后add()的合体~
transaction.hide()
隐藏当前的Fragment,仅仅是设为不可见,并不会销毁
transaction.show()
显示之前隐藏的Fragment
detach()
会将view从UI中移除,和remove()不同,此时fragment的状态依然由FragmentManager维护。
attach()
重建view视图,附加到UI上并显示。
transatcion.commit()//提交一个事务
注意:常用Fragment的哥们,可能会经常遇到这样Activity状态不一致:State loss这样的错误。主要是因为:commit方法一定要在Activity.onSaveInstance()之前调用。
上述,基本是操作Fragment的所有的方式了,在一个事务开启到提交可以进行多个的添加、移除、替换等操作。
值得注意的是:如果你喜欢使用Fragment,一定要清楚这些方法,哪个会销毁视图,哪个会销毁实例,哪个仅仅只是隐藏,这样才能更好的使用它们。
a、比如:我在FragmentA中的EditText填了一些数据,当切换到FragmentB时,如果希望会到A还能看到数据,则适合你的就是hide和show;也就是说,希望保留用户操作的面板,你可以使用hide和show,当然了不要使劲在那new实例,进行下非null判断。
b、再比如:我不希望保留用户操作,你可以使用remove(),然后add();或者使用replace()这个和remove,add是相同的效果。
c、remove和detach有一点细微的区别,在不考虑回退栈的情况下,remove会销毁整个Fragment实例,而detach则只是销毁其视图结构,实例并不会被销毁。那么二者怎么取舍使用呢?如果你的当前Activity一直存在,那么在不希望保留用户操作的时候,你可以优先使用detach。
下面开始写一个仿微信主页的动态Fragment。
1,继续上一篇,在main_activity中布局:
<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" > <FrameLayout android:id="@+id/main_content" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="1" /> <RadioGroup android:id="@+id/tab_menu" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/tab_bg2" android:orientation="horizontal" > <RadioButton android:id="@+id/rbJob" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="bottom" android:layout_weight="1" android:button="@null" android:checked="true" android:background="@drawable/tab_selector_checked_bg" android:drawableTop="@drawable/tab_selector_job" android:gravity="center_horizontal|bottom" android:paddingTop="2dp" android:text="@string/job" android:textColor="@color/tab_selector_tv_color" /> <RadioButton android:id="@+id/rbEdu" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="bottom" android:background="@drawable/tab_selector_checked_bg" android:layout_weight="1" android:button="@null" android:drawableTop="@drawable/tab_selector_edu" android:gravity="center_horizontal|bottom" android:paddingTop="2dp" android:text="@string/edu" android:textColor="@color/tab_selector_tv_color" /> <RadioButton android:id="@+id/rbLife" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="bottom" android:layout_weight="1" android:background="@drawable/tab_selector_checked_bg" android:button="@null" android:drawableTop="@drawable/tab_selector_life" android:gravity="center_horizontal|bottom" android:paddingTop="2dp" android:text="@string/life" android:textColor="@color/tab_selector_tv_color" /> <RadioButton android:id="@+id/rbMe" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="bottom" android:layout_weight="1" android:background="@drawable/tab_selector_checked_bg" android:button="@null" android:drawableTop="@drawable/tab_selector_me" android:gravity="center_horizontal|bottom" android:paddingTop="2dp" android:text="@string/me" android:textColor="@color/tab_selector_tv_color" /> </RadioGroup></LinearLayout>这里面用到FrameLayout,注意android:layout_height="0dp" 和 android:layout_weight="1"这两个设置,这样RadioGroup才会显示在最下面。
2,在res下新建color文件夹,并新建一个tab_selector_tv_color.xml文件,用来设置点击按钮后,文字显示不同的颜色
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_checked="true" android:color="@android:color/white"/> <item android:state_checked="false" android:color="@android:color/darker_gray"/> <item android:color="@android:color/darker_gray"/></selector>
3,在res文件夹下新建drawable文件夹并新建tab_selector_checked_bg.xml文件,用来设置每次点击按钮后按钮背景变化
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/tab_bg1" android:state_checked="true"/></selector>4,在drawable文件夹新建tab_selector_job.xml文件,用来设置每次点击按钮后按钮图标的变化
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:state_checked="false" android:drawable="@drawable/tab_job2"></item> <item android:state_checked="true" android:drawable="@drawable/tab_job4"></item></selector>5,其他三个类似tab_selector_edu.xml,tab_selector_life.xml,tab_selector_me.xml
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:state_checked="false" android:drawable="@drawable/tab_edu2"></item> <item android:state_checked="true" android:drawable="@drawable/tab_edu4"></item></selector>
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:state_checked="false" android:drawable="@drawable/tab_life2"></item> <item android:state_checked="true" android:drawable="@drawable/tab_life4"></item></selector>
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:state_checked="false" android:drawable="@drawable/tab_me2"></item> <item android:state_checked="true" android:drawable="@drawable/tab_me4"></item></selector>
<?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" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="工作" /></LinearLayout>
其他三个根据自己需要进行布局
7,编写MainActivity
public class MainActivity extends FragmentActivity {private JobFragment job;private RadioGroup maTabRg;private EduFragment edu;private LifeFragment life;private MeFragment me;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main_activity);//实例化initView(),设置默认的Fragment initView();}private void initView() {job = new JobFragment();getSupportFragmentManager().beginTransaction().replace(R.id.main_content, job).commit();/** * 也可以这样写: * FragmentManager fm = getFragmentManager(); //这里是v4包,所以用getSupportFragmentManager * // 开启Fragment事务 FragmentTransaction transaction = fm.beginTransaction(); job = new ContentFragment(); transaction.replace(R.id.id_content, job); transaction.commit(); *///找到界面地下的tabmaTabRg = (RadioGroup) findViewById(R.id.tab_menu);//OnCheckedChangeListener监听maTabRg.setOnCheckedChangeListener(new OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) {switch (checkedId) {case R.id.rbJob:job = new JobFragment();getSupportFragmentManager().beginTransaction().replace(R.id.main_content, job).commit();break;case R.id.rbEdu:edu = new EduFragment();getSupportFragmentManager().beginTransaction().replace(R.id.main_content, edu).commit();break;case R.id.rbLife:life = new LifeFragment();getSupportFragmentManager().beginTransaction().replace(R.id.main_content, life).commit();break;case R.id.rbMe:me = new MeFragment();getSupportFragmentManager().beginTransaction().replace(R.id.main_content, me).commit();break;default:break;}}});}}
8,创建JobFragment、EduFragment、LifeFragment、MeFragment,代码
package com.linlif.cv.fragment;import com.linlif.cv.R;import android.annotation.SuppressLint;import android.support.v4.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class JobFragment extends Fragment {@SuppressLint("InflateParams")@Overridepublic View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {return inflater.inflate(R.layout.job_fragment, null);}}
这样就实现了。
- android学习——用fragment仿微信主页
- 仿微信主页fragment
- Android主页动态添加Fragment
- android学习——Fragment
- Android主页面 多层Fragment嵌套(Fragment+ViewPager)滑动
- Android学习笔记——Fragment
- android学习笔记——Fragment
- Android仿微信(三)——主页面实现篇
- (三)Android仿微信—主页面实现篇
- Ext学习2——主页面
- android学习笔记11(1)——Fragment学习笔记
- Android学习第十二篇——Fragment基础学习
- Android 开发Fragment类似微信主页切换
- 快速开发android应用4-使用viewpager+fragment构建主页
- 十一、主页fragment
- tab+fragment实现主页
- Fragment写主页
- Android——Fragment
- Java中的Process类
- mybatis 防止sql注入的原理
- UltraEdit怎么注册激活
- tfs中获得的Azure storm代码不能提交
- linux下使用Qt库进行开发心得
- android学习——用fragment仿微信主页
- IOS项目集成ShareSDK实现第三方登录、分享、关注等功能。
- linux下nohup日志输出过大问题解决方案
- ActivityStack 详解
- Shiro入门6:自定义realm查询数据库进行认证
- 最大化与最小化
- mac安装maven记录文档
- NSDateFormatter的格式串详解
- bootstrap之表单校验