仿今日頭條/商城商品詳情指示器

仿今日頭條viewpager切換指示器, 商品詳情指示器,以及各類很靈活的定製本身須要的樣式:html

      該控件的實現, 引用的是MagicIndicator. 效果圖以下:java

 具體代碼效果以下:android

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:orientation="vertical">  
  
    <net.lucode.hackware.magicindicator.MagicIndicator  
        android:id="@+id/magic_indicator"  
        android:layout_width="match_parent"  
        android:layout_height="@dimen/dp_40"  
        android:background="@color/bg_view"/>  
    <View style="@style/separator_line" />  
    <android.support.v4.view.ViewPager  
        android:id="@+id/view_pager"  
        android:layout_width="match_parent"  
        android:layout_height="match_parent"/>  
</LinearLayout>  

MagicIndicator的使用:ide

mMagicIndicator = (MagicIndicator)findViewById(R.id.magic_indicator);
        mViewPager = (ViewPager)findViewById(R.id.view_pager);

        this.goodsId = getIntent().getStringExtra("goodsId");
        this.contents  = getIntent().getStringExtra("content");
        this.position = getIntent().getIntExtra("position" ,0);

        fragPagerAdapter = new SPProductDetailInnerTabAdapter(getSupportFragmentManager() , goodsId ,contents);
        mViewPager.setAdapter(fragPagerAdapter);


        // 當前頁不定位到中間
        final CommonNavigator commonNavigator = new CommonNavigator(this);
        commonNavigator.setAdjustMode(true);
        commonNavigator.setScrollPivotX(0.15f);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return mDataList == null ? 0 : mDataList.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                SimplePagerTitleView simplePagerTitleView = new SimplePagerTitleView(context);
                simplePagerTitleView.setText(mDataList.get(index));
                simplePagerTitleView.setNormalColor(getResources().getColor(R.color.sub_title));
                simplePagerTitleView.setSelectedColor(getResources().getColor(R.color.light_red));
                simplePagerTitleView.setTextSize(12);
                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mViewPager.setCurrentItem(index);
                    }
                });
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator indicator = new LinePagerIndicator(context);
                indicator.setMode(LinePagerIndicator.MODE_WRAP_CONTENT);
                indicator.setYOffset(UIUtil.dip2px(context, 3));
                indicator.setColors(getResources().getColor(R.color.light_red));
                return indicator;
            }
        });

        mMagicIndicator.setNavigator(commonNavigator);
        mMagicIndicator.setDelegate(new SimpleViewPagerDelegate(mViewPager));

  

這樣就能夠快速實現一個tab選項卡式效果, 能夠很隨意自定義本身的樣式.this

完整Demo下載地址: http://www.tp-shop.cn/article_id_1098.htmlspa

相關文章
相關標籤/搜索