仿QQ微信的底部导航

新人创作 高手勿喷!

实现思路:通过点击某个按钮,跳出framelayout界面,实现界面切换。

仿QQ微信的底部导航

什么都不用说了看代码:

1、新建布局文件 activity_test.xml 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <FrameLayout
        android:id="@+id/fl_main"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>
    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/gray"/>
    <RadioGroup
        android:id="@+id/rgs"
        android:layout_width="match_parent"
        android:layout_marginTop="5dp"
        android:layout_height="50dp"
        android:orientation="horizontal">
        <RadioButton
            android:id="@+id/rb_one"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:drawableTop="@drawable/home_butt"
            style="@style/RadioButton_User"
            android:checked="true"
            android:text="@string/home"/>
        <RadioButton
            android:id="@+id/rb_two"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:drawableTop="@drawable/discover"
            style="@style/RadioButton_User"
            android:text="@string/found"/>
        <RadioButton
            android:id="@+id/rb_three"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:drawableTop="@drawable/comm_video"
            style="@style/RadioButton_User"
            android:text="@string/management"/>
        <RadioButton
            android:id="@+id/rb_four"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:drawableTop="@drawable/child"
            style="@style/RadioButton_User"
            android:text="@string/my"/>
    </RadioGroup>
</LinearLayout>

2、Java代码:

(1)新建一个包fragment(废话),其实可以不用建,如果和我一样是新手的话我建议还是新建比较好。在包里创两个文件:BaseFragment、FragmentTabUtils;代码如下。

1、BaseFragment

package com.newdemo.navigationdemoyxy.navigationdemo.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by Administrator on 2018/3/21.
 */

public abstract class BaseFragment extends android.support.v4.app.Fragment{
    private View view;
    private Context context;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.context=getActivity();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view=initView(inflater);
        return view;
    }

    public abstract View initView(LayoutInflater inflater);
}
2、FragmentTabUtils


package com.newdemo.navigationdemoyxy.navigationdemo.fragment;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.widget.RadioGroup;

import java.util.List;

/**
 * Created by Administrator on 2018/3/21.
 */

public class FragmentTabUtils implements RadioGroup.OnCheckedChangeListener {

    private List<BaseFragment> baseFragments;
    private RadioGroup radioGroup;
    private FragmentManager fragmentManager;
    private int fragmentContentId;
    private int curentTab;
    private OnRgsExtraCheckedChangedListener onRgsExtraChekedChangedListener;
    public FragmentTabUtils(List<BaseFragment> fragments, RadioGroup radioGroup, FragmentManager fragmentManager, int fragmentContentId){
        this.baseFragments=fragments;
        this.fragmentManager=fragmentManager;
        this.fragmentContentId=fragmentContentId;
        this.radioGroup=radioGroup;
        fragmentManager.beginTransaction().add(fragmentContentId,fragments.get(0)).commit();
        radioGroup.setOnCheckedChangeListener(this);
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int checheId) {
        for (int i=0;i<radioGroup.getChildCount();i++){
            if (radioGroup.getChildAt(i).getId()==checheId){
                Fragment fragment=baseFragments.get(i);
                FragmentTransaction fragmentTransaction=obtainFragmentTransaction(i);
                getCurentFragment().onStop();
                if (fragment.isAdded()){
                    fragment.onStart();
                }else {
                    fragmentTransaction.add(fragmentContentId,fragment);
                    fragmentTransaction.commit();
                }
                showTab(i);
                if (null!=onRgsExtraChekedChangedListener){
                    onRgsExtraChekedChangedListener.OnRgsExtraChecheChanged(radioGroup,checheId,i);
                }
            }
        }
    }
    public void showTab(int index){
        for (int i=0;i<baseFragments.size();i++){
            Fragment fragment=baseFragments.get(i);
            FragmentTransaction fragmentTransaction=obtainFragmentTransaction(i);
            if (index==i){
                fragmentTransaction.show(fragment);
            }else {
                fragmentTransaction.hide(fragment);
            }
            fragmentTransaction.commit();
        }
        curentTab=index;
    }
    public FragmentTransaction obtainFragmentTransaction(int indes){
        FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
        return fragmentTransaction;
    }
    public int getCurentTab(){
        return curentTab;
    }
    public Fragment getCurentFragment(){
        return baseFragments.get(curentTab);
    }
    public static interface OnRgsExtraCheckedChangedListener{
        public void OnRgsExtraChecheChanged(RadioGroup radioGroup, int checkedId, int index);
    }
    public OnRgsExtraCheckedChangedListener getOnRgsExtraCheckedChangedListener(){
        return onRgsExtraChekedChangedListener;
    }
    public void setOnRgsExtraChekedChangedListener(OnRgsExtraCheckedChangedListener onRgsExtraChekedChangedListener){
        this.onRgsExtraChekedChangedListener=onRgsExtraChekedChangedListener;
    }
}
(2)MainActivity代码:

package com.newdemo.navigationdemoyxy.navigationdemo;

import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.widget.RadioGroup;

import com.newdemo.navigationdemoyxy.navigationdemo.fragment.BaseFragment;
import com.newdemo.navigationdemoyxy.navigationdemo.fragment.FoundFragment;
import com.newdemo.navigationdemoyxy.navigationdemo.fragment.FragmentTabUtils;
import com.newdemo.navigationdemoyxy.navigationdemo.fragment.HomePageFragment;
import com.newdemo.navigationdemoyxy.navigationdemo.fragment.ManagementFragment;
import com.newdemo.navigationdemoyxy.navigationdemo.fragment.MyselyFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends FragmentActivity implements FragmentTabUtils.OnRgsExtraCheckedChangedListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        List<BaseFragment> fragments=new ArrayList<BaseFragment>();
        fragments.add(new HomePageFragment());                 //主页
        fragments.add(new FoundFragment());                    //发现
        fragments.add(new ManagementFragment());               //管理
        fragments.add(new MyselyFragment());                   //个人中心
        RadioGroup group=(RadioGroup)findViewById(R.id.rgs);
        FragmentTabUtils tabUtils=new FragmentTabUtils(fragments,group,getSupportFragmentManager(),R.id.fl_main);
        tabUtils.setOnRgsExtraChekedChangedListener(this);
    }

    @Override
    public void OnRgsExtraChecheChanged(RadioGroup radioGroup, int checkedId, int index) {

    }
}
(3)新建每个报错的Fragment,因为每个界面大同小异,所以只拿一个代码来参考。
注意继承  BaseFragment

package com.newdemo.navigationdemoyxy.navigationdemo.fragment;

import android.view.LayoutInflater;
import android.view.View;

import com.newdemo.navigationdemoyxy.navigationdemo.R;

public class HomePageFragment extends BaseFragment {
    @Override
    public View initView(LayoutInflater inflater) {
        View view=inflater.inflate(R.layout.activity_home,null);
        return view;
    }
}
因为布局太简单了就不写了。

实在看不懂,有Dome下载链接:

https://download.****.net/download/qq_19311521/10409231