国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專(zhuān)欄INFORMATION COLUMN

Android實(shí)現(xiàn)底部狀態(tài)欄切換

UsherChen / 1059人閱讀

摘要:開(kāi)發(fā)過(guò)程中,特別是新開(kāi)的項(xiàng)目,底部狀態(tài)欄的切換使用的頻率非常的高,主要的實(shí)現(xiàn)方式有這里我先介紹前面兩種實(shí)現(xiàn)方式,后面兩種后期再貼出實(shí)現(xiàn)方式。代碼地址底部公眾號(hào)回復(fù)底部狀態(tài)欄切換即可獲取。

Android開(kāi)發(fā)過(guò)程中,特別是新開(kāi)的項(xiàng)目,底部狀態(tài)欄的切換使用的頻率非常的高,主要的實(shí)現(xiàn)方式有:

    (1)、TabLayout + Fragment
    (2)、FragmentTabHost + Fragment
    (3)、BottomNavigationView + Fragment
    (4)、RidioGroup + Fragment

這里我先介紹前面兩種實(shí)現(xiàn)方式,后面兩種后期再貼出實(shí)現(xiàn)方式。

一、使用TabLayout + Fragment + ViewPager實(shí)現(xiàn)

1、實(shí)現(xiàn)步驟:

    (1)、布局文件中定義TabLayout控件
    (2)、定義切換的每個(gè)Fragment布局文件
    (3)、定義切換的每個(gè)Fragment的Java類(lèi)
    (4)、定義TabLayoutMainActivity類(lèi)
    (5)、效果圖演示

2、實(shí)現(xiàn)過(guò)程:

(1)、布局文件中定義TabLayout控件(activity_main.xml)




    

    


(2)、定義切換的每個(gè)Fragment布局文件(fragment_frist.xml)

這里有四個(gè)Tab類(lèi)別(首頁(yè)、娛樂(lè)、游戲、我的),布局都類(lèi)似,這里只貼出其中一個(gè)




    

(3)、定義切換的每個(gè)Fragment的Java類(lèi)(FristFragment.class)

這里的Java 類(lèi)實(shí)現(xiàn)方式也相似,貼出其中一個(gè)

package com.showly.bottomnavigationbardemo.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.showly.bottomnavigationbardemo.R;

public class FristFragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_frist, null);
        return view;
    }
}

(4)、定義TabLayoutMainActivity類(lèi)(TabLayoutMainActivity.class)

package com.showly.bottomnavigationbardemo;


import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;

import com.showly.bottomnavigationbardemo.fragment.FourthlyFragment;
import com.showly.bottomnavigationbardemo.fragment.FristFragment;
import com.showly.bottomnavigationbardemo.fragment.SecondFragment;
import com.showly.bottomnavigationbardemo.fragment.ThirtlyFragment;

public class TabLayoutMainActivity extends AppCompatActivity {

    //未選中的Tab圖片
    private int[] unSelectTabRes = new int[]{R.drawable.i8live_menu_home_normal
            , R.drawable.i8live_menu_information_normal, R.drawable.i8live_menu_game_normal, R.drawable.i8live_menu_personl_normal};
    //選中的Tab圖片
    private int[] selectTabRes = new int[]{R.drawable.i8live_menu_home_press, R.drawable.i8live_menu_information_press
            , R.drawable.i8live_menu_game_press, R.drawable.i8live_menu_personl_press};
    //Tab標(biāo)題
    private String[] title = new String[]{"首頁(yè)", "娛樂(lè)", "游戲", "我的"};
    private ViewPager viewPager;
    private TabLayout tabLayout;
    private TabLayout.Tab tabAtOne;
    private TabLayout.Tab tabAttwo;
    private TabLayout.Tab tabAtthree;
    private TabLayout.Tab tabAtfour;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide();//隱藏掉整個(gè)ActionBar
        setContentView(R.layout.activity_main);

