欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android Studio使用ViewPager+Fragment實(shí)現(xiàn)滑動(dòng)菜單Tab效果

 更新時(shí)間:2021年10月20日 11:20:54   作者:AnneHan  
這篇文章主要為大家詳細(xì)介紹了Android Studio使用ViewPager+Fragment實(shí)現(xiàn)滑動(dòng)菜單Tab效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文為大家分享了Android Studio實(shí)現(xiàn)滑動(dòng)菜單Tab效果的具體代碼,供大家參考,具體內(nèi)容如下

描述:

        之前有做過(guò)一個(gè)記賬本APP,拿來(lái)練手的,做的很簡(jiǎn)單,是用Eclipse開(kāi)發(fā)的;

        最近想把這個(gè)APP重新完善一下,添加了一些新的功能,并選用Android Studio來(lái)開(kāi)發(fā);

        APP已經(jīng)完善了一部分,現(xiàn)在就想把已經(jīng)做好的功能整理一下,記錄下來(lái)。 

效果圖:

        可以手動(dòng)滑動(dòng)菜單

        也可以通過(guò)點(diǎn)擊頭部菜單進(jìn)行切換

具體實(shí)現(xiàn)的代碼:

前臺(tái)代碼(activity_main.xml):

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

  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:layout_marginTop="1dp"
    android:background="@android:color/white"
    android:baselineAligned="false"
    android:gravity="center_vertical"
    android:orientation="horizontal"
    android:paddingBottom="5dp"
    android:paddingTop="5dp">

    <!--detail Tab-->
    <TextView
      android:id="@+id/item_detail"
      android:layout_width="0dp"
      android:layout_height="match_parent"
      android:layout_weight="1"
      android:gravity="center_horizontal|center_vertical"
      android:text="@string/detail_tab"
      android:textColor="@color/main_tab_text_color"
      android:textSize="20dp"/>

    <!--category report Tab-->
    <TextView
      android:id="@+id/item_category_report"
      android:layout_width="0dp"
      android:layout_height="match_parent"
      android:layout_weight="1"
      android:gravity="center_horizontal|center_vertical"
      android:text="@string/category_report_tab"
      android:textColor="@color/main_tab_text_color"
      android:textSize="20dp"/>
  </LinearLayout>

  <android.support.v4.view.ViewPager
    android:id="@+id/mainViewPager"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"/>
</LinearLayout>

主界面代碼(MainActivity.java):

package com.hyl.acccountbookdemo;

import android.graphics.Color;
import android.os.Bundle;
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 android.view.View;
import android.widget.TextView;

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

