Android ViewPager的使用

Android studio 实现图片左右滑动效果有很多种方法:

1. ImageSwitcher

2. ViewPager

3. RecyclerView

其中RecyclerView是功能很强大的,和ListView、GridView类似,这里暂时不谈。

ImageSwitcher使用起来很简单,只需要在xml中创建一个ImageSwitcher的组件,并在java中创建一个factory并配置给ImageSwitcher,再给ImageSwitcher添加适当的Listener。

其中factory的作用是为ImageSwitcher创建内部的ImageView,要求override makeView的方法。

而且,ImageSwitcher很简单的就可以挂载animation效果,目前我遇到的最大问题是ImageSwitcher配合Picasso载入网络图片有问题,所以改成了ViewPager。

private boolean isLastPage = false;

private boolean isDragPage = false;

private boolean canJumpPage = true;

private ViewPager sliderLayout;

private DetailImageAdapter dia =new DetailImageAdapter(mainActivity,al);

sliderLayout.setAdapter(dia);

sliderLayout.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

/**

* swipe

    * @param position position

    * @param positionOffset  move offset, 0/1

    * @param positionOffsetPixels  image move offset

*/

    @Override

    public void onPageScrolled(int position,float positionOffset,int positionOffsetPixels) {

        if (isLastPage &&isDragPage && positionOffsetPixels ==0){

            if (canJumpPage){

                canJumpPage =false;

                Toast.makeText(mainActivity,"last page", Toast.LENGTH_SHORT).show();

            }

        }

    }

/**

* display effect between the three.

    * @param position    the current page index

*/

    @Override

    public void onPageSelected(int position) {

        isLastPage = position ==al.size() -1 ;

        switchPoint(position);

    }

/**

* screen

    * @param state  0(END),1(PRESS) , 2(UP) 。

*/

    @Override

    public void onPageScrollStateChanged(int state) {

        isDragPage = state ==1;

    }

});

自定义了Adapter

import android.annotation.SuppressLint;

import android.content.Context;

import android.support.annotation.NonNull;

import android.support.v4.view.PagerAdapter;

import android.support.v4.view.ViewPager;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.ImageView;

import com.squareup.picasso.Picasso;

import java.util.ArrayList;

public class DetailImageAdapterextends PagerAdapter {

    Contextcontext;

    ArrayListimages;

    DetailImageAdapter(Context context, ArrayList al) {

        this.context = context;

        this.images = al;

    }

    @Override

    public int getCount() {

        return images.size();

    }

    @Override

    public boolean isViewFromObject(@NonNull View view,@NonNull Object object) {

        return view == object;

    }

    @Override

    public Object instantiateItem(ViewGroup container,int position) {

        LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        assert layoutInflater !=null;

        @SuppressLint("InflateParams")

        View view = layoutInflater.inflate(R.layout.detailimage,null);

        ImageView imageView = view.findViewById(R.id.img_house);

        Picasso.get().load((images.get(position))).fit().centerCrop().into(imageView);

        ViewPager vp = (ViewPager) container;

        vp.addView(view,0);

        return view;

    }

    @Override

    public void destroyItem (ViewGroup container,int position, Object object) {

        ViewPager vp = (ViewPager) container;

        View view = (View) object;

        vp.removeView(view);

    }

}

你可能感兴趣的:(Android ViewPager的使用)