2019-01-07

2019-01-07  本文已影响0人  江北辰爱小璇子

Android底部导航栏的简单写法


MainActivity.class文件

package com.example.administrator.myfragment;

import android.graphics.Color;

import android.support.v4.app.FragmentManager;

import android.support.v4.app.FragmentTransaction;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.ImageView;

import android.widget.LinearLayout;

import android.widget.RelativeLayout;

import android.widget.TextView;

public class MainActivityextends AppCompatActivityimplements View.OnClickListener{

private FragmentManagerfragmentManager;//

    private RelativeLayoutr1_content;

private ImageViewitem1_iv,item2_iv,item3_iv,item4_iv;

private TextViewitem1_tv,item2_tv,item3_tv,item4_tv;

private LinearLayoutitem1,item2,item3,item4;

private ImageView[]ivs;

private TextView[]tvs;

@Override

    protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

initView();

fragmentManager=getSupportFragmentManager();

initListener();

setCheck(0);//选中第一个,在开始时候显示选中

}

//初始化,获取对象地址

private void initView(){

r1_content=(RelativeLayout)findViewById(R.id.rl_content);

item1_iv=(ImageView)findViewById(R.id.item1_iv);

item1_tv=(TextView)findViewById(R.id.item1_tv);

item1=(LinearLayout)findViewById(R.id.item1);

item2_iv=(ImageView)findViewById(R.id.item2_iv);

item2_tv=(TextView)findViewById(R.id.item2_tv);

item2=(LinearLayout)findViewById(R.id.item2);

item3_iv=(ImageView)findViewById(R.id.item3_iv);

item3_tv=(TextView)findViewById(R.id.item3_tv);

item3=(LinearLayout)findViewById(R.id.item3);

item4_iv=(ImageView)findViewById(R.id.item4_iv);

item4_tv=(TextView)findViewById(R.id.item4_tv);

item4=(LinearLayout)findViewById(R.id.item4);

ivs=new ImageView[]{item1_iv,item2_iv,item3_iv,item4_iv};

tvs=new TextView[]{item1_tv,item2_tv,item3_tv,item4_tv};

}

public void initListener(){

item1.setOnClickListener(this);

item2.setOnClickListener(this);

item3.setOnClickListener(this);

item4.setOnClickListener(this);

}

@Override

    public  void onClick(View view) {

switch (view.getId()) {

case R.id.item1: {

FragmentTransaction transaction=fragmentManager.beginTransaction();

transaction.replace(R.id.rl_content,new FragmentA());

transaction.commit();

setCheck(0);

break;

}

case R.id.item2: {

FragmentTransaction transaction=fragmentManager.beginTransaction();

transaction.replace(R.id.rl_content,new FragmentB());

transaction.commit();

setCheck(1);

break;

}

case R.id.item3: {

FragmentTransaction transaction=fragmentManager.beginTransaction();

transaction.replace(R.id.rl_content,new FragmentC());

transaction.commit();

setCheck(2);

break;

}

case R.id.item4: {

FragmentTransaction transaction=fragmentManager.beginTransaction();

transaction.replace(R.id.rl_content,new FragmentD());

transaction.commit();

setCheck(3);

break;

}

default:break;

}

}

public void setCheck(int itemId){

for(int i=0;i<4;i++)

{

ivs[i].setColorFilter(Color.parseColor("#0f0f0f"));

tvs[i].setTextColor(Color.parseColor("#0f0f0f"));

}

ivs[itemId].setColorFilter(Color.GREEN);

tvs[itemId].setTextColor(Color.GREEN);

}

}

activity_main.mxl

<?xml version="1.0" encoding="utf-8"?>

    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"

    android:orientation="vertical">

        android:id="@+id/rl_content"

        android:layout_width="match_parent"

        android:layout_height="0dp"

        android:layout_weight="1">

        android:layout_width="match_parent"

        android:layout_height="1dp"

        android:background="#797878"/>

        android:layout_width="match_parent"

        android:layout_height="80dp"

        android:orientation="horizontal">

            android:id="@+id/item1"

            android:layout_width="0dp"

            android:layout_weight="1"

            android:layout_height="match_parent"

            android:orientation="vertical">

                android:id="@+id/item1_iv"

                android:layout_width="match_parent"

                android:layout_height="0dp"

                android:layout_weight="3"

                android:layout_margin="3dp"

                android:scaleType="fitCenter"

                android:src="@drawable/tu"

                android:padding="1dp"/>

                android:id="@+id/item1_tv"

                android:text="女王"

                android:textSize="16sp"

                android:layout_width="match_parent"

                android:layout_height="0dp"

                android:layout_weight="1"

                android:gravity="center"/>

            android:id="@+id/item2"

            android:layout_width="0dp"

            android:layout_weight="1"

            android:layout_height="match_parent"

            android:orientation="vertical">

                android:id="@+id/item2_iv"

                android:layout_width="match_parent"

                android:layout_height="0dp"

                android:layout_weight="3"

                android:layout_margin="3dp"

                android:scaleType="fitCenter"

                android:src="@drawable/tu"

                android:padding="4dp"/>

                android:id="@+id/item2_tv"

                android:text="美妆"

                android:textSize="16sp"

                android:layout_width="match_parent"

                android:layout_height="0dp"

                android:layout_weight="1"

                android:gravity="center"/>

            android:id="@+id/item3"

            android:layout_width="0dp"

            android:layout_weight="1"

            android:layout_height="match_parent"

            android:orientation="vertical">

                android:id="@+id/item3_iv"

                android:layout_width="match_parent"

                android:layout_height="0dp"

                android:layout_weight="3"

                android:layout_margin="3dp"

                android:scaleType="fitCenter"

                android:src="@drawable/tu"

                android:padding="5dp"/>

                android:id="@+id/item3_tv"

                android:text="衣帽"

                android:textSize="16sp"

                android:layout_width="match_parent"

                android:layout_height="0dp"

                android:layout_weight="1"

                android:gravity="center" />

            android:id="@+id/item4"

            android:layout_width="0dp"

            android:layout_weight="1"

            android:layout_height="match_parent"

            android:orientation="vertical">

                android:id="@+id/item4_iv"

                android:layout_width="match_parent"

                android:layout_height="0dp"

                android:layout_weight="3"

                android:layout_margin="3dp"

                android:scaleType="fitCenter"

                android:src="@drawable/tu"

                android:padding="3dp"/>

                android:id="@+id/item4_tv"

                android:text="鞋包"

                android:textSize="16sp"

                android:layout_width="match_parent"

                android:layout_height="0dp"

                android:layout_weight="1"

                android:gravity="center" />

