Android Fragment实现底部导航

要实现上面的这种底部导航,以前我们都是用tabhost来实现,在android3.0之后android新出了一个fragment,这个比tabhost更简单实现,具体的实现方法,我就直接上代码给大家,很简单的,
首先是xml文件activity_main.xml

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

    <LinearLayout 
        android:id="@+id/main_down_lin"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true"
        android:background="#000000"
        >
        <TextView 
            android:id="@+id/main_down_homepage"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:paddingTop="10dp"
            android:paddingBottom="10dp"
            android:textColor="#ffffff"
            android:gravity="center"
            android:text="首页"
            />
        <TextView 
            android:id="@+id/main_down_mall"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:paddingTop="10dp"
            android:paddingBottom="10dp"
            android:textColor="#ffffff"
            android:gravity="center"
            android:text="商城"
            />
        <TextView 
            android:id="@+id/main_down_circle"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:paddingTop="10dp"
            android:paddingBottom="10dp"
            android:textColor="#ffffff"
            android:gravity="center"
            android:text="圈子"
            />
        <TextView 
            android:id="@+id/main_down_admin"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:paddingTop="10dp"
            android:paddingBottom="10dp"
            android:textColor="#ffffff"
            android:gravity="center"
            android:text="管理"
            />
    </LinearLayout>
    
    <LinearLayout 
        android:id="@+id/main_top_lin"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/main_down_lin"
        >
        <fragment 
            android:id="@+id/main_fragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:name="com.dy.lianxi.fragment.HomePageFragment"
            />
    </LinearLayout>

</RelativeLayout>

activity文件MainActivity

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.graphics.Color;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

@SuppressLint("NewApi")
public class MainActivity extends Activity implements OnClickListener {
	
	private HomePageFragment homePageFragment;
	private CircleFragment circleFragment;
	private AdminFragment adminFragment;
	private MallFragment mallFragment;
	
	Fragment curr;//当前的fragment
	
	private TextView main_down_homepage;
	private TextView main_down_mall;
	private TextView main_down_circle;
	private TextView main_down_admin;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		homePageFragment = new HomePageFragment();
		circleFragment = new CircleFragment();
		adminFragment = new AdminFragment();
		mallFragment = new MallFragment();
		
		initview();
		
		curr = getFragmentManager().findFragmentById(R.id.main_fragment);
		setTextColor(main_down_homepage);
		
		main_down_homepage.setOnClickListener(this);
		main_down_mall.setOnClickListener(this);
		main_down_circle.setOnClickListener(this);
		main_down_admin.setOnClickListener(this);
	}
	
	private void initview() {
		main_down_homepage = (TextView) this.findViewById(R.id.main_down_homepage);
		main_down_mall = (TextView) this.findViewById(R.id.main_down_mall);
		main_down_circle = (TextView) this.findViewById(R.id.main_down_circle);
		main_down_admin = (TextView) this.findViewById(R.id.main_down_admin);

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.main_down_homepage://首页
			FragmentSwitch(homePageFragment);
			setTextColor(main_down_homepage);
			break;
		case R.id.main_down_mall://商城
			FragmentSwitch(mallFragment);
			setTextColor(main_down_mall);
			break;
		case R.id.main_down_circle://圈子
			FragmentSwitch(circleFragment);
			setTextColor(main_down_circle);
			break;
		case R.id.main_down_admin://管理
			FragmentSwitch(adminFragment);
			setTextColor(main_down_admin);
			break;
		}
		
	}
	
	//切换fragment
	private void FragmentSwitch(Fragment fragment) {
		if(curr != fragment){
			//开始事物
			FragmentTransaction ft = getFragmentManager().beginTransaction();
			if(!fragment.isAdded()){
				//没找到fragment就添加
				ft.add(R.id.main_top_lin, fragment);
			}else{
				//找到了就显示
				ft.show(fragment);
			}
			
			//判断当前的fragment是否和传进来的fragment是同一个
			if(curr == fragment){
				//true 显示fragment
				ft.show(fragment);
			}else {
				//false 隐藏当前的fragment
				ft.hide(curr);
			}
			
			//关闭事物
			ft.commitAllowingStateLoss();
			
			//将fragment的值赋给curr
			curr = fragment;
		}

	}
	
	//变化文字颜色
	private void setTextColor(TextView tv) {
		
		main_down_homepage.setTextColor(Color.WHITE);
		main_down_mall.setTextColor(Color.WHITE);
		main_down_circle.setTextColor(Color.WHITE);
		main_down_admin.setTextColor(Color.WHITE);
		
		tv.setTextColor(Color.RED);

	}


	
}

fragment 文件HomePageFragment

import android.annotation.SuppressLint;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

@SuppressLint("NewApi")
public class HomePageFragment extends Fragment {
	
	View view;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		
		view = inflater.inflate(R.layout.fragment_homepage, container, false);
		
		
		return view;
	}

}

资源下载


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值