亲宝软件园·资讯

展开

Android ViewPager滑动 Android ViewPager实现左右滑动的实例

龙哥IT 人气:0
想了解Android ViewPager实现左右滑动的实例的相关内容吗,龙哥IT在本文为您仔细讲解Android ViewPager滑动的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Android,ViewPager,Android,ViewPager左右滑动的实现,下面大家一起来学习吧。

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左右滑动的实例,如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

加载全部内容

相关教程
猜你喜欢
用户评论