/**
 * @programName: MainActivity.java
 * @programFunction: Recording of income and expenditure
 * @createDate: 2018/09/25
 * @author: AnneHan
 * @version:
 * xx.  yyyy/mm/dd  ver  author  comments
 * 01.  2018/09/25  1.00  AnneHan  New Create
 */
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  private TextView item_detail, item_category_report;
  private ViewPager vp;
  private OneFragment oneFragment;
  private TwoFragment twoFragment;
  private List<Fragment> mFragmentList = new ArrayList<Fragment>();
  private FragmentAdapter mFragmentAdapter;

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

    initViews();

    mFragmentAdapter = new FragmentAdapter(this.getSupportFragmentManager(), mFragmentList);
    vp.setOffscreenPageLimit(2);//ViewPager的緩存為2幀
    vp.setAdapter(mFragmentAdapter);
    vp.setCurrentItem(0);//初始設(shè)置ViewPager選中第一幀
    item_detail.setTextColor(Color.parseColor("#1ba0e1"));

    //ViewPager的監(jiān)聽(tīng)事件
    vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
      @Override
      public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

      }

      @Override
      public void onPageSelected(int position) {
        /*此方法在頁(yè)面被選中時(shí)調(diào)用*/
        changeTextColor(position);
      }

      @Override
      public void onPageScrollStateChanged(int state) {
        /*此方法是在狀態(tài)改變的時(shí)候調(diào)用,其中arg0這個(gè)參數(shù)有三種狀態(tài)(0,1,2)。
        arg0==1的時(shí)辰默示正在滑動(dòng),
        arg0==2的時(shí)辰默示滑動(dòng)完畢了,
        arg0==0的時(shí)辰默示什么都沒(méi)做。*/
      }
    });
  }

  /**
   * 初始化布局View
   */
  private void initViews() {
    item_detail = (TextView) findViewById(R.id.item_detail);
    item_category_report = (TextView) findViewById(R.id.item_category_report);

    item_detail.setOnClickListener(this);
    item_category_report.setOnClickListener(this);

    vp = (ViewPager) findViewById(R.id.mainViewPager);
    oneFragment = new OneFragment();
    twoFragment = new TwoFragment();
    //給FragmentList添加數(shù)據(jù)
    mFragmentList.add(oneFragment);
    mFragmentList.add(twoFragment);
  }

  /**
   * 點(diǎn)擊頭部Text 動(dòng)態(tài)修改ViewPager的內(nèi)容
   */
  @Override
  public void onClick(View v) {
    switch (v.getId()) {
      case R.id.item_detail:
        vp.setCurrentItem(0, true);
        break;
      case R.id.item_category_report:
        vp.setCurrentItem(1, true);
        break;
    }
  }

  public class FragmentAdapter extends FragmentPagerAdapter {

    List<Fragment> fragmentList = new ArrayList<Fragment>();

    public FragmentAdapter(FragmentManager fm, List<Fragment> fragmentList) {
      super(fm);
      this.fragmentList = fragmentList;
    }

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

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

  }

  /**
   * 由ViewPager的滑動(dòng)修改頭部導(dǎo)航Text的顏色
   * @param position
   */
  private void changeTextColor(int position) {
    if (position == 0) {
      item_detail.setTextColor(Color.parseColor("#1ba0e1"));
      item_category_report.setTextColor(Color.parseColor("#000000"));
    } else if (position == 1) {
      item_category_report.setTextColor(Color.parseColor("#1ba0e1"));
      item_detail.setTextColor(Color.parseColor("#000000"));
    }
  }
}

需要多少個(gè)Fragment,便創(chuàng)建多少個(gè),這里只舉例寫一個(gè),其它相同

建立Fragment(fragment_one.xml):

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

  <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="@string/detail_tab"
    android:textSize="25sp"/>

</LinearLayout>

Fragment界面代碼(OneFragment.java):

package com.hyl.acccountbookdemo;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * @programName: OneFragment.java
 * @programFunction:
 * @createDate: 2018/09/25
 * @author: AnneHan
 * @version:
 * xx.  yyyy/mm/dd  ver  author  comments
 * 01.  2018/09/25  1.00  AnneHan  New Create
 */
public class OneFragment extends Fragment {
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_one, container, false);
  }
}

strings.xml:

<string name="detail_tab">明細(xì)</string>
<string name="category_report_tab">類別報(bào)表</string>

colors.xml:

