实现的功能是可以一个动态切换的fragment,可以通过图片点击切换fragment,图片也会相应改变
这里有两种做法
第一种
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
第二种
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
这两种效果都差不多,这里使用的第二种
我代码中有相应的提示,这里就不做太多讲解了
MainActivity.java
package com.example.fragmentTest;
import com.example.fragment.R;
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.ImageView;
//继承FragmentActivity
public class MainActivity extends FragmentActivity {
private ImageView wode;
private ImageView tuijian;
private ImageView lixian;
private ImageView daohang;
private ImageView faxian;
private tuijian ftTuijian;
//fragment的定义
private wode fWode;
private lixian fLixian;
private daohang fDaohang;
private faxian fFaxian;
private listener l;
private FragmentManager fragmentManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化控件
initView();
//初始化默认下表图片
defaultImage();
//初始化fragment
selectFragment(0);
}
//建议使用隐藏,可以节约资源
private void hideFragment(FragmentTransaction transaction){
//如果检验上一个是否添加进来,如果有就隐藏一起,因为新的进来
if(ftTuijian != null)
transaction.hide(ftTuijian);
if(fWode != null)
transaction.hide(fWode);
if(fDaohang != null)
transaction.hide(fDaohang);
if(fFaxian != null)
transaction.hide(fFaxian);
if(fLixian != null)
transaction.hide(fLixian);
}
//移除fragment已经存在的fragment,但是这样操作是很消耗资源的
private void removeFragment(FragmentTransaction transaction){
//如果fragment已经存在就remove,这样就不会冲突
if(ftTuijian != null)
transaction.remove(ftTuijian);
if(fWode != null)
transaction.remove(fWode);
if(fDaohang != null)
transaction.remove(fDaohang);
if(fFaxian != null)
transaction.remove(fFaxian);
if(fLixian != null)
transaction.remove(fLixian);
}
//选择fragment
//index自定的数据,调用传入需要现实的对应的fragment
private void selectFragment(int index) {
// TODO Auto-generated method stub
//设置事务管理器
FragmentTransaction transaction = fragmentManager.beginTransaction();
//remove比较消耗资源,因为每次都需要new,所以比较消耗资源,
//removeFragment(transaction);
//hide比较好的做法,可以最大限度节约资源
hideFragment(transaction);
switch (index) {
case 0:
//如果第一次进来就new一个,并且add
if(ftTuijian == null){
ftTuijian = new tuijian();
transaction.add(R.id.fl, ftTuijian);
}else{
//第二进来就不new,add浪费资源
transaction.show(ftTuijian);
}
break;
case 1:
if(fWode == null){
fWode = new wode();
transaction.add(R.id.fl, fWode);
}else{
transaction.show(fWode);
}
break;
case 2:
if(fLixian == null){
fLixian = new lixian();
transaction.add(R.id.fl, fLixian);
}else{
transaction.show(fLixian);
}
break;
case 3:
if(fFaxian == null){
fFaxian = new faxian();
transaction.add(R.id.fl, fFaxian);
}else{
transaction.show(fFaxian);
}
break;
case 4:
if(fDaohang == null){
fDaohang = new daohang();
transaction.add(R.id.fl, fDaohang);
}else{
transaction.show(fDaohang);
}
break;
default:
break;
}
//提交事务
transaction.commit();
}
//初始化view控件
private void initView() {
//监听事件
l = new listener();
//事务管理器,对应的是
fragmentManager = getSupportFragmentManager();
wode = (ImageView)findViewById(R.id.wode);
tuijian = (ImageView)findViewById(R.id.tuijian);
lixian = (ImageView)findViewById(R.id.lixian);
daohang = (ImageView)findViewById(R.id.daohang);
faxian = (ImageView)findViewById(R.id.faxian);
wode.setOnClickListener(l);
tuijian.setOnClickListener(l);
lixian.setOnClickListener(l);
daohang.setOnClickListener(l);
faxian.setOnClickListener(l);
}
//设置监听时间
class listener implements OnClickListener{
@Override
public void onClick(View v) {
clearImage();
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.wode:
//选到对应图标将图标改变
wode.setImageResource(R.drawable.wode2);
//选择对应的fragment显示
selectFragment(1);
break;
case R.id.tuijian:
tuijian.setImageResource(R.drawable.tuijian2);
selectFragment(0);
break;
case R.id.lixian:
lixian.setImageResource(R.drawable.lixian2);
selectFragment(2);
break;
case R.id.faxian:
faxian.setImageResource(R.drawable.faxian2);
selectFragment(3);
break;
case R.id.daohang:
daohang.setImageResource(R.drawable.daohang2);
selectFragment(4);
break;
default:
break;
}
}
}
//初始化选定下面默认的图片
public void defaultImage(){
tuijian.setImageResource(R.drawable.tuijian2);
}
//图片重置
public void clearImage(){
tuijian.setImageResource(R.drawable.tuijian1);
lixian.setImageResource(R.drawable.lixian1);
wode.setImageResource(R.drawable.wode1);
faxian.setImageResource(R.drawable.faxian1);
daohang.setImageResource(R.drawable.daohang1);
}
}
activity_main.xml
<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"
tools:context="com.example.fragmentTest.MainActivity" >
<!-- FrameLayout作为Fragment的容器,可以在java代码种动态修改
可以装载fragment -->
<FrameLayout
android:id="@+id/fl"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp">
</FrameLayout>
<View
android:layout_height="0.5dp"
android:layout_marginBottom="10dp"
android:layout_width="match_parent"
android:background="#888888"/>
<!-- 点击图片切换不同的fragment -->
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="50dp">
<ImageView
android:id="@+id/daohang"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:src="@drawable/daohang1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageView
android:id="@+id/faxian"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:src="@drawable/faxian1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageView
android:id="@+id/lixian"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:src="@drawable/lixian1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageView
android:id="@+id/tuijian"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:src="@drawable/tuijian1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageView
android:id="@+id/wode"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:src="@drawable/wode1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
对应的5个fragment:daohang.java,faxian.java,lixian.java,tuijian.java,wode.java
package com.example.fragmentTest;
import com.example.fragment.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 daohang extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.daohang, container, false);
return view;
}
}
package com.example.fragmentTest;
import com.example.fragment.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 faxian extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.faxian, container, false);
return view;
}
}
package com.example.fragmentTest;
import com.example.fragment.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
//这就是一个fragment
public class lixian extends Fragment{
//需要重写onCreateView();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.lixian, container, false);
//返回view,这个对应一个layout
return view;
}
}
package com.example.fragmentTest;
import com.example.fragment.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 tuijian extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.tuijian, container, false);
return view;
}
}
package com.example.fragmentTest;
import com.example.fragment.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 wode extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.wode, container, false);
return view;
}
}
5个fragment对应的布局:daohang.xml,faxian.xml,lixian.xml,tuijian.xml,wode.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#FF0000"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:textSize="30sp"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="daohang"/>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#FFFF00"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:textSize="30sp"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="faxian"/>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#00FF00"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:textSize="30sp"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="lixian"/>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#00BBFF"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:textSize="30sp"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="tuijian"/>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#FF00FF"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:textSize="30sp"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="wode"/>
</RelativeLayout>
结果图: