ViewPager实现左右滑动效果功能

老汪的技术人生 2017-08-17

多个标题以及标题下的每个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;
    }

相关推荐