AndroidUI组件---SlidingTabLayout实现ViewPager页卡滑动效果

使用SlidingTabLayout需要准备2个类,分别是 SlidingTabLayout,与SlidingTabStrip,,放进项目中时只用修改下包名即可。
这里写图片描述

效果制作的不是很好。
这篇文章,也是在网上搜了很多资源参考,对 SlidingTabLayout.java和SlidingTabStrip.java进行了修改。大家可以更改他的格式字体大小、选中状态,分割线调整等等。先上传这两个文件,改动支出都做了注释。
SlidingTabLayout.java

/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.my.slidingtablayout;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Build;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * To be used with ViewPager to provide a tab indicator component which give constant feedback as to
 * the user's scroll progress.
 * <p>
 * To use the component, simply add it to your view hierarchy. Then in your
 * {@link android.app.Activity} or {@link android.support.v4.app.Fragment} call
 * {@link #setViewPager(ViewPager)} providing it the ViewPager this layout is being used for.
 * <p>
 * The colors can be customized in two ways. The first and simplest is to provide an array of colors
 * via {@link #setSelectedIndicatorColors(int...)} and {@link #setDividerColors(int...)}. The
 * alternative is via the {@link TabColorizer} interface which provides you complete control over
 * which color is used for any individual position.
 * <p>
 * The views used as tabs can be customized by calling {@link #setCustomTabView(int, int)},
 * providing the layout ID of your custom layout.
 */
public class SlidingTabLayout extends HorizontalScrollView {
   

    /**
     * Allows complete control over the colors drawn in the tab layout. Set with
     * {@link #setCustomTabColorizer(TabColorizer)}.
     */
    public interface TabColorizer {
   

        /**
         * @return return the color of the indicator used when {@code position} is selected.
         */
        int getIndicatorColor(int position);

        /**
         * @return return the color of the divider drawn to the right of {@code position}.
         */
        int getDividerColor(int position);

    }

    private static final int TITLE_OFFSET_DIPS = 24;
    private static final int TAB_VIEW_PADDING_DIPS = 16;   //内边距
    private static int TAB_VIEW_TEXT_SIZE_SP = 16;   //字体大小

    private int mTitleOffset;

    private int mTabViewLayoutId;
    private int mTabViewTextViewId;

    // 定义两种需要添加的选项卡颜色
    private int mDefaultTextColor;
    private int mSelectedTextColor;

    private ViewPager mViewPager;
    private ViewPager.OnPageChangeListener mViewPagerPageChangeListener;

    private final SlidingTabStrip mTabStrip;

    public SlidingTabLayout(Context context) {
        this(context, null);
    }

    public SlidingTabLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SlidingTabLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        // 获取选项卡颜色,如果未定义的话,则使用主题默认的颜色
        TypedArray a = context.obtainStyledAttributes(attrs,
                R.styleable.SlidingTabLayout);
        int defaultTextColor = a.getColor(
                R.styleable.SlidingTabLayout_android_textColorPrimary, 0);
        mDefaultTextColor = a.getColor(
                R.styleable.SlidingTabLayout_textColorTabDefault, defaultTextColor);
        mSelectedTextColor = a.getColor(
                R.styleable.SlidingTabLayout_textColorTabSelected ,defaultTextColor);
        a.recycle();


        // Disable the Scroll Bar
        setHorizontalScrollBarEnabled(false);
        // Make sure that the Tab Strips fills this View
        setFillViewport(true);

        mTitleOffset = (int) (TITLE_OFFSET_DIPS * getResources().getDisplayMetrics().density);

        mTabStrip = new SlidingTabStrip(context);
        addView(mTabStrip, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    }

    // 在每次选项改变时更新选项卡文本颜色的新方法
    private void updateSelectedTitle(int position) {
        final PagerAdapter adapter = mViewPager.getAdapter();
        for (int i = 0; i < adapter.getCount(); i++) {
            final View tabView = mTabStrip.getChildAt(i);
            if (TextView.class.isInstance(tabView)) {
                TextView titleView = (TextView)tabView;
                boolean isSelected = i == position;
                titleView.setTextColor(isSelected ? mSelectedTextColor : mDefaultTextColor);
            }
        }
    }
    /**
     * Set the custom {@link TabColorizer} to be used.
     * <p>
     * If you only require simple custmisation then you can use
     * {@link #setSelectedIndicatorColors(int...)} and {@link #setDividerColors(int...)} to achieve
     * similar effects.
     */
    public void setCustomTabColorizer(TabColorizer tabColorizer) {
        mTabStrip.setCustomTabColorizer(tabColorizer);
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Android实现左右滑动动画,可以使用ViewPager。ViewPager是一个视图容器,可以让用户在屏幕上左右滑动,以便浏览多个面。 下面是一些步骤,以实现ViewPager左右滑动效果: 1. 在布局文件中添加ViewPager。 ```xml <android.support.v4.view.ViewPager android:id="@+id/viewPager" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FFFFFF" /> ``` 2. 在MainActivity中,创建一个PagerAdapter类,它将管理面。 ```java public class MyPagerAdapter extends PagerAdapter { private List<View> viewList; public MyPagerAdapter(List<View> viewList) { this.viewList = viewList; } @Override public int getCount() { return viewList.size(); } @Override public boolean isViewFromObject(View view, Object object) { return view == object; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView(viewList.get(position)); } @Override public Object instantiateItem(ViewGroup container, int position) { container.addView(viewList.get(position)); return viewList.get(position); } } ``` 3. 在MainActivity中,为ViewPager设置PagerAdapter。 ```java ViewPager viewPager = findViewById(R.id.viewPager); List<View> viewList = new ArrayList<>(); viewList.add(new View(this)); viewList.add(new View(this)); viewList.add(new View(this)); MyPagerAdapter adapter = new MyPagerAdapter(viewList); viewPager.setAdapter(adapter); ``` 现在,当用户在ViewPager中左右滑动时,面将滑动到下一实现了左右滑动效果。 当然,你可以在PagerAdapter中添加自己的View,以实现更多的自定义效果

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值