Android-Fragment实现底部导航栏

布局

my_fragment.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"
                tools:context=".MainActivity">

    <RelativeLayout
            android:id="@+id/ly_top_bar"
            android:layout_width="match_parent"
            android:layout_height="48dp"
           >

        <TextView
                android:id="@+id/topBar"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_centerInParent="true"
                android:gravity="center"
                android:textSize="18sp"
                android:textColor="#000"
                android:text="信息"/>


        <View
                android:layout_width="match_parent"
                android:layout_height="2px"
                android:background="#C0C0C0"
                android:layout_alignParentBottom="true"/>

    </RelativeLayout>



    <LinearLayout
            android:id="@+id/ly_tab_bar"
            android:layout_width="match_parent"
            android:layout_height="56dp"
            android:layout_alignParentBottom="true"
            android:background="#C0C0C0"
            android:orientation="horizontal">

        <TextView
                android:id="@+id/txt_channel"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="#FFF"
                android:drawablePadding="3dp"
                android:drawableTop="@drawable/tab_menu_channel"
                android:gravity="center"
                android:padding="5dp"
                android:text="提醒"
                android:textColor="@drawable/tab_menu_text"
                android:textSize="16sp" />

        <TextView
                android:id="@+id/txt_message"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="#FFF"
                android:drawablePadding="3dp"
                android:drawableTop="@drawable/tab_menu_message"
                android:gravity="center"
                android:padding="5dp"
                android:text="消息"
                android:textColor="@drawable/tab_menu_text"
                android:textSize="16sp" />

        <TextView
                android:id="@+id/txt_better"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="#FFF"
                android:drawablePadding="3dp"
                android:drawableTop="@drawable/tab_menu_better"
                android:gravity="center"
                android:padding="5dp"
                android:text="我的"
                android:textColor="@drawable/tab_menu_text"
                android:textSize="16sp" />

        <TextView
                android:id="@+id/txt_setting"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="#FFF"
                android:drawablePadding="3dp"
                android:drawableTop="@drawable/tab_menu_setting"
                android:gravity="center"
                android:padding="5dp"
                android:text="设置"
                android:textColor="@drawable/tab_menu_text"
                android:textSize="16sp"/>

    </LinearLayout>

    <View
            android:id="@+id/div_tab_bar"
            android:layout_width="match_parent"
            android:layout_height="2px"
            android:background="#FFF"
            android:layout_above="@id/ly_tab_bar"/>


    <FrameLayout
            android:background="#D3D3D3"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/ly_top_bar"
            android:layout_above="@id/div_tab_bar"
            android:id="@+id/ly_content">

    </FrameLayout>

</RelativeLayout>

fg_content.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#D3D3D3">

    <TextView
        android:id="@+id/txt_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:textColor="#000"
        android:textSize="20sp"/>

</LinearLayout>

MainActivity类

package com.demo;


import android.content.Intent;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;

import android.support.v4.widget.DrawerLayout;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;


import android.util.Log;
import android.view.*;

import android.widget.*;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    
    

    private ViewPager pager1;
    private ArrayList<View> list;
    private MyPagerAdapter myPagerAdapter;

    private ImageView img_cursor;
    private TextView tv_one;
    private TextView tv_two;
    private TextView tv_three;
    private int offSet = 0;  //移动条图片的偏移量
    private int currIndex = 0;  //当前页
    private int imageWidth = 0;  //移动条图片的宽度
    private int one = 0;   //移动条滑动一页的距离
    private int two = 0;   //移动条滑动两页的距离

    private DrawerLayout drawerLayout;
    private TextView ll_content;
    private LinearLayout id_ll_left;
    Button button;

    private TextView txt_topbar;
    private TextView txt_channel;
    private TextView txt_message;
    private TextView txt_better;
    private TextView txt_setting;
    private FrameLayout ly_content;

    //Fragment Object
    private MyFragment fg1,fg2,fg3,fg4;
    private FragmentManager fManager;




    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_fragment);

