1.布局文件
<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:background="#EFF0EF"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/top_usermanager_bar"
android:layout_width="match_parent"
android:layout_height="58dp"
android:background="#b5dff5" >
<TextView
android:id="@+id/btn_action_back"
android:layout_width="120dp"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:layout_margin="4dp"
android:background="@drawable/selector_holo_light_view"
android:drawableLeft="@drawable/ic_actionbar_back"
android:gravity="center"
android:onClick="onClick"
android:text="用户管理"
android:textColor="@color/white"
android:textSize="18sp" />
<View
android:layout_width="1.5px"
android:layout_height="35dp"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/btn_action_back"
android:background="@color/white" />
RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="150dp"
android:layout_height="match_parent"
android:background="#9ACFF0"
android:gravity="center_horizontal"
android:orientation="vertical" >
<Button
android:id="@+id/usermanager"
android:layout_width="135dp"
android:layout_height="60dp"
android:layout_margin="5dp"
android:background="#ffffff"
android:text="用户管理"
android:textColor="#be3131"
android:textSize="20dp" />
<Button
android:id="@+id/qxmanager"
android:layout_width="135dp"
android:layout_height="60dp"
android:layout_margin="5dp"
android:background="#ffffff"
android:text="权限管理"
android:textColor="#BE3131"
android:textSize="20dp" />
LinearLayout>
<View
android:layout_width="2dip"
android:layout_height="match_parent"
android:background="#ffffff" />
<FrameLayout
android:id="@+id/frametest"
android:layout_width="match_parent"
android:layout_height="match_parent" >
FrameLayout>
LinearLayout>
LinearLayout>
点击不同的按钮会framelayout会显示不同的内容,因为要为fragment设置不同的布局
用户管理布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户管理布局"
/>
LinearLayout>
权限管理布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="权限管理布局"
/>
LinearLayout>
这两个布局要通过两个fragment来进行加载显示
UserManager继承fragment加载用户管理布局(获取上下文使用getActivity())
public class UserManager extends Fragment{
View view;
ListView listView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view=inflater.inflate(R.layout.usermanager, container,false);
init();
return view;
}
private void init() {
List listItem = new ArrayList();
UserTest userTest1 = new UserTest("limz","李明珠","女","院长","18264751002");
listItem.add(userTest1);
listView = (ListView) view.findViewById(R.id.usermanagerlistview);
UserManagerListViewAdapter listViewAdapter = new UserManagerListViewAdapter(
getActivity(),listItem);
listView.setAdapter(listViewAdapter);
}
}
QXManager继承fragment加载权限管理权限布局(获取上下文使用getActivity())
public class QxManager extends Fragment{
View view;
ListView listView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view=inflater.inflate(R.layout.qxmanager, container,false);
init();
return view;
}
private void init() {
List listItem = new ArrayList();
UserTest userTest1 = new UserTest("limz","李明珠","女","院长","18264751002");
listItem.add(userTest1);
listView = (ListView) view.findViewById(R.id.usermanagerlistview);
QXManagerListViewAdapter listViewAdapter = new QXManagerListViewAdapter(
getActivity(),listItem);
listView.setAdapter(listViewAdapter);
}
}
在主activity中(切记我们这里导入的是v4包)
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
public class UserManagerActivity extends FragmentActivity implements
OnClickListener {
private TextView textViewBack;
private Button usermanagerButton, qxmanagerButton;
private QxManager qxManager;
private UserManager userManager;
private FragmentManager fManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_manager);
init();
}
private void init() {
textViewBack = (TextView) findViewById(R.id.btn_action_back);
usermanagerButton = (Button) findViewById(R.id.usermanager);
qxmanagerButton = (Button) findViewById(R.id.qxmanager);
usermanagerButton.setOnClickListener(this);
qxmanagerButton.setOnClickListener(this);
textViewBack.setOnClickListener(this);
selectframement(1);
}
@Override
public void onClick(View arg0) {
switch (arg0.getId()) {
case R.id.qxmanager:
selectframement(2);
break;
case R.id.usermanager:
selectframement(1);
break;
case R.id.btn_action_back:
Intent intent = new Intent(getApplicationContext(),MainActivity.class);
startActivity(intent);
break;
}
}
private void selectframement(int i) {
fManager = getSupportFragmentManager();//在v4包中获取FragmentManager 对象(不在v4包中获取FragmentManager 对象是gettFragmentManager())
FragmentTransaction tramscation = fManager.beginTransaction();
hideFragment(tramscation);
switch (i) {
case 2:
qxmanagerButton.setBackgroundColor(Color.parseColor("#ffffff"));
usermanagerButton.setBackgroundColor(Color.parseColor("#9ACFF0"));
if (qxManager == null) {
//如果布局加载对象为空,先创建对象,然后将内容add到布局里面
qxManager = new QxManager();
//参数一:fragelayout布局的ID,参数二:要加载显示的布局对象
tramscation.add(R.id.frametest, qxManager);
} else {
//如果布局加载对象不为空则直接展示布局加载对象
tramscation.show(qxManager);
}
break;
case 1:
usermanagerButton .setBackgroundColor(Color.parseColor("#ffffff"));
qxmanagerButton.setBackgroundColor(Color.parseColor("#9ACFF0"));
if (userManager == null) {
//如果布局加载对象为空,先创建对象,然后将内容add到布局里面
userManager = new UserManager();
//参数一:fragelayout布局的ID,参数二:要加载显示的布局对象
tramscation.add(R.id.frametest, userManager);
} else {
//如果布局加载对象不为空则直接展示布局加载对象
tramscation.show(userManager);
}
break;
default:
break;
}
tramscation.commit();
}
private void hideFragment(FragmentTransaction tramscation) {
if (qxManager != null) {
tramscation.hide(qxManager);
}
if (userManager != null) {
tramscation.hide(userManager);
}
}
}