孚睿科技·ANDROID知识总结

怎样抽取一个“滑动标题栏+ViewPager”?

2016-09-28  本文已影响310人  安卓小生

怎样抽取一个“滑动标题栏+ViewPager”?

前段时间遇到一个需求:滑动标题栏+可滑动的viewpager展示页面数据,然后到github上找了一圈,发现这个作者写的工程还是蛮好用的LuckyJayce/ViewPagerIndicator,效果如图:

pic1pic1
    @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
       View rootView=super.onCreateView(inflater, container, savedInstanceState);
       fragmentList=getChildFragments();
       titles=getChildTitles();
       Log.i(TAG,titles.toString());
       Log.i(TAG,fragmentList.toString());
       initScrollViewPager(titles,fragmentList);
       return rootView;
   }
    ```
  * 第七步,初始化标题栏和Viewpager的适配器
    ```java
    //fragment的适配器
    private class MyAdapter extends IndicatorViewPager.IndicatorFragmentPagerAdapter {

        public MyAdapter(FragmentManager fragmentManager) {
            super(fragmentManager);
        }

        @Override
        public int getCount() {
            return fragmentList.length;
        }

        @Override
        public View getViewForTab(int position, View convertView, ViewGroup container) {
            if (convertView == null) {
                convertView = getActivity().getLayoutInflater().inflate(R.layout.tab_top, container, false);
            }
            TextView textView = (TextView) convertView;
            textView.setText(titles[position]);

            int witdh = getTextWidth(textView);
//            int padding = DisplayUtil.dipToPix(getContext(), 8);
            //因为wrap的布局 字体大小变化会导致textView大小变化产生抖动,这里通过设置textView宽度就避免抖动现象
            //1.3f是根据上面字体大小变化的倍数1.3f设置
            textView.setWidth((int) (witdh * 1.13f) + 0);
            return convertView;
        }

        @Override
        public Fragment getFragmentForPage(int position) {
            return fragmentList[position];
        }

        private int getTextWidth(TextView textView) {
            if (textView == null) {
                return 0;
            }
            Rect bounds = new Rect();
            String text = textView.getText().toString();
            Paint paint = textView.getPaint();
            paint.getTextBounds(text, 0, text.length(), bounds);
            int width = bounds.left + bounds.width();
            return width;
        }

    }
    //设置数据源
    public void initScrollViewPager(String[] titles,BaseFragment[] fragmentList){
          scrollIndicatorView.setOnTransitionListener(new OnTransitionTextListener().setColor(0xFF2196F3, Color.GRAY).setSize(selectSize, unSelectSize));
  
          scrollIndicatorView.setScrollBar(new ColorBar(getContext(), 0xFF2196F3, 4));
  
          viewPager.setOffscreenPageLimit(2);
          indicatorViewPager = new IndicatorViewPager(scrollIndicatorView, viewPager);
          indicatorViewPager.setAdapter(new MyAdapter(getActivity().getSupportFragmentManager()));
          indicatorViewPager.setCurrentItem(0, true);
      }
    ```

* 如何使用抽取的“滑动标题栏+ViewPager”?  
  直接自定义一个TestFragment继承自ScrollViewpagerBaseFragment,然后系统会提示你去重写ScrollViewpagerBaseFragment的抽象方法,然后初始化数据,并将初始化的数据赋值给这两个方法,就可以使用"滑动标题栏+ViewPager"了。
```java
  public class TestFragment extends ScrollViewpagerBaseFragment{
    private BaseFragment[] childsFragment = {new DeviceInfoFragment(), new DeviceInfoFragment(), new DeviceInfoFragment(), new DeviceInfoFragment(), new DeviceInfoFragment()};
    private String[] childTitle = {"title1","title2","title3","title4","title5"};
    @Override
    public BaseFragment[] getChildFragments() {
        return childsFragment;
    }

    @Override
    public String[] getChildTitles() {
        return childTitle;
    }
}

希望能帮到你!

上一篇下一篇

猜你喜欢

热点阅读