Android Tab实现之FragmentManager+Fragment

这种实现方法主要是

1.在布局中使用FrameLayout

2.不同的页面继承Fragment父类,重写onCreateView()方法,在此方法中定义需要返回的View对象

3.使用Fragment Manager去管理

主页面布局:

<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:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">


    <FrameLayout
        android:id="@+id/fl_content"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <include layout="@layout/bottom_menu" />


</LinearLayout>

主页面代码:

package com.example.baidu.mydemos.demo1;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.widget.FrameLayout;
import android.widget.RadioGroup;

import com.example.baidu.mydemos.R;

/**
 * Created by baidu on 15/12/30.
 */
public class DemoOneMenuTwoActivity extends Activity implements RadioGroup.OnCheckedChangeListener {

    private FrameLayout fl_content;
    private RadioGroup rg_bottom;
    private FragmentManager fragmentManager;

    //tab views
    private AppFragmentOneContent appFragmentOneContent;
    private AppFragmentTwoContent appFragmentTwoContent;
    private AppFragmentThreeContent appFragmentThreeContent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.demo1_2);
        fragmentManager = getFragmentManager();
        rg_bottom = (RadioGroup) findViewById(R.id.rg_bottom);
        rg_bottom.setOnCheckedChangeListener(this);
        initData();

    }

    public void initData() {
        setTabContent(0);
        rg_bottom.check(R.id.rb_1);
    }

    public void setTabContent(int index) {
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        switch (index) {
            case 0:
                if (appFragmentOneContent == null) {
                    appFragmentOneContent = new AppFragmentOneContent();
                }
                transaction.replace(R.id.fl_content, appFragmentOneContent, "fragmentone");
                break;
            case 1:
                if (appFragmentTwoContent == null) {
                    appFragmentTwoContent = new AppFragmentTwoContent();
                }
                transaction.replace(R.id.fl_content, appFragmentTwoContent, "fragmenttwo");
                break;
            case 2:
                if (appFragmentThreeContent == null) {
                    appFragmentThreeContent = new AppFragmentThreeContent();
                }
                transaction.replace(R.id.fl_content, appFragmentThreeContent, "fragmentthree");
        }
        transaction.commit();
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.rb_1:
                setTabContent(0);
                break;
            case R.id.rb_2:
                setTabContent(1);
                break;
            case R.id.rb_3:
                setTabContent(2);
                break;
        }

    }
}

Fragment所使用的布局文件:

<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:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:gravity="center"
    tools:context=".MainActivity">

<TextView
    android:id="@+id/tv_content"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="25sp"
    android:text="content"/>
    
</LinearLayout>


Fragment代码:

package com.example.baidu.mydemos.demo1;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.baidu.mydemos.R;

/**
 * Created by baidu on 15/12/30.
 */
public class FragmentOneContent extends Fragment {
    public FragmentOneContent() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.demo1_content, null);
        TextView tv_content = (TextView) view.findViewById(R.id.tv_content);
        tv_content.setText("content 1");
        return view;
    }
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值