156.n1-fragment创建有侧边栏的MainActivity

侧边栏的内容是固定的,主页的内容是变化的,因此不能通过一个xml实现所有的内容,需要使用fragment分别实现侧边栏和主页的内容。

将activity_main.xml设置成FrameLayout,空布局,然后往里面填充Fragment;填同时左侧边栏layout_menu.xml也设置成FrameLayout,空布局,然后往里面填充Fragment.这样可以实现左侧边栏和内容的独立布局。

内容的布局activity_main.xml,然后往空的frameLayout中填充fragment

<?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>

内容填充的fragment,fragment_content.xml

<?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>

侧边栏的布局left_menu.xml,然后往空的frameLayout中填充fragment

<?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>

侧边栏填充的fragment,fragment_left_menu.xml

<?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>

基类的fragment初始化fragment的生命周期方法BaseFragment.java,处理activity的布局的时候会初始化fragment的布局来填充内容

package com.ldw.news.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);
		//获取到activity对象
		mActivity = getActivity();
	}

	// 处理fragment的布局
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		//创建fragement的布局
		return initViews();
	}

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

		initData();
	}

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

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

	}

}
内容的fragment填充ContentFragment.java

package com.ldw.news.fragment;

import com.ldw.news.R;

import android.view.View;

/*
 * 主界面
 */
public class ContentFragment extends BaseFragment{

	//初始化布局
	@Override
	public View initViews() {
		View view = View.inflate(mActivity, R.layout.fragment_content, null);
		return view;
	}

}

左侧边栏的填充LeftMenuFragment.java

package com.ldw.news.fragment;

import com.ldw.news.R;

import android.view.View;

/*
 * 左侧边栏的内容
 */
public class LeftMenuFragment extends BaseFragment{

	//初始化布局
	@Override
	public View initViews() {
		View view = View.inflate(mActivity, R.layout.fragment_left_menu, null);
		return view;
	}

}

MainActivity.java的逻辑,让fragment来填充布局

package com.ldw.news;

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

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;
import com.ldw.news.fragment.ContentFragment;
import com.ldw.news.fragment.LeftMenuFragment;

/*
 * 主界面
 */
public class MainActivity extends SlidingFragmentActivity {

	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);
        
        
        initView();
	}

	private void initView() {
		setContentView(R.layout.activity_main);
        
		
		//设置左侧边栏
        setBehindContentView(R.layout.left_menu);
        //获取侧边栏对象
        SlidingMenu slidingMenu = getSlidingMenu();
        //设置全屏触摸
        slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
        //设置预留屏幕的宽度
        slidingMenu.setBehindOffset(200);
        
        initFragment();
        
		
	}
	
	/*
	 * 初始化fragment,将fragment填充给布局文件
	 */
	private void initFragment() {
		FragmentManager fm = getSupportFragmentManager();
		FragmentTransaction transaction = fm.beginTransaction();// 开启事务
		//左侧边栏用布局替代(填充)
		//第三个参数就是给fragment取一个名字,可以使用fm.findFragmentByTag(FRAGMENT_LEFT_MENU);
		transaction.replace(R.id.fl_left_menu, new LeftMenuFragment(),
				FRAGMENT_LEFT_MENU);// 用fragment替换framelayout
		//内容的填充
		transaction.replace(R.id.fl_content, new ContentFragment(),
				FRAGMENT_CONTENT);

		transaction.commit();// 提交事务
		//使用第三个参数
		// Fragment leftMenuFragment = fm.findFragmentByTag(FRAGMENT_LEFT_MENU);
	}

}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值