Android开发之旅一fragment选项卡的实现

时间:2022-06-13 06:28:53

今天来看看fragment选项卡的实现,更多的可以查看API

package com.lfj.testfragment;

import com.example.testfragment.R;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Window;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class TestFragment extends FragmentActivity{

/**
* fragment使用android.support.v4.app.Fragment可以向下兼容
*/

private Fragment[] mFragment;
private FragmentManager mFragmentManager;
private FragmentTransaction mFragmentTransaction;

private RadioGroup tab; //选项卡
private RadioButton tab_home,tab_search,tab_setting;

@Override
protected void onCreate(Bundle arg0) {
// TODO Auto-generated method stub
super.onCreate(arg0);

requestWindowFeature(Window.FEATURE_NO_TITLE);

this.setContentView(R.layout.testmain);

this.mFragment=new Fragment[3];
this.mFragmentManager=getSupportFragmentManager();

//初始化三个fragment
mFragment[0]=this.mFragmentManager.findFragmentById(R.id.fragement_home);
mFragment[1]=this.mFragmentManager.findFragmentById(R.id.fragement_search);
mFragment[2]=this.mFragmentManager.findFragmentById(R.id.fragement_setting);

mFragmentTransaction = mFragmentManager.beginTransaction()
.hide(mFragment[0]).hide(mFragment[1]).hide(mFragment[2]);

//默认显示首页
mFragmentTransaction.show(mFragment[0]).commit();

this.setFragmentIndicator();

}

public void setFragmentIndicator(){

this.tab=(RadioGroup) findViewById(R.id.tab);
this.tab_home=(RadioButton) findViewById(R.id.tab_home);
this.tab_search=(RadioButton) findViewById(R.id.tab_search);
this.tab_setting=(RadioButton) findViewById(R.id.tab_setting);

this.tab.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub

mFragmentTransaction = mFragmentManager.beginTransaction()
.hide(mFragment[0]).hide(mFragment[1])
.hide(mFragment[2]);

switch (checkedId) {
case R.id.tab_home:
//设置fragment切换动画
mFragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
mFragmentTransaction.show(mFragment[0]).commit();

break;
case R.id.tab_search:

mFragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
mFragmentTransaction.show(mFragment[1]).commit();

break;

case R.id.tab_setting:

mFragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
mFragmentTransaction.show(mFragment[2]).commit();


break;

default:
break;
}
}
});
}

public void setSelect(int selectID){
for(int i=0;i<mFragment.length;i++){
if(selectID == i){

}
}
}

}

<?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" >

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/tab"
android:orientation="vertical" >

<fragment
android:id="@+id/fragement_home"
android:name="com.lfj.testfragment.Fragment_home"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="10" />

<fragment
android:id="@+id/fragement_search"
android:name="com.lfj.testfragment.Fragment_search"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="10" />

<fragment
android:id="@+id/fragement_setting"
android:name="com.lfj.testfragment.Fragment_setting"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="10" />
</LinearLayout>

<RadioGroup
android:id="@+id/tab"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:background="@drawable/bg">

<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tab_home"
android:background="@drawable/select"
android:button="@null"
android:checked="true"
android:drawableTop="@drawable/home_n"
android:gravity="center"
android:text="首页"
android:layout_weight="1"/>

<RadioButton
android:id="@+id/tab_search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/select"
android:button="@null"
android:drawableTop="@drawable/search_n"
android:gravity="center"
android:text="搜索"
android:layout_weight="1"/>

<RadioButton
android:id="@+id/tab_setting"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/select"
android:button="@null"
android:drawableTop="@drawable/menu_n"
android:gravity="center"
android:text="设置"
android:layout_weight="1"/>
</RadioGroup>

</RelativeLayout>

</LinearLayout>

package com.lfj.testfragment;

import com.example.testfragment.R;

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

/**
* 继承fragment
*
* @author Joke
*
*/
public class Fragment_home extends Fragment{
private View view;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub

this.view=inflater.inflate(R.layout.home, container, false);

return view;
}

}

package com.lfj.testfragment;

import com.example.testfragment.R;

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

public class Fragment_search extends Fragment{
private View view;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub

this.view=inflater.inflate(R.layout.search, container, false);

return view;
}

}

package com.lfj.testfragment;

import com.example.testfragment.R;

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

public class Fragment_setting extends Fragment{
private View view;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub

this.view=inflater.inflate(R.layout.setting, container, false);

return view;
}

}

<?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"
android:background="@drawable/login_1">

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="首页"
android:textSize="50dp"
android:gravity="center"/>

</LinearLayout>

<?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"
android:background="@drawable/login_2" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="搜索"
android:textSize="50dp"/>

</LinearLayout>

<?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"
android:background="@drawable/login_3">

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="设置"
android:textSize="50dp" />

</LinearLayout>


Android开发之旅一fragment选项卡的实现



源码下载