//        init();
        bindViews();


        //getFragmentManager();不推荐使用
        fManager = getSupportFragmentManager();
        //模拟点击
        txt_channel.performClick();

        button = findViewById(R.id.button);
        Intent intent =new Intent(MainActivity.this,SecondActivity.class);
        startActivityForResult(intent,1);




    }

    private void bindViews() {
    
    
        txt_topbar = findViewById(R.id.topBar);
        txt_channel = findViewById(R.id.txt_channel);
        txt_message =findViewById(R.id.txt_message);
        txt_better =  findViewById(R.id.txt_better);
        txt_setting =  findViewById(R.id.txt_setting);
        ly_content = findViewById(R.id.ly_content);

        txt_channel.setOnClickListener(this);
        txt_message.setOnClickListener(this);
        txt_better.setOnClickListener(this);
        txt_setting.setOnClickListener(this);
    }

    //设置文本的选中状态
    private void setSelected(){
    
    
         txt_channel.setSelected(false);
         txt_better.setSelected(false);
         txt_message.setSelected(false);
         txt_setting.setSelected(false);
    }

    //隐藏所有Fragment
    private void hideAllFragment(FragmentTransaction transaction){
    
    
        if(fg1!=null) transaction.hide(fg1);
        if(fg2!=null) transaction.hide(fg2);
        if(fg3!=null) transaction.hide(fg3);
        if(fg4!=null) transaction.hide(fg4);

    }

    @Override
    public void onPageScrolled(int i, float v, int i1) {
    
    

    }

    @Override
    public void onPageSelected(int i) {
    
    

    }

    @Override
    public void onPageScrollStateChanged(int i) {
    
    

    }

    @Override
    public void onClick(View view) {
    
    

        FragmentTransaction fragmentTransaction = fManager.beginTransaction();
        hideAllFragment(fragmentTransaction);
        switch (view.getId()){
    
    
            case R.id.txt_channel:
                setSelected();
                txt_channel.setSelected(true);
                if(fg1==null){
    
    
                    fg1 = new MyFragment("first Fragment");
                    fragmentTransaction.add(R.id.ly_content,fg1);
                }
                else{
    
    
                    fragmentTransaction.show(fg1);
                }
                break;
            case  R.id.txt_message:
                setSelected();
                txt_message.setSelected(true);
                if(fg2==null){
    
    
                    fg2 = new MyFragment("second Fragment");
                    fragmentTransaction.add(R.id.ly_content,fg2);
                }
                else{
    
    
                    fragmentTransaction.show(fg2);
                }
                break;
            case R.id.txt_better:
                setSelected();
                txt_better.setSelected(true);
                if(fg3==null){
    
    
                    fg3 = new MyFragment("third Fragment");
                    fragmentTransaction.add(R.id.ly_content,fg3);
                }
                else{
    
    
                    fragmentTransaction.show(fg3);
                }
                break;
            case R.id.txt_setting:
                setSelected();
                txt_setting.setSelected(true);
                if(fg4==null){
    
    
                    fg4 = new MyFragment("forth Fragment");
                    fragmentTransaction.add(R.id.ly_content,fg4);
                }
                else{
    
    
                    fragmentTransaction.show(fg4);
                }
                break;
        }
        fragmentTransaction.commit();

    }



}

自定义Fragment类

package com.demo;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


@SuppressLint("ValidFragment")
public class MyFragment extends Fragment {
    
    

    private String content;

    public MyFragment(String content) {
    
    
        this.content = content;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    
    
        View view = inflater.inflate(R.layout.fg_content,null);
        TextView textView = (TextView) view.findViewById(R.id.txt_content);
        textView.setText(content);

        return  view;
    }
}

效果

在这里插入图片描述
在这里插入图片描述

完整代码下载地址

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/m0_46267375/article/details/109393163