Android 之底部导航(Fragment)

2019-03-20  本文已影响0人  逸曦穆泽

谜之android,自己啃!

  1. Bottom Navigation Activity 模版创建项目,得到可切换按钮的底部导航,可是放置内容的页面并没有,只有一个主页面,然后只能自己硬干了;
  2. 说一下Fragment 这个点:(有两个分支:android.app 包android.support.v4.app包 这两个是不兼容的,是两个家族,引用其中一个就可以了,我用的是v4包的)
  3. 在主文件(MainActivity)的同级目录中新建一个文件夹 fragment,存放Fragment类文件,然后在下面写几个相应的Fragment的实现类;OneFragment,TwoFragment,ThreeFragment,就是对应的导航按钮的页面;下面贴出一份:
public class OneFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.lamp_fragment, container, false);
    }
}

activity_main.xml文件内容:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <FrameLayout
        android:id="@+id/framePage"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:layout_editor_absoluteX="16dp"
        tools:layout_editor_absoluteY="16dp" />

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="0dp"
        android:layout_marginEnd="0dp"
        android:background="?android:attr/windowBackground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/navigation" />

</android.support.constraint.ConstraintLayout>

MainActivity 主类文件

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MenuItem;
import android.widget.FrameLayout;

import com.example.app_demo.fragmes.OneFragment;
import com.example.app_demo.fragmes.TwoFragment;
import com.example.app_demo.fragmes.ThreepFragment;

import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";//文件日志

    private BottomNavigationView mBottomNavigationView;//底部导航
    List<Fragment> mFragments;//Fragment 集合
    private int lastIndex;//标记


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

        initViews();//实例化
        initListener();//初始化监听
        initData();
    }
    private void initViews(){
        mBottomNavigationView = (BottomNavigationView) findViewById(R.id.navigation);//导航切换按钮
    }
    private void initListener(){
        mBottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                switch (menuItem.getItemId()) {
                    case R.id.navigation_one:
                        setFragmentPosition(0);//第一个
                        Log.d(TAG, "-----: "+ mBottomNavigationView.getMenu().getItem(0));
                        break;
                    case R.id.navigation_two:
                        setFragmentPosition(1);//第二个
                        Log.d(TAG, "-----: "+ mBottomNavigationView.getMenu().getItem(1));
                        break;
                    case R.id.navigation_three:
                        setFragmentPosition(2);//第三个
                        Log.d(TAG, "-----: "+ mBottomNavigationView.getMenu().getItem(2));
                        break;
                    default:
                        break;
                }
                return true;
            }
        });
    }
    private void initData(){
        mFragments = new ArrayList<Fragment>();
        mFragments.add(new OneFragment());
        mFragments.add(new TwoFragment());
        mFragments.add(new ThreeFragment());
        //初始化显示 OneFragment
        setFragmentPosition(0);//调用
        Log.d(TAG, "----初始化initData---- ");
    }
    private void setFragmentPosition(int position) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        Fragment currentFragment = mFragments.get(position);//当前的Fragment
        Fragment lastFragment = mFragments.get(lastIndex);//上一个的Fragment
        Log.d(TAG, "----当前的Fragment: ----"+currentFragment);
        Log.d(TAG, "----上一个的Fragment:---- "+lastFragment);
        lastIndex = position;//得到上一个index下标
        ft.hide(lastFragment);
        //isAdded() 返回true => Fragment对象被添加到它的Activity中
        Log.d(TAG, "------判断: "+currentFragment.isAdded());
        if (!currentFragment.isAdded()) {
            getSupportFragmentManager().beginTransaction().remove(currentFragment).commit();
            ft.add(R.id.framePage, currentFragment);
        }
        //ft.addToBackStack(null);
        ft.show(currentFragment);
        ft.commitAllowingStateLoss();
    }
}

对了values文件夹下有几个.xml文件,是配置共同引用部分的(颜色,字体,样式,边距之类的),如:

color.xml
dimens.xml
string.xml
style.xml
顾名思义,差不多就是字面上的意思。这里的文件引用也是是挺方便的,有点像Java里的国际化文件;不过,有些UI效果实现还挺麻烦,不是用代码动态控制就是用xml文件实现。

OK,废话就到这里,零碎的一些文件就贴出来!

上一篇下一篇

猜你喜欢

热点阅读