Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use viewpager slide indicator in Android

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

这篇文章主要介绍"Android中怎么使用viewpager滑动指示器",在日常操作中,相信很多人在Android中怎么使用viewpager滑动指示器问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"Android中怎么使用viewpager滑动指示器"的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

工程目录:

1. MainActivity.java

public class MainActivity extends FragmentActivity { private ViewPagerFrameAdapter adapter; //适配器(标题和内容) private ViewPager mPager; private TabPageIndicator tabbPageIndicator; // private UnderlinePageIndicatorEx underlinePageIndicator; private int COUNT = 0; private List titles = new ArrayList(); //标题 @Override protected void onCreate(Bundle bundle) { super.onCreate(bundle); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); //标题,这里自己造的假数据 for (int j = 0; j < 3 ; j++){ titles.add( "第"+ j +"标签卡"); } mPager = (ViewPager)findViewById(R.id.pager); adapter = new ViewPagerFrameAdapter(getSupportFragmentManager(),titles); mPager.setAdapter(adapter); //得到指示器 tabbPageIndicator = (TabPageIndicator) findViewById(R.id.tab_indicator); tabbPageIndicator.setViewPager(mPager); //下标 underlinePageIndicator = (UnderlinePageIndicatorEx)findViewById(R.id.underline_indicator); underlinePageIndicator.setViewPager(mPager); underlinePageIndicator.setFades(false); //设置指示器 tabbPageIndicator.setOnPageChangeListener(underlinePageIndicator); } //adapter 没啥好说的 跟前面的一样 private class ViewPagerFrameAdapter extends FragmentStatePagerAdapter{ private List title; //页面标题头部数据 public ViewPagerFrameAdapter(FragmentManager fm, List title) { super(fm); this.title = title; } //根据position 返回 fragment @Override public Fragment getItem(int position) { return ArrayFragment.newInstance(position); } //size @Override public int getCount() { return title.size(); } //顶部的title @Override public CharSequence getPageTitle(int position) { if (titles != null){ return titles.get(position).toString(); } return super.getPageTitle(position); } } }

UnderlinePageIndicatorEx.java

//继承这个然后重写 public class UnderlinePageIndicatorEx extends UnderlinePageIndicator{ public UnderlinePageIndicatorEx(Context context) { super(context, null); } public UnderlinePageIndicatorEx(Context context, AttributeSet attrs) { super(context, attrs, R.attr.vpiUnderlinePageIndicatorStyle); } public UnderlinePageIndicatorEx(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } //自身的 ViewPage传递过去 @Override public void setViewPager(ViewPager viewPager) { if (mViewPager == viewPager){ return; } // if (mViewPager != null) { // mViewPager.setOnPageChangeListener(null); // } if (viewPager.getAdapter() == null) { throw new IllegalStateException(" pager 没有 加入 adapter"); } mViewPager = viewPager; // mViewPager.setOnPageChangeListener(this); invalidate(); post(new Runnable(){ @Override public void run() { if (mFades) { post(mFadeRunnable); } } }); } }

ArrayFragment.java

public class ArrayFragment extends Fragment{ private int position; public ArrayFragment(int position){ this.position = position; } // 返回 viwePager 下面滑动的页面 public static ArrayFragment newInstance(int position){ return new ArrayFragment(position); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public void onDestroy() { super.onDestroy(); } // 根据position 返回不同的view @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.layoutaa, null); return view; } }

好了接下来是 布局xml了。

接下来设置 滑动指示器的style

@style/CustomTabPageIndicator @style/CustomUnderlinePageIndicator #00000000 @style/CustomTabPageIndicator_text @color/selector_tab 20sp @drawable/custom_tab_indicator_divider 10dip middle 4dp 8dp 4dp 8dp horizontal 8dp monospace #EB5548 #00000000 1000 1000

selector_tab.xml 这个是 选择器 在style中引用的,是用来切换 被选中的背景色的

到此,关于"Android中怎么使用viewpager滑动指示器"的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report