欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android ViewPager实现左右滑动的实例

程序员文章站 2023-12-18 13:05:46
android viewpager实现左右滑动的实例 多个标题以及标题下的每个view视图

android viewpager实现左右滑动的实例

多个标题以及标题下的每个view视图

<com.shizhefei.view.indicator.scrollindicatorview 
   android:id="@+id/moretab_indicator" 
   android:layout_width="match_parent" 
   android:layout_height="45dp" /> 
 <view 
   android:layout_width="match_parent" 
   android:layout_height="0.5dp" 
   android:background="#f2f2f2" 
   /> 
 <android.support.v4.view.viewpager 
   android:id="@+id/moretab_viewpager" 
   android:layout_width="match_parent" 
   android:layout_height="match_parent" /> 

 在build.gradle添加包:

//左右滑动布局框架 
 compile 'com.shizhefei:viewpagerindicator:1.1.4' 

然后在代码中实现功能:

scrollindicatorview scrollindicatorview; 
  viewpager viewpager; 
  private string[] titles = {"在线申办", "信用卡代换", "信用卡取现", "办卡进度"}; 
  private indicatorviewpager indicatorviewpager; 
  private layoutinflater inflate; 
  private context mcontext; 
 
 
 
 private void initviewpager() { 
//    默认字体是14 
    float unselectsize = 14; 
//    滑动字体是14*1.2f 
    float selectsize = unselectsize; 
    scrollindicatorview.setontransitionlistener(new ontransitiontextlistener().setcolor(0xff1da9ff, color.gray).setsize(selectsize, unselectsize)); 
    scrollindicatorview.setscrollbar(new colorbar(mcontext, 0xff1da9ff, 2)); 
 
    viewpager.setoffscreenpagelimit(1); 
    indicatorviewpager = new indicatorviewpager(scrollindicatorview, viewpager); 
    inflate = layoutinflater.from(mcontext); 
    indicatorviewpager.setadapter(new myadapter(getfragmentmanager())); 
  } 
 
  private int size = titles.length; 
 
 
  private class myadapter extends indicatorviewpager.indicatorfragmentpageradapter { 
 
    public myadapter(fragmentmanager fragmentmanager) { 
      super(fragmentmanager); 
    } 
 
 
    @override 
    public int getcount() { 
      return size; 
    } 
 
    @override 
    public view getviewfortab(int position, view convertview, viewgroup container) { 
      if (convertview == null) { 
        convertview = inflate.inflate(r.layout.tab_top, container, false); 
      } 
      textview textview = (textview) convertview; 
      textview.settext(titles[position % titles.length]); 
      int padding = diptopix(10); 
      textview.setpadding(padding, 0, padding, 0); 
      return convertview; 
    } 
 
    @override 
    public fragment getfragmentforpage(int position) { 
      creditcardfragment fragment = new creditcardfragment(); 
      /* bundle bundle = new bundle(); 
      bundle.putint(morefragment.intent_int_index, position); 
      fragment.setarguments(bundle);*/ 
      return fragment; 
 
//      morefragment fragment = new morefragment(); 
//      bundle bundle = new bundle(); 
//      bundle.putint(morefragment.intent_int_index, position); 
//      fragment.setarguments(bundle); 
//      return fragment; 
    } 
 
    @override 
    public int getitemposition(object object) { 
      //这是viewpager适配器的特点,有两个值 position_none,position_unchanged,默认就是position_unchanged, 
      // 表示数据没变化不用更新.notifydatachange的时候重新调用getviewforpage 
      return pageradapter.position_none; 
    } 
 
  } 
 
  private int diptopix(float dip) { 
    int size = (int) typedvalue.applydimension(typedvalue.complex_unit_dip, dip, getresources().getdisplaymetrics()); 
    return size; 
  } 

以上就是实现android viewpager左右滑动的实例,如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

上一篇:

下一篇: