侧边栏的内容是固定的,主页的内容是变化的,因此不能通过一个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);
}
}