I would like some help on integrating two pieces of functionality from my code as I am stuck and require some help with it.
Basically what I want to develop is a slider where the user gets to upload images from their gallery onto the slider and flick through the slider to see their uploaded pictures. I would like a maximum of 20 images and lets say there's 5 images uploaded, it will be nice to start from first image, slide all the way to the fifth image and then it goes back to image number one after flicking fifth image.
At the moment I have a piece of code that is ale to upload an image from the gallery and another piece of code that uses PageViewer to create the image slider (however these images are images already stored within drawable for now).
I would be the happiest person if this can be implemented because it is the only part of the app I am stuck on.
Below are the relevant code:
PhotosActivity - code that uploads image from gallery:
private static int loadImageResults = 100;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_photos);
uploadButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openGallery();
}
});
...
public void openGallery() {
Intent intentImageContent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intentImageContent, loadImageResults);
}
PhotoActivity - code that calls on the PhotosPageViewerAdapter class for the image slider:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_photos);
...
PhotosViewPagerAdapter photosViewPagerAdapter = new PhotosViewPagerAdapter(PhotosActivity.this);
photosSlider.setAdapter(photosViewPagerAdapter);
PhotosPageViewerAdapter class:
public class PhotosViewPagerAdapter extends PagerAdapter {
private Context context;
private LayoutInflater layoutInflater;
private Integer [] images = {R.drawable.question, R.drawable.love_heart, R.drawable.love_heart_dark};
public PhotosViewPagerAdapter(Context context){
this.context = context;
}
@Override
public int getCount() {
return images.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public Object instantiateItem(ViewGroup container, int position){
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.activity_photos, null);
ImageView imageView = view.findViewById(R.id.image_content);
imageView.setImageResource(images[position]);
ViewPager vp = (ViewPager) container;
vp.addView(view, 0);
return view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object){
ViewPager vp = (ViewPager) container;
View view = (View) object;
vp.removeView(view);
}
}
Finally the activity_photos.xml where it contains the image view from the upload image and the page viewer for the slider, preferable the uploaded image will go into the page viewer.
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:id="@+id/image_content"/>
<android.support.v4.view.ViewPager
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/photos_slider">
</android.support.v4.view.ViewPager>
Thank you very much