BaseFragment类
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public abstract class BaseFragment extends Fragment {
public Context context;
public BaseFragment(Context context) {
this.context = context;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return initView(inflater, container);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
loadData();
}
public abstract void loadData();
public abstract View initView(LayoutInflater inflater, ViewGroup container);
}
Fragment类之一,代码简单主要为了示例,其余三个Fragment代码类似。
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class OneFragment extends BaseFragment {
public OneFragment(Context context) {
super(context);
}
@Override
public void loadData() {
}
@Override
public View initView(LayoutInflater inflater, ViewGroup container) {
TextView tv=new TextView();
tv.setText("one");
return tv;
}
MainActivity主要代码
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity implements View.OnClickListener {
private OneFragment fragment1;
private TwoFragment fragment2;
private ThrFragment fragment3;
private FourFragment fragment4;
private FragmentManager fm;
private TextView one, two, thr, four;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
one = (TextView) findViewById(R.id.one);
two = (TextView) findViewById(R.id.two);
thr = (TextView) findViewById(R.id.thr);
four = (TextView)findViewById(R.id.four);
one.setOnClickListener(this);
two.setOnClickListener(this);
thr.setOnClickListener(this);
four.setOnClickListener(this);
// 设置默认的Fragment
setDefaultFragment();
}
private void setDefaultFragment(){
FragmentManager fm = getFragmentManager();
if (fragment1 == null) {
fragment1 =new OneFragment(MainActivity.this);
}
fm.beginTransaction()
.add(R.id.framelayout, fragment1)
.commit();
}
@Override
public void onClick(View v) {
fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
hidden(ft);
switch (v.getId()) {
case R.id.one:
if (fragment1 == null) {
fragment1 = new OneFragment(MainActivity.this);
ft.add(R.id.framelayout, fragment1);
} else {
ft.show(fragment1);
}
break;
case R.id.two:
if (fragment2 == null) {
fragment2 = new TwoFragment(MainActivity.this);
ft.add(R.id.framelayout, fragment2);
} else {
ft.show(fragment2);
}
break;
case R.id.thr:
if (fragment3 == null) {
fragment3 = new ThrFragment(MainActivity.this);
ft.add(R.id.framelayout, fragment3);
} else {
ft.show(fragment3);
}
break;
case R.id.four:
if (fragment4 == null) {
fragment4 = new FourFragment(MainActivity.this);
ft.add(R.id.framelayout, fragment4);
} else {
ft.show(fragment4);
}
break;
}
ft.commit();
}
private void hidden(FragmentTransaction ft) {
if (fragment1 != null) {
ft.hide(fragment1);
}
if (fragment2 != null) {
ft.hide(fragment2);
}
if (fragment3 != null) {
ft.hide(fragment3);
}
if (fragment4 != null) {
ft.hide(fragment4);
}
}
}
MainActivty Xml布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/framelayout"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:background="#000"
android:layout_above="@+id/ll"
android:layout_width="match_parent"
android:layout_height="1px" />
<LinearLayout
android:id="@+id/ll"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_alignParentBottom="true">
<TextView
android:id="@+id/one"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="one"
android:textSize="20sp" />
<TextView
android:layout_width="1px"
android:layout_height="match_parent"
android:background="#000" />
<TextView
android:id="@+id/two"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="two"
android:textSize="20sp" />
<TextView
android:layout_width="1px"
android:layout_height="match_parent"
android:background="#000" />
<TextView
android:id="@+id/thr"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="thr"
android:textSize="20sp" />
<TextView
android:layout_width="1px"
android:layout_height="match_parent"
android:background="#000" />
<TextView
android:id="@+id/four"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="four"
android:textSize="20sp" />
</LinearLayout>
</RelativeLayout>