-2

Is it possible to create a carousel in android which contains set of images which is horizontally aligned in list view . And also i want to highlight one image item when it is clicked.

Hitesh Sahu
  • 41,955
  • 17
  • 205
  • 154
Maria
  • 99
  • 2
  • 10

3 Answers3

0

Please use RecyclerView instead of using ListView. Check out this code - Carousel. Use RecyclerView as a root view with a LinearLayoutManager.HORIZONTAL as a LayoutManager.

Selvakumar
  • 84
  • 7
0

Take view Pager inside in layout

 <android.support.v4.view.ViewPager
    android:id="@+id/viewPager"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />

in your activty

public class Event_Image_Slider extends Activity {
ViewPager viewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_event__image__slider);
    viewPager = (ViewPager) findViewById(R.id.viewPager);

    CustomAdapter adapter = new CustomAdapter(Event_Image_Slider.this);
    viewPager.setAdapter(adapter);
   }
}

Your Custom adpter code ,before it in your activty declare ArrayList imagepathArray =new ArrayList();

public class CustomAdapter extends PagerAdapter{

Context context;

public CustomAdapter(Context context){
    this.context = context;

}


@Override
public Object instantiateItem(ViewGroup container, int position) {
    // TODO Auto-generated method stub

    LayoutInflater inflater = ((Activity)context).getLayoutInflater();

    View viewItem = inflater.inflate(R.layout.image_item, container, false);
    ImageView imageView = (ImageView) viewItem.findViewById(R.id.imageView10);

    Glide.with(context).load(yourActivty.imagepathArray.get(position)).into(imageView);



    ((ViewPager)container).addView(viewItem);

    return viewItem;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return yourActivty.imagepathArray.size();
}

@Override
public boolean isViewFromObject(View view, Object object) {
    // TODO Auto-generated method stub

    return view == ((View)object);
}


@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    // TODO Auto-generated method stub
    ((ViewPager) container).removeView((View) object);
}}
Dixit Panchal
  • 3,406
  • 1
  • 11
  • 14
  • So here the carousel is not need ? Insted of that we are using the viewPager. – Maria Jun 02 '16 at 05:56
  • Can you please check this link http://stackoverflow.com/questions/37559984/how-to-select-and-deselect-each-item-in-horizontal-carousel-in-android – Maria Jun 02 '16 at 05:58
  • If You want just show Images only in Horizonataly then you have to use HorizontalScrollView or else View Pager – Dixit Panchal Jun 02 '16 at 06:11
0

You can use this github library for carousel functionality in your project here is the link of carouselview library.

App level Gradle file (not project level gradle):

compile 'com.synnapps:carouselview:0.0.9'

Include following code in your layout:

<com.synnapps.carouselview.CarouselView
    android:id="@+id/carouselView"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    app:fillColor="#FFFFFFFF"
    app:pageColor="#00000000"
    app:radius="6dp"
    app:slideInterval="3000"
    app:strokeColor="#FF777777"
    app:strokeWidth="1dp"/>

Include following code in your activity

    public class SampleCarouselViewActivity extends AppCompatActivity {

    CarouselView carouselView;

    int[] sampleImages = {R.drawable.image_1, R.drawable.image_2, R.drawable.image_3, R.drawable.image_4, R.drawable.image_5};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sample_carousel_view);

        carouselView = (CarouselView) findViewById(R.id.carouselView);
        carouselView.setPageCount(sampleImages.length);

        carouselView.setImageListener(imageListener);
    }

    ImageListener imageListener = new ImageListener() {
        @Override
        public void setImageForPosition(int position, ImageView imageView) {
            imageView.setImageResource(sampleImages[position]);
        }
    };
}

Also you can explore it extra supported xml Attributes on given link.

Anant Shah
  • 3,744
  • 1
  • 35
  • 48