Android Studio使用ViewPager+Fragment实现滑动菜单Tab效果
本文为大家分享了Android Studio实现滑动菜单Tab效果的具体代码,供大家参考,具体内容如下
描述:
之前有做过一个记账本APP,拿来练手的,做的很简单,是用Eclipse开发的;
最近想把这个APP重新完善一下,添加了一些新的功能,并选用Android Studio来开发;
APP已经完善了一部分,现在就想把已经做好的功能整理一下,记录下来。
效果图:
可以手动滑动菜单
也可以通过点击头部菜单进行切换
具体实现的代码:
前台代码(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);//初始设置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<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的滑动修改头部导航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):
<?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">明细</string> <string name="category_report_tab">类别报表</string>
colors.xml:
<color name="main_tab_text_color">#000000</color>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
赞 (0)