ViewPager实现联动效果

添加依赖

 compile 'com.android.support:design:26.1.0'
 compile 'com.tencent.mm.opensdk:wechat-sdk-android-without-mta:+'

TabLayout实现联动效果
第一步:创建xml文件名字为vipexplain**




    

    

    

        

    

    

第二步:创建Activity 名字为:VipExplain

package haolaidai.cloudcns.com.haolaidai_as.main.Vip.explain;

import android.content.Intent;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.youth.banner.Banner;
import com.youth.banner.transformer.DepthPageTransformer;

import java.util.ArrayList;
import java.util.List;

import haolaidai.cloudcns.com.haolaidai_as.R;
import haolaidai.cloudcns.com.haolaidai_as.User;
import haolaidai.cloudcns.com.haolaidai_as.comm.base.BaseActivity;
import haolaidai.cloudcns.com.haolaidai_as.main.Vip.Model;
import haolaidai.cloudcns.com.haolaidai_as.main.Vip.rush.RushMoney;

/**
 * Created by Administrator on 2018/9/18.
 */

public class VipExplain extends BaseActivity {
    private List titleList;
    private List mList;
    private TabLayout mTab;
    private ViewPager mViewPager;
    private ViewGroup linearLayout;
    private static final float MAX_SCALE = 0.9f;
    private static final String  VIP_explain = "会员权益说明";
    private Button bt;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.vipexplain);
        ((TextView) findViewById(R.id.pu_tv)).setText(VIP_explain);
        bt = findViewById(R.id.bt);
        initView();
        oncilk();
        if(User.vipFlag==1){
            bt.setText("立即续费");
        }else if(User.vipFlag==0){
            bt.setText("立即开通");
        } else if(User.vipFlag==2){
            bt.setText("立即续费");
        }

    }

    private void oncilk() {
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                startActivity(new Intent(VipExplain.this, RushMoney.class));
            }
        });
    }

    private void initView() {
        linearLayout = findViewById(R.id.layout_viewpager_container);
        mViewPager = (ViewPager) findViewById(R.id.main_viewpager);

        mViewPager.setOffscreenPageLimit(2);
        //设置与下一个item的距离
        mViewPager.setPageMargin(0);
        linearLayout.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return mViewPager.dispatchTouchEvent(event);
            }
        });
//设置动画缩放大小,并且设置透明度
        mViewPager.setPageTransformer(true, new ViewPager.PageTransformer() {
            @Override
            public void transformPage(View page, float position) {
                Log.i("TAG", "【page】:" + page + ",【position】:" + position);
                if (position < -1 || position > 1) {
                    //不可见区域
                    page.setAlpha(1);
                    page.setScaleX(MAX_SCALE);
                    page.setScaleY(MAX_SCALE);
                } else {
                    //可见区域,透明度效果
//                    if (position <= 0) {
//                        //pos区域[-1,0)
//                        page.setAlpha(MAX_ALPHA + MAX_ALPHA * (1 + position));
//                    } else {
//                        //pos区域[0,1]
//                        page.setAlpha(MAX_ALPHA + MAX_ALPHA * (1 - position));
//                    }

                    //可见区域,缩放效果
                    float scale = Math.max(MAX_SCALE, 1 - Math.abs(position));
                    page.setScaleX(scale);
                    page.setScaleY(scale);
                }

            }
        });
        //设置标题  极速贷款,拒就赔,搞下款率  私人客服  人工测评 增加收益 征信查询 分享赚钱 生活服务
        titleList = new ArrayList<>();
        titleList.add("极速贷款");
        titleList.add("拒就赔");
        titleList.add("高下款率");
        titleList.add("私人客服");
        titleList.add("人工测评");
        titleList.add("增加收益");
        titleList.add("征信查询");
        titleList.add("分享赚钱");
        titleList.add("生活服务");
        //设置ViewPager里面也要显示的图片
        mList = new ArrayList<>();
        mList.add(new Model(titleList.get(0),R.mipmap.jsdk,"会员用户申请贷款将由公司提供极速通道," +
                "为会员直接联系贷款公司及信贷经理,将在最短时间内为会员用户解决贷款需求") );
        mList.add(new Model(titleList.get(1),R.mipmap.jjp,"会员用户无论处于任何原因导致申请平台内产品无法获取资金,平台都将为会员用户进行赔付") );
        mList.add(new Model(titleList.get(2),R.mipmap.gxkl,"会员用户所申请的贷款将由人工客服受理,为用户推荐最适合的贷款产品并迅速为用户提报贷款需求至贷款终端") );
        mList.add(new Model(titleList.get(3),R.mipmap.srkf,"亚热贷将为会员用户搭配专属私人客服,八小时随时在线为会员用户解决贷款疑问及需求") );
        mList.add(new Model(titleList.get(4),R.mipmap.rgcp,"亚热贷将为用户提供人工测评服务,由专属客服为用户测评信息并匹配贷款产品") );
        mList.add(new Model(titleList.get(5),R.mipmap.zjsy,"会员用户所获得的收益将会高于非会员用户,其中收益项目包括,申请红包、拒就赔、分享有礼等") );
        mList.add(new Model(titleList.get(6),R.mipmap.zxjc,"会员用户可以通过人工客服提供信息查询征信信息并为用户绝对保密") );
        mList.add(new Model(titleList.get(7),R.mipmap.fxzq,"会员可以通过分享私人二维码至其他用户,其他用户通过二维码注册进来并购买会员即可得分享有礼红包") );
        mList.add(new Model(titleList.get(8),R.mipmap.shfw,"私人客服将为您提供力所能及的服务,这是专属于您的私人小助手") );

        mTab = (TabLayout) findViewById(R.id.main_tab);
        //设置tab的模式 现在是滑动模式 可以设置静止模式
        mTab.setTabMode(TabLayout.MODE_SCROLLABLE);
        //设置选中颜色
        mTab.setTabTextColors(getResources().getColor(R.color.black_xq), getResources().getColor(R.color.c1ac7e));
        //去掉下面的滑动线条 tabIndicatorHeight  偷懒只是给下面的线条该了一个颜色
        mTab.setSelectedTabIndicatorColor(getResources().getColor(R.color.white));
        //添加tab选项卡
        for (int i = 0; i < titleList.size(); i++) {
            mTab.addTab(mTab.newTab().setText(titleList.get(i)));
        }
        //把TabLayout和ViewPager关联起来
        mTab.setupWithViewPager(mViewPager);
        //实例化adapter
        mViewPager.setAdapter(new MyViewPagerAdapter(this, mList, titleList));
        //给ViewPager绑定Adapter

    }
}

第三步:创建用到的PageAdapter 名字为 MyViewPagerAdapter

public class MyViewPagerAdapter extends PagerAdapter {
    private List list;
    private List titleList;
    private Context context;
    public MyViewPagerAdapter(Context context,List mList,List titleList) {
        this.context = context;
        this.list = mList;
        this.titleList = titleList;
    }


    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view==object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View view = View.inflate(context, R.layout.vipexplain_item,null);
        ((ImageView)view.findViewById(R.id.iv_ion)).setImageResource(list.get(position).getPicture());
        ((TextView)view.findViewById(R.id.tv_title)).setText(list.get(position).getTitle());
        ((TextView)view.findViewById(R.id.tv_content)).setText(list.get(position).getContent());
        container.addView(view);
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        // super.destroyItem(container,position,object); 这一句要删除,否则报错
        container.removeView((View)object);
    }
    @Override
    public CharSequence getPageTitle(int position) {
        return titleList.get(position);//页卡标题
    }
}

你可能感兴趣的:(android,studio)