Fragment Add实现

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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值