viewPager实现多卡片滚动效果

直接上代码
定义xml文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:clipChildren="false"
    android:layout_height="match_parent">
    <android.support.v4.view.ViewPager
        android:layout_gravity="center_horizontal"
        android:id="@+id/Viewpager"
        android:layout_width="match_parent"
        android:layout_marginTop="@dimen/d20"
        android:layout_marginBottom="@dimen/d30"
        android:layout_marginLeft="@dimen/d60"
        android:layout_marginRight="@dimen/d60"
        android:clipChildren="false"
        android:layout_height="match_parent"/>
    <LinearLayout
        android:layout_marginRight="5dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:id="@+id/indicator"
        android:orientation="horizontal"
        android:layout_marginBottom="5dp"
        android:layout_width="wrap_content"
        android:layout_height="15dp"/>
</RelativeLayout>

activity中的代码

V_SchoolsAdapter schoolsAdapter = new V_SchoolsAdapter(getActivity(),mDataList);
Viewpager.setAdapter(schoolsAdapter);
Viewpager.setPageMargin(30);
Viewpager.setOffscreenPageLimit(3);
Viewpager.setPageTransformer(false, new AlphaTransformer());

V_SchoolsAdapter 代码

public class V_SchoolsAdapter extends PagerAdapter {
    private List<getArcticListResponse.ariticInfo> dataList;
    private Context context;
    public static final int MAX_SCROLL_VALUE = 10000;

    public V_SchoolsAdapter(Context context, List<getArcticListResponse.ariticInfo> dataList) {
        this.context = context;
        this.dataList = dataList;
    }
    /**
     * @param container
     * @param position
     * @return 对position进行求模操作
     * 因为当用户向左滑时position可能出现负值,所以必须进行处理
     */
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        position %= dataList.size();
        getArcticListResponse.ariticInfo ariticInfo = dataList.get(position);
        View ret = LayoutInflater.from(context).inflate(R.layout.h_adapter_coursename_item, container,false);
        TextView textView = ret.findViewById(R.id.name);
        textView.setText(ariticInfo.CreateTime);
        ViewParent viewParent = ret.getParent();
        if (viewParent != null) {
            ViewGroup parent = (ViewGroup) viewParent;
            parent.removeView(ret);
        }
        container.addView(ret);

        return ret;
    }
    /**
     * 由于我们在instantiateItem()方法中已经处理了remove的逻辑,
     * 因此这里并不需要处理。实际上,实验表明这里如果加上了remove的调用,
     * 则会出现ViewPager的内容为空的情况。
     *
     * @param container
     * @param position
     * @param object
     */
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        //警告:不要在这里调用removeView, 已经在instantiateItem中处理了
    }


    @Override
    public int getCount() {
        int ret = 0;
        if (dataList.size() > 0) {
            ret = MAX_SCROLL_VALUE;
        }
        return ret;
    }

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

    class MyViewHolder extends RecyclerView.ViewHolder {

        private final TextView name;


        public MyViewHolder(View itemView) {
            super(itemView);
            name =  itemView.findViewById(R.id.name);
        }
    }
}

AlphaTransformer的代码

public class AlphaTransformer implements ViewPager.PageTransformer {
    private static final float MIN_ALPHA = 0.5f;
    private static final float MIN_SCALE = 0.90f;

    @Override
    public void transformPage(View page, float position) {
        if (position < -1 || position > 1) {
            page.setAlpha(MIN_ALPHA);
            page.setScaleY(MIN_SCALE);
        } else   if (position <= 1) { // [-1,1]
            float scaleFactor = Math.max(MIN_SCALE, 1 - Math.abs(position));
            if (position < 0) {
                float scaleX = 1 + 0.1f * position;
                page.setScaleY(scaleX);
            } else {
                float scaleX = 1 - 0.1f * position;
                page.setScaleY(scaleX);
            }
            page.setAlpha(MIN_ALPHA + (scaleFactor - MIN_SCALE) / (1 - MIN_SCALE) * (1 - MIN_ALPHA));
        }
    }
}

你可能感兴趣的:(viewPager实现多卡片滚动效果)