底部导航栏点击跳转fragment

网上找了很多底部导航栏fragment切换,感觉各种方法都试了,下面是我自己整理的,希望能帮到大家。

(1)点击图片变色的xml文件写好。图片自己处理好。一个为例,其三个同理。



    
    
(2)layout布局文件的引用。



    
        
            
                
                
                
                
            
        
        
    

自己定义的style→tab_button_style



    
    


 
  
tabcolor_selector.xml文件的定义


    
    

(3)建立四个跳转用的fragment的布局文件,对应的fragment.clas

callfragment.xml




CallFragment.class

package com.ncsyeyy.YeyyNavigationBar;
import android.app.Activity;
import android.content.AsyncQueryHandler;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.CallLog;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 15-4-13.
 */
public class CallFragment extends Fragment {



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.callfragment, null);

        return view;
    }


}


 
  (5)在主的activity中的使用 
  

package com.ncsyeyy.YeyyNavigationBar;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.RadioButton;

public class MyActivity extends FragmentActivity {
    private RadioButton call;
    private RadioButton addressList;
    private RadioButton pay;
    private RadioButton more;
    private boolean isKeyboardVisible = true;
    private boolean isFragmentVisible = true;
    private Handler mHandler;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        call = (RadioButton) findViewById(R.id.call);
        addressList = (RadioButton) findViewById(R.id.addressList);
        pay = (RadioButton) findViewById(R.id.pay);
        more = (RadioButton) findViewById(R.id.more);

        //默认显示拨号
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        Fragment fragment = new CallFragment();
        ft.replace(R.id.content, fragment);
        ft.commit();

        //拨号显示
        call.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
                    Fragment fragment = new CallFragment();
                    ft.replace(R.id.content, fragment);
                    ft.commit();
                    call.setCompoundDrawablesRelativeWithIntrinsicBounds(null,getResources().getDrawable(R.drawable.call_menu_up),null,null);
                    call.setTextColor(getResources().getColor(R.color.TextColorBlue));
                    isKeyboardVisible = true;
                    isFragmentVisible = true;
            }
        });

        //通讯录显示
        addressList.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
                Fragment fragment = new AddressListFragment();
                ft.replace(R.id.content, fragment);
                ft.commit();
                isKeyboardVisible = false;
                isFragmentVisible = false;
                call.setCompoundDrawablesRelativeWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.tab_dial_selector), null, null);
                call.setTextColor(getResources().getColor(R.color.TextColorBlack));
            }
        });

        //充值显示
        pay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
                Fragment fragment = new PayFragment();
                ft.replace(R.id.content, fragment);
                ft.commit();
                isKeyboardVisible = false;
                isFragmentVisible = false;
                call.setCompoundDrawablesRelativeWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.tab_dial_selector), null, null);
                call.setTextColor(getResources().getColor(R.color.TextColorBlack));            }
        });

        //更多显示
        more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
                Fragment fragment = new MoreFragment();
                ft.replace(R.id.content, fragment);
                ft.commit();
                isKeyboardVisible = false;
                isFragmentVisible = false;
                call.setCompoundDrawablesRelativeWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.tab_dial_selector), null, null);
                call.setTextColor(getResources().getColor(R.color.TextColorBlack));            }
        });
    }
}


还有另一种方法,稍后写出博客


源码下载地址:http://download.csdn.net/detail/csdnyuandaimaxuexi/8968575

底部导航栏点击跳转fragment_第1张图片



你可能感兴趣的:(Android学习)