fragment实现底部导航栏不刷新切换

安卓自带的底部导航栏每次切换fragment都会刷新数据,用户体验会很不好,

 

所以自己做了个不会刷新的fragment切换的底部导航栏

先看效果,我上传一个录屏,请看效果(首页时c站的一个链接):

捂脸,上传审核,于是我把录屏转成gif图片,请看

​​​​​​​

 

MainActivity+HomeFragment+Notificationfragment+Minefragment

1.MainActivity代码

package com.xmkjstudio.jyj9;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.xmkjstudio.jyj9.fragment.HomeFragment;
import com.xmkjstudio.jyj9.fragment.MineFragment;
import com.xmkjstudio.jyj9.fragment.NotificationFragment;

public class MainActivity extends AppCompatActivity  implements View.OnClickListener {

    private FragmentManager fragmentManager;


    private FragmentTransaction fragmentTransaction;
    private HomeFragment homeFragment;
    private NotificationFragment notificationFragment;
    private MineFragment mineFragment;
    private FragmentTransaction ft;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        findViewById(R.id.tab_1_img).setOnClickListener(this);
        findViewById(R.id.tab_1_txt).setOnClickListener(this);
        findViewById(R.id.tab1_contain).setOnClickListener(this);
        findViewById(R.id.tab_2_img).setOnClickListener(this);
        findViewById(R.id.tab_2_txt).setOnClickListener(this);
        findViewById(R.id.tab2_contain).setOnClickListener(this);
        findViewById(R.id.tab_3_img).setOnClickListener(this);
        findViewById(R.id.tab_3_txt).setOnClickListener(this);
        findViewById(R.id.tab3_contain).setOnClickListener(this);

//        addToIndex();


        homeFragment=new HomeFragment();
        notificationFragment=new NotificationFragment();
        mineFragment=new MineFragment();



        fragmentManager = getSupportFragmentManager();
        ft = fragmentManager.beginTransaction();
        ft.add(R.id.frame,homeFragment).commit();


//
//        fragmentManager=getSupportFragmentManager();
//        fragmentTransaction=fragmentManager.beginTransaction();


    }

    @Override
    public void onClick(View v) {
        fragmentTransaction=fragmentManager.beginTransaction();
        FragmentTransaction transaction = fragmentManager.beginTransaction().setCustomAnimations(
                android.R.anim.fade_in, android.R.anim.fade_out);

        switch (v.getId()){
            //tab1
            case R.id.tab_1_txt:

                break;
            case R.id.tab_1_img:


                break;
            case R.id.tab1_contain:
                if(notificationFragment.isAdded()){
                    fragmentManager = getSupportFragmentManager();
                    ft = fragmentManager.beginTransaction();
                    ft.hide(notificationFragment).commit();
                }

                if(mineFragment.isAdded()){
                    fragmentManager = getSupportFragmentManager();
                    ft = fragmentManager.beginTransaction();
                    ft.hide(mineFragment).commit();
                }
                if(homeFragment.isAdded()) {

                    if(notificationFragment.isAdded()){
                        fragmentManager = getSupportFragmentManager();
                        ft = fragmentManager.beginTransaction();
                        ft.hide(notificationFragment).commit();
                    }

                    if(mineFragment.isAdded()){
                        fragmentManager = getSupportFragmentManager();
                        ft = fragmentManager.beginTransaction();
                        ft.hide(mineFragment).commit();
                    }
                    fragmentManager = getSupportFragmentManager();
                    ft = fragmentManager.beginTransaction();
                    ft.show(homeFragment).commit();

                }else{
                    fragmentManager = getSupportFragmentManager();
                    ft = fragmentManager.beginTransaction();

                    ft.add(R.id.frame, homeFragment).commit();
                }
                break;

                //tab2
            case R.id.tab_2_txt:

                break;
            case R.id.tab_2_img:

                break;
            case R.id.tab2_contain:
                if(homeFragment.isAdded()){
                    fragmentManager = getSupportFragmentManager();
                    ft = fragmentManager.beginTransaction();
                    ft.hide(homeFragment).commit();
                }

                if(mineFragment.isAdded()){
                    fragmentManager = getSupportFragmentManager();
                    ft = fragmentManager.beginTransaction();
                    ft.hide(mineFragment).commit();
                }

                if(notificationFragment.isAdded()){
                    if(homeFragment.isAdded()){
                        fragmentManager = getSupportFragmentManager();
                        ft = fragmentManager.beginTransaction();
                        ft.hide(homeFragment).commit();
                    }

                    if(mineFragment.isAdded()){
                        fragmentManager = getSupportFragmentManager();
                        ft = fragmentManager.beginTransaction();
                        ft.hide(mineFragment).commit();
                    }
                    fragmentManager = getSupportFragmentManager();
                    ft = fragmentManager.beginTransaction();
                    ft.show(notificationFragment).commit();
                }else {
                    fragmentManager = getSupportFragmentManager();
                    ft = fragmentManager.beginTransaction();
                    ft.add(R.id.frame, notificationFragment).commit();
                }
                break;

            //tab3
            case R.id.tab_3_txt:

                break;
            case R.id.tab_3_img:

                break;
            case R.id.tab3_contain:
                if(homeFragment.isAdded()){
                    fragmentManager = getSupportFragmentManager();
                    ft = fragmentManager.beginTransaction();
                    ft.hide(homeFragment).commit();
                }

                if(notificationFragment.isAdded()){
                    fragmentManager = getSupportFragmentManager();
                    ft = fragmentManager.beginTransaction();
                    ft.hide(notificationFragment).commit();
                }
                if(mineFragment.isAdded()){
                    if(homeFragment.isAdded()){
                        fragmentManager = getSupportFragmentManager();
                        ft = fragmentManager.beginTransaction();
                        ft.hide(homeFragment).commit();
                    }

                    if(notificationFragment.isAdded()){
                        fragmentManager = getSupportFragmentManager();
                        ft = fragmentManager.beginTransaction();
                        ft.hide(notificationFragment).commit();
                    }
                    fragmentManager = getSupportFragmentManager();
                    ft = fragmentManager.beginTransaction();
                    ft.show(mineFragment).commit();
                }else {
                    fragmentManager = getSupportFragmentManager();
                    ft = fragmentManager.beginTransaction();
                    ft.add(R.id.frame, mineFragment).commit();
                }
                break;
        }
        fragmentTransaction.commit();
    }



    //提前加载