新建的几个Fragment


btnMainOne =(Button)findViewById(R.id.btn_main_one);

        btnMainOne.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                mFragmentTransaction=getFragmentManager().beginTransaction();

                mFragmentTransaction.replace(R.id.fl_main,new FragmentOne());

                //设置简单的过度动画

                mFragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);

                mFragmentTransaction.commit();

            }

        });


FragmentTransaction使用注意

每次在使用FragmentTransaction的时候都需要重新获取,每一个FragmentTransaction只能够commit()一次。

错误代码:

btnMainOne =(Button)findViewById(R.id.btn_main_one);

mFragmentTransaction=getFragmentManager().beginTransaction();

/* FragmentTransaction的获取挪到了点击事件之前 */

        btnMainOne.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                mFragmentTransaction.replace(R.id.fl_main,new FragmentOne());

                //设置简单的过度动画

                mFragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);

                mFragmentTransaction.commit();

            }

        });

错误使用中把FragmentTransaction的获取挪到了点击事件之前,这样在点击第二次的时候就会出现错误。


添加到返回栈

在使用Fragment的时候,我们经常会有一个这样的需求,就是需要通过返回键让fragment回复到之前的一个状态。使用FragmentTransaction就能很容易做到

mFragmentTransaction.addToBackStack(null);//添加fragment到返回栈

其实主要就是通过addToBackStack()这个方法,在commit()之前使用,能够保留commit之前的状态,在使用返回键时,能够回到之前的状态。

private void initViewTwo() {

        //主要逻辑在MainActivity的onClick中

        btnMainOne =(Button)findViewById(R.id.btn_main_one);

        btnMainOne.setOnClickListener(this);

        btnMainTwo =(Button)findViewById(R.id.btn_main_two);

        btnMainTwo.setOnClickListener(this);

        btnMainThree =(Button)findViewById(R.id.btn_main_three);

        btnMainThree.setOnClickListener(this);

    }

    @Override

    public void onClick(View v) {

        //每次点击事件都会重新获取FragmentTransaction

        mFragmentTransaction=getFragmentManager().beginTransaction();

        switch (v.getId()){

            case R.id.btn_main_one:

                if(fraOne==null){

                    fraOne=new FragmentOne();

                }

                mFragmentTransaction.replace(R.id.fl_main,fraOne);

                mFragmentTransaction.addToBackStack(null);//添加fragment到返回栈

                mFragmentTransaction.commit();

                break;

            case R.id.btn_main_two:

                if(fraTwo==null){

                    fraTwo=new FragmentTwo();

                }

                mFragmentTransaction.replace(R.id.fl_main,fraTwo);

                mFragmentTransaction.addToBackStack(null);//添加fragment到返回栈

                mFragmentTransaction.commit();

                break;

            case R.id.btn_main_three:

                if(fraThree==null){

                    fraThree=new FragmentThree();

                }

                mFragmentTransaction.replace(R.id.fl_main,fraThree);

                mFragmentTransaction.addToBackStack(null);//添加fragment到返回栈

                mFragmentTransaction.commit();

                break;

        }

    }



FragmentTransaction管理的Fragment生命周期状态

在我们Android中,对Fragment的操作都是通过FragmentTransaction来执行的。而如果从Fragment的结果来看,FragmentTransaction中对Fragment的操作大致可以分为两类:

1、显示操作:add()、 replace()、 show()、 attach()

2、隐藏操作:remove() 、hide() 、detach()

对于每一组方法,虽然最后产生的效果有点类似,但方法背后带来的副作用以及对Fragment的生命周期的影响都不尽相同。

add() VS replace()

只有在Fragment数量大于等于2的时候,调用add()还是replace()的区别才能体现出来。当通过add()连续两次添加Fragment的时候,每个Fragment生命周期中的onAttach()-onResume()都会被各调用一次,而且两个Fragment的View会被同时attach到containerView中。

同样,退出Activty时,每个Fragment生命周期中的onPause() - onDetach()也会被各调用一次。

但是当使用replace()来添加Fragment的时候,第二次添加的Fragment会导致第一个Fragment被销毁(在不使用回退栈的情况下),即执行第二个Fragment的onAttach()方法之前会先执行第一个Fragment的onPause()-onDetach()方法,与此同时containerView会detach掉第一个Fragment的View

文章来源与网络,本人只是记录知识点而已。

上一篇下一篇

猜你喜欢

热点阅读