Android中怎么使用ViewPager+Fragment实现滑动菜单Tab效果?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
前台代码(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>
看完上述内容,你们掌握Android中怎么使用ViewPager+Fragment实现滑动菜单Tab效果的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。