Carousel Loop

1

I'm making a Carousel , which I got friend help from here . But now a small problem has arisen, in the example there are 6 images, and upon arriving at the last it stops. I would like to know how I do it so that when I get to the last one, it goes back to the first, type a loop.

    
asked by anonymous 24.08.2016 / 22:59

1 answer

1

I had an idea to do it this way, but there are other ways to do it.

Main

public class MainViewPage extends AppCompatActivity {

    ViewPager mViewPager;

    int[] mResources = {
            R.mipmap.imagem1,
            R.mipmap.imagem2,
            R.mipmap.imagem3,
            R.mipmap.imagem4,
            R.mipmap.imagem5,
            R.mipmap.imagem6
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.carousel);

        mViewPager = (ViewPager) findViewById(R.id.pager);

        CarouselAdapter mCustomPagerAdapter = new CarouselAdapter(this, mResources);
        mViewPager.setOffscreenPageLimit(1);
        mViewPager.setAdapter(mCustomPagerAdapter);


    }
}

Adapter

public class CarouselAdapter extends PagerAdapter {

    private Context mContext;
    private LayoutInflater mLayoutInflater;
    private int[] mResources;


    private int pos = 0;

    public CarouselAdapter(Context context, int[] resources) {
        mContext = context;
        mResources = resources;
        mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return Integer.MAX_VALUE;
    }

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

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View itemView = mLayoutInflater.inflate(R.layout.item_carousel, container, false);

        ImageView imageView = (ImageView) itemView.findViewById(R.id.imageView);
        imageView.setImageResource(mResources[pos]);

        container.addView(itemView);

        if (pos >= mResources.length - 1)
            pos = 0;
        else
            ++pos;

        return itemView;
    }

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

}

XML

In this XML case, it is already adapted with CardView

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_height="match_parent"
    android:layout_width="match_parent">

    <android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/card_view"
        android:layout_width="250dp"
        android:layout_height="250dp"
        android:layout_gravity="center|center_horizontal"
        card_view:cardElevation="8dp"
        card_view:cardCornerRadius="8dp"
        android:layout_centerInParent="true">

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="centerCrop"/>

    </android.support.v7.widget.CardView>

</RelativeLayout>

Screenshots

Takealookatthis InfiniteViewPager project that will also help you have some idea how to do it. Here is an example:

    
25.08.2016 / 02:33