Android ViewPager实现左右滑动的实例
AndroidViewPager实现左右滑动的实例
多个标题以及标题下的每个View视图
在build.gradle添加包:
//左右滑动布局框架 compile'com.shizhefei:ViewPagerIndicator:1.1.4'
然后在代码中实现功能:
ScrollIndicatorViewscrollIndicatorView; ViewPagerviewPager; privateString[]titles={"在线申办","信用卡代换","信用卡取现","办卡进度"}; privateIndicatorViewPagerindicatorViewPager; privateLayoutInflaterinflate; privateContextmContext; privatevoidinitViewPager(){ //默认字体是14 floatunSelectSize=14; //滑动字体是14*1.2f floatselectSize=unSelectSize; scrollIndicatorView.setOnTransitionListener(newOnTransitionTextListener().setColor(0xFF1DA9FF,Color.GRAY).setSize(selectSize,unSelectSize)); scrollIndicatorView.setScrollBar(newColorBar(mContext,0xFF1DA9FF,2)); viewPager.setOffscreenPageLimit(1); indicatorViewPager=newIndicatorViewPager(scrollIndicatorView,viewPager); inflate=LayoutInflater.from(mContext); indicatorViewPager.setAdapter(newMyAdapter(getFragmentManager())); } privateintsize=titles.length; privateclassMyAdapterextendsIndicatorViewPager.IndicatorFragmentPagerAdapter{ publicMyAdapter(FragmentManagerfragmentManager){ super(fragmentManager); } @Override publicintgetCount(){ returnsize; } @Override publicViewgetViewForTab(intposition,ViewconvertView,ViewGroupcontainer){ if(convertView==null){ convertView=inflate.inflate(R.layout.tab_top,container,false); } TextViewtextView=(TextView)convertView; textView.setText(titles[position%titles.length]); intpadding=dipToPix(10); textView.setPadding(padding,0,padding,0); returnconvertView; } @Override publicFragmentgetFragmentForPage(intposition){ CreditCardFragmentfragment=newCreditCardFragment(); /*Bundlebundle=newBundle(); bundle.putInt(MoreFragment.INTENT_INT_INDEX,position); fragment.setArguments(bundle);*/ returnfragment; //MoreFragmentfragment=newMoreFragment(); //Bundlebundle=newBundle(); //bundle.putInt(MoreFragment.INTENT_INT_INDEX,position); //fragment.setArguments(bundle); //returnfragment; } @Override publicintgetItemPosition(Objectobject){ //这是ViewPager适配器的特点,有两个值POSITION_NONE,POSITION_UNCHANGED,默认就是POSITION_UNCHANGED, //表示数据没变化不用更新.notifyDataChange的时候重新调用getViewForPage returnPagerAdapter.POSITION_NONE; } } privateintdipToPix(floatdip){ intsize=(int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,dip,getResources().getDisplayMetrics()); returnsize; }
以上就是实现AndroidViewPager左右滑动的实例,如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!