//    private  void addToIndex(){
//
//        fragmentManager=getSupportFragmentManager();
//        fragmentTransaction=fragmentManager.beginTransaction();
//        if(homeFragment==null){
//            homeFragment=new HomeFragment();
//        }
//        fragmentTransaction.replace(R.id.frame,homeFragment);
//
//
//        fragmentTransaction.commit();
//    }


}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".MainActivity">

   <LinearLayout
       android:layout_width="match_parent"
       android:orientation="vertical"
       android:layout_height="match_parent">


       <FrameLayout
           android:id="@+id/frame"
           android:orientation="vertical"
           android:layout_weight="0.8"
           android:layout_width="match_parent"
           android:layout_height="match_parent">

       </FrameLayout>



       <!--导航栏-->
       <LinearLayout
           android:layout_marginTop="5dp"
           android:layout_weight="9.2"
           android:orientation="horizontal"
           android:layout_width="match_parent"
           android:layout_height="match_parent">
            <!--tab1-->
           <LinearLayout
               android:id="@+id/tab1_contain"
               android:layout_weight="1"
               android:orientation="vertical"
               android:gravity="center_horizontal"
               android:layout_width="match_parent"
               android:layout_height="match_parent">
               <ImageView
                   android:id="@+id/tab_1_img"
                   android:src="@drawable/ic_launcher_background"
                   android:layout_width="30dp"
                   android:layout_height="30dp"/>
               <TextView
                   android:id="@+id/tab_1_txt"
                   android:text="经验记"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"/>
           </LinearLayout>

           <!--tab2-->
           <LinearLayout
               android:id="@+id/tab2_contain"
               android:orientation="vertical"
               android:gravity="center_horizontal"
               android:layout_weight="1"
               android:layout_width="match_parent"
               android:layout_height="match_parent">
               <ImageView
                   android:id="@+id/tab_2_img"
                   android:src="@drawable/ic_launcher_background"
                   android:layout_width="30dp"
                   android:layout_height="30dp"/>
               <TextView
                   android:id="@+id/tab_2_txt"
                   android:text="消息"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"/>

           </LinearLayout>


           <!--tab3-->
           <LinearLayout
               android:id="@+id/tab3_contain"
               android:orientation="vertical"
               android:gravity="center_horizontal"
               android:layout_weight="1"
               android:layout_width="match_parent"
               android:layout_height="match_parent">
               <ImageView
                   android:id="@+id/tab_3_img"
                   android:src="@drawable/ic_launcher_background"
                   android:layout_width="30dp"
                   android:layout_height="30dp"/>
               <TextView
                   android:id="@+id/tab_3_txt"
                   android:text="我"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"/>
           </LinearLayout>
       </LinearLayout>

   </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

HomeFragment

package com.xmkjstudio.jyj9.fragment;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;

import com.xmkjstudio.jyj9.R;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link HomeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HomeFragment extends Fragment {


    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    public HomeFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment HomeFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static HomeFragment newInstance(String param1, String param2) {
        HomeFragment fragment = new HomeFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }

    }

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

        WebView webView=view.findViewById(R.id.webview);
        webView.loadUrl("https://blog.csdn.net/b305723/ar" +
                "ticle/details/101892612?ops_request_misc=" +
                "%257B%2522request%255Fid%2522%253A%2522165" +
                "447567616782184659916%2522%252C%2522scm%25" +
                "22%253A%252220140713.130102334..%2522%257D" +
                "&request_id=165447567616782184659916&biz_i" +
                "d=0&utm_medium=distribute.pc_search_result" +
                ".none-task-blog-2~all" +
                "~sobaiduend~default-1-101892612-null-null." +
                "142^v11^control,157^v13^new_style1&utm_" +
                "term=fragment里面怎么写代码&spm=1018.2226.3001.4187");

        return view;


    }
}

fragment_home.x m l

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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=".fragment.HomeFragment">

    <!-- TODO: Update blank fragment layout -->
   <WebView
       android:id="@+id/webview"
       android:layout_width="match_parent"
       android:layout_height="match_parent"/>

</FrameLayout>

NotificationFragment代码

package com.xmkjstudio.jyj9.fragment;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

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

import com.xmkjstudio.jyj9.R;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link NotificationFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class NotificationFragment extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    public NotificationFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment NotificationFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static NotificationFragment newInstance(String param1, String param2) {
        NotificationFragment fragment = new NotificationFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_notification, container, false);
    }
}

fragment_notification.xml 

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:background="@mipmap/ic_launcher"
    tools:context=".fragment.NotificationFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_blank_fragment" />

</FrameLayout>

Minefragment代码

package com.xmkjstudio.jyj9.fragment;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

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

import com.xmkjstudio.jyj9.R;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link MineFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class MineFragment extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    public MineFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment MineFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static MineFragment newInstance(String param1, String param2) {
        MineFragment fragment = new MineFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_mine, container, false);
    }
}

fragmen_mine.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:background="@drawable/ic_launcher_background"
    android:layout_height="match_parent"
    tools:context=".fragment.MineFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_blank_fragment" />

</FrameLayout>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值