实现fragment的方法有很多,例如FragmentTabHost,replace,实现fragment切换,今天我为大家介绍一个显示隐藏的方法。
这个就是实现显示隐藏的运行类,简单方便,不会出现切换的时候,这个界面的布局复用到下一个界面
package cn.bgs.fragment1;
import cn.bgs.fragment1.fragment.HomeFragment;
import cn.bgs.fragment1.fragment.JPFragment;
import cn.bgs.fragment1.fragment.TJFragment;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
public class MainActivity extends FragmentActivity implements OnClickListener {
private TextView mHome,mTuijian,mJingpin;//三个按钮
//创建三个碎片的对象
private HomeFragment mHomeFragment;
private JPFragment mJPFragment;
private TJFragment mTJFragment;
//创建碎片管理器
private FragmentManager manager;//碎片管理器
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
manager=getSupportFragmentManager();//获取碎片管理器
initView();
goneFragment();
//默认到第二个碎片
selectFragment(R.id.tuijian_frag);
}
private void initView() {
mHome=(TextView) findViewById(R.id.first_frag);
mTuijian=(TextView) findViewById(R.id.tuijian_frag);
mJingpin=(TextView) findViewById(R.id.jingpin_frag);
mHome.setOnClickListener(this);
mTuijian.setOnClickListener(this);
mJingpin.setOnClickListener(this);
}
@Override
public void onClick(View v) {
int ID=v.getId();
goneFragment();
selectFragment(ID);
}
//根据ID选择碎片的方法
private void selectFragment(int ID){
//创建碎片事务管理 每一次碎片的显示与隐藏都要通过事务管理来操作
FragmentTransaction ft=manager.beginTransaction();
//操作:碎片为空 创建添加
// 碎片不为空,直接显示
switch (ID) {
case R.id.first_frag:
if(mHomeFragment==null){//为空,创建
mHomeFragment=new HomeFragment();//创建
ft.add(R.id.frag_group, mHomeFragment);//将碎片添加到专门存放碎片的容器中
}else{
ft.show(mHomeFragment);//不为空,直接显示
}
break;
case R.id.tuijian_frag:
if(mTJFragment==null){
mTJFragment=new TJFragment();
ft.add(R.id.frag_group, mTJFragment);
}else{
ft.show(mTJFragment);
}
break;
case R.id.jingpin_frag:
if(mJPFragment==null){
mJPFragment=new JPFragment();
ft.add(R.id.frag_group, mJPFragment);
}else{
ft.show(mJPFragment);
}
break;
}
ft.commit();//提交 一定一定要提交
}
//隐藏所有碎片的饿方法
private void goneFragment(){
FragmentTransaction ft=manager.beginTransaction();
if(mHomeFragment!=null){
ft.hide(mHomeFragment);
}
if(mTJFragment!=null){
ft.hide(mTJFragment);
}
if(mJPFragment!=null){
ft.hide(mJPFragment);
}
ft.commit();
}
}
下面是几个frament,我也为大家展示一下:
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;
/*
* Fragment:碎片 1.继承自Fragment(V4包下)
* 2.重写下面的oncontentView的方法
* */
public class HomeFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//用java代码实现布局 动态布局
TextView tv=new TextView(getActivity());
tv.setText("我是首页!!!");
tv.setTextSize(30);
return tv;
}
}
import cn.bgs.fragment1.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 JPFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v=LayoutInflater.from(getActivity()).inflate(R.layout.jingpin_fragment, null);
return v;
}
}
import cn.bgs.fragment1.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 TJFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v=View.inflate(getActivity(), R.layout.tuijian_frgment, null);
return v;
}
}
下面是Mainactivity的布局:
<RelativeLayout 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"
tools:context=".MainActivity" >
<LinearLayout
android:id="@+id/bottom_linear"
android:layout_width="match_parent"
android:layout_height="45dp"
android:background="#28c8b7"
android:gravity="center"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
>
<TextView
android:id="@+id/first_frag"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="首页"
android:textSize="18sp" />
<TextView
android:id="@+id/tuijian_frag"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="推荐"
android:textSize="18sp" />
<TextView
android:id="@+id/jingpin_frag"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="精品"
android:textSize="18sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/frag_group"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/bottom_linear"
/>
</RelativeLayout>
<?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" >
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/ic_launcher"
/>
</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" >
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/aaaaaa"
android:scaleType="fitXY"
/>
</LinearLayout>