        initView();
        initData();
        initListener();
    }

    private void initView() {
        viewPager = (ViewPager) findViewById(R.id.viewpager_content_view);
        tabLayout = (TabLayout) findViewById(R.id.tab_layout_view);

        //使用適配器將ViewPager與Fragment綁定在一起
        viewPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager()));
        //將TabLayout與ViewPager綁定
        tabLayout.setupWithViewPager(viewPager);

      /*  //設(shè)置方式一:
        //獲取底部的單個(gè)Tab
        tabAtOne = tabLayout.getTabAt(0);
        tabAttwo = tabLayout.getTabAt(1);
        tabAtthree = tabLayout.getTabAt(2);
        tabAtfour = tabLayout.getTabAt(3);

        //設(shè)置Tab圖片
        tabAtOne.setIcon(R.drawable.i8live_menu_home_press);
        tabAttwo.setIcon(R.drawable.i8live_menu_information_normal);
        tabAtthree.setIcon(R.drawable.i8live_menu_game_normal);
        tabAtfour.setIcon(R.drawable.i8live_menu_personl_normal);*/

        //設(shè)置方式二:
        for (int i = 0; i < title.length; i++) {
            if (i == 0) {
                tabLayout.getTabAt(0).setIcon(selectTabRes[0]);
            } else {
                tabLayout.getTabAt(i).setIcon(unSelectTabRes[i]);
            }
        }

    }

    private void initData() {

    }

    private void initListener() {
        //TabLayout切換時(shí)導(dǎo)航欄圖片處理
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {//選中圖片操作

                for (int i = 0; i < title.length; i++) {
                    if (tab == tabLayout.getTabAt(i)) {
                        tabLayout.getTabAt(i).setIcon(selectTabRes[i]);
                        viewPager.setCurrentItem(i);
                    }
                }

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {//未選中圖片操作

                for (int i = 0; i < title.length; i++) {
                    if (tab == tabLayout.getTabAt(i)) {
                        tabLayout.getTabAt(i).setIcon(unSelectTabRes[i]);
                    }
                }

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    //自定義適配器
    public class MyFragmentPagerAdapter extends FragmentPagerAdapter {

        public MyFragmentPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            if (position == 1) {
                return new SecondFragment();//娛樂(lè)
            } else if (position == 2) {
                return new ThirtlyFragment();//游戲
            } else if (position == 3) {
                return new FourthlyFragment();//我的
            }
            return new FristFragment();//首頁(yè)
        }

        @Override
        public int getCount() {
            return title.length;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return title[position];
        }
    }
}

(5)、效果圖演示

二、使用FragmentTabHost+ Fragment + ViewPager實(shí)現(xiàn)

1、實(shí)現(xiàn)步驟:

    (1)、布局文件中定義FragmentTabHost控件
    (2)、定義底部菜單欄布局
    (3)、定義切換的每個(gè)Fragment布局文件
    (4)、定義切換的每個(gè)Fragment的Java類(lèi)
    (5)、切換按鈕的圖片
    (6)、定義FragmentTabHostMainActivity類(lèi)
    (7)、效果圖演示

2、實(shí)現(xiàn)過(guò)程:

(1)、布局文件中定義FragmentTabHost控件(fragment_tabhost_activity.xml)




    

    

        
    

(2)、定義底部菜單欄布局(tab_content.xml)




    

    

(3)、定義切換的每個(gè)Fragment布局文件(fragment_frist.xml)
這里有四個(gè)Tab類(lèi)別(首頁(yè)、娛樂(lè)、游戲、我的),布局都類(lèi)似,這里只貼出其中一個(gè)




    

(4)、定義切換的每個(gè)Fragment的Java類(lèi)(FristFragment.class)

這里的Java 類(lèi)實(shí)現(xiàn)方式也相似,貼出其中一個(gè)

package com.showly.bottomnavigationbardemo.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.showly.bottomnavigationbardemo.R;

public class FristFragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_frist, null);
        return view;
    }
}

(5)、切換按鈕的圖片(tab_main.xml)
這里有四個(gè)是相似的,只貼出其中一個(gè)



   
    
    
    

(6)、定義FragmentTabHostMainActivity類(lèi)(FragmentTabHostMainActivity.class)

package com.showly.bottomnavigationbardemo;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTabHost;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;

import com.showly.bottomnavigationbardemo.fragment.FourthlyFragment;
import com.showly.bottomnavigationbardemo.fragment.FristFragment;
import com.showly.bottomnavigationbardemo.fragment.SecondFragment;
import com.showly.bottomnavigationbardemo.fragment.ThirtlyFragment;

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


public class FragmentTabHostMainActivity extends FragmentActivity implements ViewPager.OnPageChangeListener, TabHost.OnTabChangeListener {

    private int[] selectTabRes = new int[]{R.drawable.tab_main, R.drawable.tab_infomation
            , R.drawable.tab_game, R.drawable.tab_personal};
    //Tab標(biāo)題
    private String[] title = new String[]{"首頁(yè)", "娛樂(lè)", "游戲", "我的"};
    private Class fragmentArry[] = {FristFragment.class, SecondFragment.class, ThirtlyFragment.class, FourthlyFragment.class};
    private List fragmentList = new ArrayList();

    private ViewPager viewPager;
    private FragmentTabHost tabHost;

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

