android122 zhihuibeijing 主页面使用fragment搭建

                                   

fragment的生命周期:
onAttach()当fragment添加进Activity的时候调用(这个时候Activity对象已经存在了,不然就依赖不上去的)
onCreate()当fragment创建的时候调用。
onCreateView()当fragment画界面的时候调用。
onActivityCreated()当fragment依附的Activity创建完成的时候调用。
onStart(),onResume(),onPause(),onStop(),onDestroyView(),onDestroy().
onStart()对着onStop(),
onResume()对着onPause(),
onDestroyView()对着onCreateView(),
onDestroy()对着onCreate()。

 Mainactivity

package com.itheima.zhbj52;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

import com.itheima.zhbj52.fragment.ContentFragment;
import com.itheima.zhbj52.fragment.LeftMenuFragment;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;

/**
 * 主页面
 */
public class MainActivity extends SlidingFragmentActivity {//使用开源SlidingMenu框架实现侧边栏,

    private static final String FRAGMENT_LEFT_MENU = "fragment_left_menu";
    private static final String FRAGMENT_CONTENT = "fragment_content";

    @Override
    public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);//主页面是一个空的FrameLayout,后面会用Fragment填充。
          setBehindContentView(R.layout.left_menu);//侧边栏也是一个空的FrameLayout,后面会用Fragment填充,SlidingFragmentActivity这个库的方法,设置左侧边栏布局。
          SlidingMenu slidingMenu = getSlidingMenu();// 获取侧边栏对象
          slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);// 设置主屏幕可以全屏触摸进行滑动,
          slidingMenu.setBehindOffset(200);// 设置预留屏幕的宽度,左右侧边栏滑动的时候主屏幕最少有100像素。

          initFragment();
    }

    /**
     * 初始化fragment, 将fragment数据填充给布局文件
     */
    private void initFragment() {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();// 开启事务
        //fl_left_menu和fl_content位于不同的xml
        transaction.replace(R.id.fl_left_menu, new LeftMenuFragment(),
                FRAGMENT_LEFT_MENU);// 用fragment替换framelayout,FRAGMENT_LEFT_MENU是名字。
        transaction.replace(R.id.fl_content, new ContentFragment(),
                FRAGMENT_CONTENT);
        transaction.commit();// 提交事务
        // Fragment leftMenuFragment = fm.findFragmentByTag(FRAGMENT_LEFT_MENU);根据名字找fragment,
    }

}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
空布局,就是一个容器。
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fl_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

</FrameLayout>

left_menu.xml

<?xml version="1.0" encoding="utf-8"?>
空布局,就是一个容器。
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fl_left_menu"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

</FrameLayout>

LeftMenuFragment

package com.itheima.zhbj52.fragment;

import com.itheima.zhbj52.R;

import android.view.View;

/**
 * 侧边栏
 * 
 * @author Kevin
 * 
 */
public class LeftMenuFragment extends BaseFragment {

    @Override
    public View initViews() {
        View view = View.inflate(mActivity, R.layout.fragment_left_menu, null);
        return view;
    }

}

fragment_left_menu.xml

<RelativeLayout 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:background="#f00" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我是侧边栏!" />

</RelativeLayout>

ContentFragment

package com.itheima.zhbj52.fragment;

import com.itheima.zhbj52.R;

import android.view.View;

/**
 * 主页内容
 * 
 * @author Kevin
 * 
 */
public class ContentFragment extends BaseFragment {

    @Override
    public View initViews() {
        View view = View.inflate(mActivity, R.layout.fragment_content, null);
        return view;
    }

}

fragment_content.xml

<RelativeLayout 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" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我是主页面哦!" />

</RelativeLayout>

BaseFragment

package com.itheima.zhbj52.fragment;

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

/**
 * fragment基类
 */
public abstract class BaseFragment extends Fragment {

    public Activity mActivity;

    // fragment创建
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivity = getActivity();//拿到fragment所依赖Activity的对象,fragment创建创建的时候Activity对象肯定是已经存在了,所以这个对象不会是空的。
    }

    // 处理fragment的布局
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return initViews();//子类实现,因为每个布局不一样。
    }

    // 依附的activity创建完成
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        initData();
    }

    // 子类必须实现初始化布局的方法
    public abstract View initViews();

    // 初始化数据, 可以不实现
    public void initData() {
    }

}
原文地址:https://www.cnblogs.com/yaowen/p/5040283.html