<color name="main_tab_text_color">#000000</color>

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android Studio 3.0 Gradle 配置變更

    Android Studio 3.0 Gradle 配置變更

    這篇文章主要介紹了Android Studio 3.0 Gradle 配置變更的相關(guān)知識(shí),即多渠道打包變更和更改打包命名及路徑的代碼,感興趣的朋友跟隨腳本之家小編一起看看吧
    2018-03-03
  • Android判斷軟鍵盤彈出并隱藏的簡(jiǎn)單完美解決方法(推薦)

    Android判斷軟鍵盤彈出并隱藏的簡(jiǎn)單完美解決方法(推薦)

    下面小編就為大家?guī)?lái)一篇Android判斷軟鍵盤彈出并隱藏的簡(jiǎn)單完美解決方法(推薦)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-10-10
  • Android實(shí)現(xiàn)動(dòng)畫效果詳解

    Android實(shí)現(xiàn)動(dòng)畫效果詳解

    這篇文章主要介紹了Android實(shí)現(xiàn)動(dòng)畫效果詳解,目前Android平臺(tái)提供了Tween動(dòng)畫和Frame動(dòng)畫,實(shí)現(xiàn)這兩類動(dòng)畫有兩種方式:一種使用XML文件(文件放在res/anim),一種直接代碼搞定,需要的朋友可以參考下
    2015-07-07
  • Android TabLayout(選項(xiàng)卡布局)簡(jiǎn)單用法實(shí)例分析

    Android TabLayout(選項(xiàng)卡布局)簡(jiǎn)單用法實(shí)例分析

    這篇文章主要介紹了Android TabLayout(選項(xiàng)卡布局)簡(jiǎn)單用法,結(jié)合實(shí)例形式簡(jiǎn)單分析了Android選項(xiàng)卡布局的界面布局與功能實(shí)現(xiàn)具體相關(guān)技巧,需要的朋友可以參考下
    2016-01-01
  • Android開(kāi)發(fā)必備:秒殺真機(jī)超快模擬器Genymotion介紹

    Android開(kāi)發(fā)必備:秒殺真機(jī)超快模擬器Genymotion介紹

    這篇文章主要介紹了Android開(kāi)發(fā)必備:秒殺真機(jī)超快模擬器Genymotion介紹,本文直接用圖片說(shuō)明Genymotion的安裝和模擬效果,并提供官網(wǎng),需要的朋友可以參考下
    2015-04-04
  • 使用Kotlin實(shí)現(xiàn)文字漸變TextView的代碼

    使用Kotlin實(shí)現(xiàn)文字漸變TextView的代碼

    這篇文章主要介紹了使用Kotlin實(shí)現(xiàn)文字漸變TextView的代碼,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-04-04
  • Android TextView控件文字添加下劃線的實(shí)現(xiàn)方法

    Android TextView控件文字添加下劃線的實(shí)現(xiàn)方法

    下面小編就為大家?guī)?lái)一篇Android TextView控件文字添加下劃線的實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-09-09
  • Android簡(jiǎn)單封裝一個(gè)MVP基類流程詳解

    Android簡(jiǎn)單封裝一個(gè)MVP基類流程詳解

    MVP是從經(jīng)典的模式MVC演變而來(lái),它們的基本思想有相通的地方:Controller/Presenter負(fù)責(zé)邏輯的處理,Model提供數(shù)據(jù),View負(fù)責(zé)顯示。下面這篇文章主要給大家介紹了關(guān)于Android從實(shí)現(xiàn)到封裝MVP的相關(guān)內(nèi)容,分享出來(lái)供大家參考學(xué)習(xí),下面話不多說(shuō)了,來(lái)一起看看詳細(xì)的介紹吧
    2023-03-03
  • Android程序開(kāi)發(fā)之Fragment實(shí)現(xiàn)底部導(dǎo)航欄實(shí)例代碼

    Android程序開(kāi)發(fā)之Fragment實(shí)現(xiàn)底部導(dǎo)航欄實(shí)例代碼

    流行的應(yīng)用的導(dǎo)航一般分為兩種,一種是底部導(dǎo)航,一種是側(cè)邊欄。本文給大家介紹Fragment實(shí)現(xiàn)底部導(dǎo)航欄,對(duì)Fragment實(shí)現(xiàn)底部導(dǎo)航欄相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧
    2016-03-03
  • Android實(shí)現(xiàn)網(wǎng)絡(luò)多線程文件下載

    Android實(shí)現(xiàn)網(wǎng)絡(luò)多線程文件下載

    這篇文章主要介紹了Android實(shí)現(xiàn)網(wǎng)絡(luò)多線程文件下載的相關(guān)資料,需要的朋友可以參考下
    2016-03-03

最新評(píng)論