如何进行滑动视图循环

时间:2016-03-17 15:54:08

标签: android android-studio android-viewpager

我正在尝试创建一个应用程序,图像显示在列表视图中。但是我无法循环View Pager。显示在最后一个图像第一张图像出现之后。这是我的代码。我尝试了基本的java方法,但它无法正常工作

package com.union.pr26;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
public class MainActivity extends Activity {
ViewPager viewPager;
CustomeSwipeAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    viewPager=(ViewPager)findViewById(R.id.viewpager);
    adapter=new CustomeSwipeAdapter(this);
    viewPager.setAdapter(adapter);

}
}

ADAPTER

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class CustomeSwipeAdapter extends PagerAdapter {
private Context ctx;
final int position = 0;
private LayoutInflater layoutInflater;
private int []imgID ={R.drawable.img1,R.drawable.img2,R.drawable.img3};
public CustomeSwipeAdapter(Context ctx) {
    this.ctx=ctx;
}

@Override
public int getCount() {
    return imgID.length;
}

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

@Override
public Object instantiateItem(ViewGroup container, int position) {


    layoutInflater =(LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View item_view =layoutInflater.inflate(R.layout.extra,container,false);
    ImageView imageView =(ImageView)item_view.findViewById(R.id.imageView);
    if(position>=3){
        position=-imgID.length;


        position--;
    }
    imageView.setImageResource(imgID[position]);
    container.addView(item_view);
    return item_view;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    container.removeView((LinearLayout)object);
}
}

2 个答案:

答案 0 :(得分:1)

在mainActivity中:

List<Integer> galImages = new ArrayList<>();
    galImages.add(R.drawable.one);
    galImages.add(galImages.get(0));
    galImages.add(0,galImages.get(galImages.size()-2));

   final  ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
    ImageAdapter adapter = new ImageAdapter(this,galImages);
    viewPager.setAdapter(adapter);
    viewPager.setCurrentItem(1, false);
    viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {

        }

        @Override
        public void onPageScrollStateChanged(int state) {
            if (state == ViewPager.SCROLL_STATE_IDLE) { //this is triggered when the switch to a new page is complete
                final int lastPosition = viewPager.getAdapter().getCount() - 1;
                int currentPosition = viewPager.getCurrentItem();
                if (currentPosition == lastPosition) {
                    viewPager.setCurrentItem(1, false); //false so we don't animate
                } else if (currentPosition == 0) {
                    viewPager.setCurrentItem(lastPosition - 1, false);
                }

            }
        }
    });

在我的例子中,我在我的列表中添加了一个图像,然后再次添加它。然后在onPageScrollStateChaged中我一遍又一遍地重复它

答案 1 :(得分:0)

您必须使用InfiniteViewPager。这是实现相同目标的链接。 GitHub Link