您的位置:首页 > 移动开发 > 微信开发

android学习——用fragment仿微信主页

2016-03-24 13:41 603 查看
上一篇写完登陆界面,用户在登陆后进入主页,这里要写一个类似微信主页的功能,下面介绍具体步骤。

首先了解一下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>


6,创建res/layout/job_fragment.xml,用来在fragment中显示的内容
<?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;

@Override
protected 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();
*/

//找到界面地下的tab
maTabRg = (RadioGroup) findViewById(R.id.tab_menu);

//OnCheckedChangeListener监听
maTabRg.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override
public 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")
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {

return inflater.inflate(R.layout.job_fragment, null);
}
}


这样就实现了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: