Android Studio使用ViewPager+Fragment实现滑动菜单Tab效果

作者:袖梨 2022-06-25

本文为大家分享了Android Studio实现滑动菜单Tab效果的具体代码,供大家参考,具体内容如下

描述:

        之前有做过一个记账本APP,拿来练手的,做的很简单,是用Eclipse开发的;

        最近想把这个APP重新完善一下,添加了一些新的功能,并选用Android Studio来开发;

        APP已经完善了一部分,现在就想把已经做好的功能整理一下,记录下来。 

效果图:

        可以手动滑动菜单

        也可以通过点击头部菜单进行切换

 

具体实现的代码:

前台代码(activity_main.xml):




  

    
    

    
    
  

  

主界面代码(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 mFragmentList = new ArrayList();
  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);//初始设置ViewPager选中第一帧
    item_detail.setTextColor(Color.parseColor("#1ba0e1"));

    //ViewPager的监听事件
    vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
      @Override
      public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

      }

      @Override
      public void onPageSelected(int position) {
        /*此方法在页面被选中时调用*/
        changeTextColor(position);
      }

      @Override
      public void onPageScrollStateChanged(int state) {
        /*此方法是在状态改变的时候调用,其中arg0这个参数有三种状态(0,1,2)。
        arg0==1的时辰默示正在滑动,
        arg0==2的时辰默示滑动完毕了,
        arg0==0的时辰默示什么都没做。*/
      }
    });
  }

  /**
   * 初始化布局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添加数据
    mFragmentList.add(oneFragment);
    mFragmentList.add(twoFragment);
  }

  /**
   * 点击头部Text 动态修改ViewPager的内容
   */
  @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 fragmentList = new ArrayList();

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

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

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

  }

  /**
   * 由ViewPager的滑动修改头部导航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"));
    }
  }
}

需要多少个Fragment,便创建多少个,这里只举例写一个,其它相同

建立Fragment(fragment_one.xml):




  

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:

明细
类别报表

colors.xml:

#000000

相关文章

精彩推荐