用Fragment模仿QQ界面

概要

在android开发中,Fragment是很重要的一部分,他有activity的特性也有他自己独特的生命周期,使activity代码更简洁

效果演示



Demo思考问题

1.如何实现的底部三个按钮效果?
2.用什么来承载Fragment
3.为什么设置默认的Fragment时单独放在一个方法中
4.如何进行Fragment的切换

Demo

首先,我们还是从主activity的界面文件开始

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context="com.example.administrator.icephonefhhxml.CustomerRecommendation">


    <LinearLayout
        android:id="@+id/button_list"
        android:layout_alignParentBottom="true"
        android:layout_width="fill_parent"
        android:layout_height="80dp"
        android:background="#efefef"
        android:orientation="horizontal"
        >
        <LinearLayout
            android:layout_width="120dp"
            android:layout_height="fill_parent"
            android:orientation="vertical"
            android:id="@+id/message">
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/iv_message"
                android:background="@drawable/recommend_icon_addressbook_pressed"
                android:layout_gravity="center"
                android:layout_marginTop="8dp"/>
            <TextView
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text="消息"
                android:gravity="center"/>
        </LinearLayout>
        <LinearLayout
            android:layout_width="120dp"
            android:layout_height="fill_parent"
            android:orientation="vertical"
            android:id="@+id/connect"
            >
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/iv_connect"
                android:background="@drawable/recommend_icon_album_pressed"
                android:layout_gravity="center"
                android:layout_marginTop="8dp"/>
            <TextView
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text="联系人"
                android:gravity="center"/>
        </LinearLayout>
        <LinearLayout
            android:layout_width="120dp"
            android:layout_height="fill_parent"
            android:orientation="vertical"
            android:id="@+id/the_new">
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/iv_new"
                android:background="@drawable/my_icon_ring01_pressed"
                android:layout_gravity="center"
                android:layout_marginTop="8dp"/>
            <TextView
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text="动态"
                android:gravity="center"/>
        </LinearLayout>


    </LinearLayout>
    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/main_board"
        android:layout_above="@id/button_list"/>
</RelativeLayout>

这里要注意的是最后声明的FrameLayout,他是用来承接Fragment的,这里一定要把他放到最后声明,具体原因留给大家思考~另外就是底下的三个按钮其实是三个layout,这种方法可以使按钮美观而且实现了图文结合的按钮

下面我们要实现三个Fragment,以便主Activity使用,以其中的一个为例

之前我们说过它和Activity相似,所以他也需要布局文件,下面是布局文件,名为
contain_fragment_first

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffff">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="消息"
        android:textSize="50sp"
        android:gravity="center"
        android:textColor="#7B7B7B"
        android:textStyle="bold"/>

</LinearLayout>

布局只是简单地配置一下,具体的内容我们以后再加
然后我们新建一个继承于Fragment的类
FragmentBottomFirst:
package com.example.administrator.icephonefhhxml.Fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.administrator.icephonefhhxml.R;

/**
 * Created by Administrator on 2015/6/15.
 */
public class FragmentBottomFirst extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {
        return inflater.inflate(R.layout.contain_fragment_first, container, false);
    }
}

这里只是简单地绑定布局文件,剩下的Fragment与第一个相似,只是名分别把first换成second、third......

然后我们就可以开始写Activity的代码了

package com.example.administrator.icephonefhhxml;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.view.View;
import android.view.View.OnClickListener;

import com.example.administrator.icephonefhhxml.Fragment.FragmentBottomFirst;
import com.example.administrator.icephonefhhxml.Fragment.FragmentBottomSecond;
import com.example.administrator.icephonefhhxml.Fragment.FragmentBottomThird;


public class CustomerRecommendation extends Activity implements OnClickListener   {
    //声明三个layout当做按钮监听fragment的切换
    private LinearLayout message;
    private LinearLayout connect;
    private LinearLayout theNew;
    //我们之前定义的三个Fragment,分别声明三个这样的类的对象
    private FragmentBottomFirst firstBoard;
    private FragmentBottomSecond secondBoard;
    private FragmentBottomThird thirdBoard;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_customer_recommendation);
        //实例化layout并配置监听器
        message = (LinearLayout)findViewById(R.id.message);
        connect = (LinearLayout)findViewById(R.id.connect);
        theNew = (LinearLayout)findViewById(R.id.the_new);

        message.setOnClickListener(this);
        connect.setOnClickListener(this);
        theNew.setOnClickListener(this);
        //用这个方法配置初始默认的Fragment
        startFragment();

    }

    private void startFragment()
    {
        FragmentManager fm = getFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        firstBoard = new FragmentBottomFirst();            //利用以上步骤实例化之前建立好的对象
        transaction.replace(R.id.main_board,firstBoard);   //把这个对象放到我们的Framelayout里
        transaction.commit();                              //提交事务执行操作
    }

    @Override
    public  void onClick(View v)
    {
        FragmentManager fm = getFragmentManager();

        FragmentTransaction transaction = fm.beginTransaction();

        switch (v.getId())
        {
            case R.id.message: {
                firstBoard = new FragmentBottomFirst();
                transaction.replace(R.id.main_board, firstBoard);
                break;
            }
            case R.id.connect: {
                secondBoard = new FragmentBottomSecond();
                transaction.replace(R.id.main_board, secondBoard);
                break;
            }
            case R.id.the_new: {
                thirdBoard = new FragmentBottomThird();
                transaction.replace(R.id.main_board, thirdBoard);
                break;
            }
        }
        transaction.commit();
    }


}


其中注意要把实例化放到方法里这样可以合理控制其生存周期,大家试着把方法里的代码拿出来看看会发生什么吧~

这样就完成了我们今天的任务,博文里留了很多悬念,大家如果有什么不明白的或者问题请加我QQ(为了防止人肉就不发出来了,个人信息里有哈~)

感想

上大学就是这样,不知道怎么就忙成狗~~会尽量更新的,多学一点东西,也尽量写一些高质量的博客!博客中如有不好的地方还望各路大神斧正~~

每日一句

成功过的路上并不拥挤,因为很少有人能够坚持!
  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值