        initView();
        initData();
        initListener();
    }

    /**
     * 初始化Fragment并給ViewPager添加適配器
     */
    private void initVaper() {
        FristFragment fristFragment = new FristFragment();
        SecondFragment secondFragment = new SecondFragment();
        ThirtlyFragment thirtlyFragment = new ThirtlyFragment();
        FourthlyFragment fourthlyFragment = new FourthlyFragment();

        fragmentList.add(fristFragment);
        fragmentList.add(secondFragment);
        fragmentList.add(thirtlyFragment);
        fragmentList.add(fourthlyFragment);

        //ViewPager添加適配器
        viewPager.setAdapter(new MyFragmentAdapter(getSupportFragmentManager(), fragmentList));
        tabHost.getTabWidget().setDividerDrawable(null);
    }

    private void initView() {
        viewPager = (ViewPager) findViewById(R.id.vp_pager);

        tabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);//綁定tabhost
        tabHost.setup(this, getSupportFragmentManager(), R.id.vp_pager);//TabHost綁定viewpager

        //獲取item的個(gè)數(shù)
        int count = title.length;

        for (int i = 0; i < count; i++) {
            //設(shè)置每個(gè)TabHost布局
            TabHost.TabSpec tabSpec = tabHost.newTabSpec(title[i])
                    .setIndicator(getTabItemView(i));

            //item與fragment關(guān)聯(lián)
            tabHost.addTab(tabSpec, fragmentArry[i], null);
            tabHost.setTag(i);
        }

        //初始化TabHost文字顏色
        upDateTab(tabHost);
        //給ViewPager設(shè)置適配器
        initVaper();
    }

    /**
     * 更新文字顏色。
     *
     * @param mTabHost
     */
    private void upDateTab(FragmentTabHost mTabHost) {
        for (int i = 0; i < mTabHost.getTabWidget().getChildCount(); i++) {
            TextView tv = (TextView) mTabHost.getTabWidget().getChildAt(i).findViewById(R.id.tv_item);
            if (mTabHost.getCurrentTab() == i) {//選中
                tv.setTextColor(Color.parseColor("#FF5959"));
            } else {//不選中
                tv.setTextColor(Color.parseColor("#777777"));
            }
        }
    }

    /**
     * 設(shè)置每個(gè)Item布局
     */
    private View getTabItemView(int i) {
        View view = LayoutInflater.from(this).inflate(R.layout.tab_content, null);

        ImageView itemImg = (ImageView) view.findViewById(R.id.iv_imageview);
        TextView itemText = (TextView) view.findViewById(R.id.tv_item);

        itemImg.setBackgroundResource(selectTabRes[i]);
        itemText.setText(title[i]);
        return view;
    }

    private void initData() {

    }

    private void initListener() {
        viewPager.addOnPageChangeListener(this);
        tabHost.setOnTabChangedListener(this);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        TabWidget widget = tabHost.getTabWidget();
        int oldFocusability = widget.getDescendantFocusability();
        widget.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);//設(shè)置View覆蓋子類(lèi)控件而直接獲得焦點(diǎn)
        tabHost.setCurrentTab(position);//根據(jù)位置Postion設(shè)置當(dāng)前的Tab
        widget.setDescendantFocusability(oldFocusability);//設(shè)置取消分割線
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onTabChanged(String tabId) {
        int position = tabHost.getCurrentTab();
        viewPager.setCurrentItem(position);//把選中的Tab的位置賦給適配器,讓它控制頁(yè)面切換
        upDateTab(tabHost);//設(shè)置TabHost文字顏色
    }

    /**
     * 適配器
     * */
    public class MyFragmentAdapter extends FragmentPagerAdapter {
        List list;

        public MyFragmentAdapter(FragmentManager fm, List list) {
            super(fm);
            this.list = list;
        }

        @Override
        public Fragment getItem(int position) {
            return list.get(position);
        }

        @Override
        public int getCount() {
            return list.size();
        }
    }
}

(7)、效果圖演示

三、總結(jié)
這兩種方式的實(shí)現(xiàn)已完成,實(shí)現(xiàn)起來(lái)相對(duì)還是比較簡(jiǎn)單的,后期有時(shí)間會(huì)把后面兩種的實(shí)現(xiàn)方式補(bǔ)上。
Demo代碼地址:底部公眾號(hào)回復(fù)"底部狀態(tài)欄切換"即可獲取。

以下是個(gè)人公眾號(hào)(longxuanzhigu),之后發(fā)布的文章會(huì)同步到該公眾號(hào),方便交流學(xué)習(xí)Android知識(shí)及分享個(gè)人愛(ài)好文章:

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://m.specialneedsforspecialkids.com/yun/77805.html

相關(guān)文章

  • Android項(xiàng)目實(shí)戰(zhàn)-博學(xué)谷》底部導(dǎo)航

    摘要:底部導(dǎo)航欄布局思路本項(xiàng)目的主界面設(shè)計(jì)了底部導(dǎo)航欄,為了方便后續(xù)布局的搭建,創(chuàng)建一個(gè)底部導(dǎo)航欄的框架,修改主界面布局為,利用包裹標(biāo)題欄與界面內(nèi)容,將各界面內(nèi)容設(shè)置為,再用一個(gè)包裹底部導(dǎo)航欄,在內(nèi)部用三個(gè)來(lái)顯示三個(gè)按鈕具體代碼如下限定大小 底部導(dǎo)航欄布局 思路 本項(xiàng)目的主界面設(shè)計(jì)了底部導(dǎo)航欄,為了方便后續(xù)布局的搭建,創(chuàng)建一個(gè)底部導(dǎo)航欄UI的框架,修改主界面布局為 RelativeLayou...

    余學(xué)文 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<