Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How could I develop an image carousel on Android?

I am trying to put a carousel on my top half of Android screen.

I like Bootstrap carousels like:

http://www.w3schools.com/bootstrap/bootstrap_carousel.asp

I am not sure the best way to approach this? Do I use a GridView in top half of my screen??

like image 297
Kamilski81 Avatar asked Nov 30 '22 00:11

Kamilski81


2 Answers

First you must create a Viewpager into your xml like:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:id="@+id/relativeLayout">


    <android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </android.support.v4.view.ViewPager>

</RelativeLayout>

For a custom Adapter you need to create a res/layout/pager_item.xml which will be used to inflate and generate a view into Adapter:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

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

</LinearLayout>

After that, you can create the custom Adapter to set your ViewPager:

public class CustomPagerAdapter extends PagerAdapter {

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

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

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

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

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

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

        container.addView(itemView);

        return itemView;
    }

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

}

Then put the following code into onCreate in your Activity:

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

// This is just an example. You can use whatever collection of images.
int[] mResources = {
    R.drawable.first,
    R.drawable.second,
    R.drawable.third,
    R.drawable.fourth,
    R.drawable.fifth,
    R.drawable.sixth
};

CustomPagerAdapter mCustomPagerAdapter = new CustomPagerAdapter(this, mResources);

mViewPager.setAdapter(mCustomPagerAdapter);

Take a look the following tutorial for more details: http://codetheory.in/android-image-slideshow-using-viewpager-pageradapter/

like image 91
Lennon Spirlandelli Avatar answered Dec 04 '22 01:12

Lennon Spirlandelli


Of simply use Android Image Slider, it's a beautiful and simple to use Library : https://github.com/daimajia/AndroidImageSlider

like image 44
florent champigny Avatar answered Dec 04 '22 01:12

florent champigny