Loop in Carousel

Asked

Viewed 112 times

1

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

  • This is @Carlos Giovani casilo, I have not tested but I believe that, assuming you know the number of images you have in your matrix list, if you do something like this in this line imageView.setImageResource(mResources[position==nrLista?0:position]); would work for Voce

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 case of XML, it is already adapted with the 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

inserir a descrição da imagem aqui

Take a look at this design Infiniteviewpager, that also has will help to have some more idea of how to do. Below follows an example:

inserir a descrição da imagem aqui

  • Thank you very much, I’ll test it right now.

  • It worked perfectly, thank you very much.

  • @carlosgiovanicasilo there are other variations of how to solve this. After further researched for improvement. Abs

Browser other questions tagged

You are not signed in. Login or sign up in order to post.