This is a code I downloaded from here. It is storing the images in an array and is displayed accordingly when I run it. I want to store the images in a matrix and do that. i.e. swipe up,down,right,left for different images. I also want to skip some positions in the matrix. some sort of picture-map kind of thing. Is it possible editing this code? Or will I have to use a completely new syntax? How do I do it?
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
ImagePagerAdapter adapter = new ImagePagerAdapter();
viewPager.setAdapter(adapter);
}
private class ImagePagerAdapter extends PagerAdapter {
private int[] mImages = new int[] {
R.drawable.chiang_mai,
R.drawable.himeji,
R.drawable.petronas_twin_tower,
R.drawable.ulm
};
#Override
public int getCount() {
return mImages.length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
Context context = MainActivity.this;
ImageView imageView = new ImageView(context);
int padding = context.getResources().getDimensionPixelSize(
R.dimen.padding_medium);
imageView.setPadding(padding, padding, padding, padding);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setImageResource(mImages[position]);
((ViewPager) container).addView(imageView, 0);
return imageView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
}
}
All depends on the number of pictures you have.
If there are not so many, you could give TableLayout a try. However, this implies starting over. You will have to implement scrolling, for instance, yourself.
If there are many pictures, you will have to work with (one or two) adapter classes. I believe there are two basic options.
You could reuse the code you have, make as many ImagePagerAdapter s as you want to have rows, put those adapters into an array which could be the data for a VerticalViewPager (https://github.com/castorflex/VerticalViewPager, for instance). This is probably the most difficult solution, but you will also learn a lot.
You could implement a GridView which is scrollable in two dimensions and needs an adapter, a BaseAdapter for example, which is different from the adapter you have in your code. However, if you can manage the skipping of positions (which you write about and which I don't really understand) in a one-dimensional array, too, this is a viable solution.
Related
I try to implement Fragment into ViewPager to make my app more flexible. My app contains TextView and I have a few phrases that will appear one by one if you swap the page from left to right. For now it works in this way like I posted but I want to add a button or maybe some more function, that's why I want to make it like constructor from Fragments.. I try to change my code watching on this post but can't totally understand how to do it. I created two layouts: one for text view another one to put there fragment. Could any one show me how to do it clear?
public class SwipeAdapter extends PagerAdapter{
private int[] car = {R.string.car1, R.string.car2,
R.string.car3, R.string.car4, R.string.car5};
private Context context;
private LayoutInflater layoutInflater;
public SwipeAdapter(Context context){
this.context = context;
}
#Override
public int getCount() {
return car.length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return (view==(RelativeLayout)object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = layoutInflater.inflate(R.layout.carSwipe, container, false);
TextView textView = (TextView) itemView.findViewById(R.id.interTextView);
textView.setText(car[position]);
container.addView(itemView);
return itemView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((RelativeLayout)object);
}
You don't need ViewPager with Fragments to Achieve of showing a TextView.
You can try:
In your activity layout xml that you set in content View add a ViewPager with width and height of your preference.
Then you can set the adapter that you have posted above for that ViewPager and it will work.
Alternatively if you still need to inflate Fragments then you need to extend FragmentStateAdapter of FragmentAdapter. That will give you the ability to have in the ViewPager Fragment,
How can I permanently delete a page from a view pager. I want to delete a page when the user presses a button present on that screen. I am struggling a lot with this. I read the documentation of pageradapter and found the destroyitem but it didn't help me much though. I am able to add the images in viewpager but deleting is still giving me a tough time. Please help.
Below is the code for the activity in which images are fetched and I want to delete an image on user request.
public class ImageDisplayActivity extends Activity {
private List<String> filePaths;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display1);
// Get Image Paths for the PagerAdapter
filePaths = getIntent().getExtras().getStringArrayList("filePaths");
int currPosition = getIntent().getExtras().getInt("position");
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
viewPager.setCurrentItem(currPosition);
ImagePagerAdapter adapter = new ImagePagerAdapter();
viewPager.setAdapter(adapter);
}
private class ImagePagerAdapter extends PagerAdapter {
#Override
public int getCount() {
return filePaths.size();
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
Context context = ImageDisplayActivity.this;
ImageView imageView = new ImageView(context);
int padding = 16;
imageView.setPadding(padding, padding, padding, padding);
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
// Get Image
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = false;
bmOptions.inSampleSize = 4;
bmOptions.inPurgeable = true;
Bitmap bitmap = BitmapFactory.decodeFile(filePaths.get(position), bmOptions);
// Display Image
imageView.setImageBitmap(bitmap);
((ViewPager) container).addView(imageView, 0);
return imageView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object imageView) {
((ViewPager) container).removeView((ImageView) imageView);
}
}
}
As #Kuffs is saying, remove the items from filePaths, then notify the adapter about that the underlying data has changed:
filePaths.remove(...); // Remove whatever you want
adapter.notifyDatasetChanged(); // Tell the adapter that it's time to update views
I am posting an answer because I cannot comment.
You can try removing it from the adapter. Add this to your destroyItem:
if(position == conditionToRemove){
filePaths.remove(position);
adapter.notifyDatasetChanged()
}
And, you'll have to make ImagePagerAdapter a global variable.
Hope this helps!
i Want to make a slideshow with viewpager where pages are changed automatically every 5 sec. Ive done the viewpager , but got blocked till here , any advise??
Adapter.class (its inner class in my fragment)
class CustomPagerAdapter extends PagerAdapter {
Context mContext;
LayoutInflater mLayoutInflater;
public CustomPagerAdapter(Context context) {
mContext = context;
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.slide_show_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);
}
}
}
Here is the function i call in my fragment
public void setSlideShow(){
mCustomPagerAdapter = new CustomPagerAdapter(getActivity());
mViewPager = (ViewPager)rootView.findViewById(R.id.slide_show);
mViewPager.setAdapter(mCustomPagerAdapter);
}
Well, android does not provide any function to automatically flip views inside ViewPager but it provides you with something called TimerTask class which you can use to achieve your goal.
You can look at this link to do it this way.
There is one more easy way to achieve this but for that you will have to drop the idea of using ViewPager. Instead you can use ViewFlipper.
ViewFlipper has its own methods to flip automatically. You just need to add these two lines to make your views automatically flippable:
mViewFlipper.setAutoStart(true);
mViewFlipper.setFlipInterval(2000); // flip every 2 seconds (2000ms)
You can get ViewFlipper example here.
I am using this code for showing between bunch of images.I want to show all images one by one through swipe to left or right.Can anyone help me how is it possible to make a round swipe images, it means that you be able to see the first image again after reaching the last one in the list?
and how is it possible to put arrows or part of other images in the current page just to give an ides to the user which needs to swipe to left or right for changing the current picture.
And the last question is that how am I able to make each image clickable in order to user goes to another activity page after clicking on an image? I put the function "public void onClick(View arg0) " in the adopter to go to another activity page after clicking on the images but it does not work.
thanks for your help in advance.
public class DestinationActivity extends ActionBarActivity implements View.OnClickListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.destination);
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
DestinationAdapter adapter = new DestinationAdapter(this);
viewPager.setAdapter(adapter);
}
The adapter class :
public class DestinationAdapter extends PagerAdapter {
Context context;
private int[] GalImages = new int[] {
R.drawable.ufficio_204,
R.drawable.ufficio_203,
R.drawable.ufficio_202,
R.drawable.ufficio_201
};
DestinationAdapter(Context context){
this.context=context;
}
#Override
public int getCount() {
return GalImages.length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = new ImageView(context);
int padding = context.getResources().getDimensionPixelSize(R.dimen.padding_medium);
imageView.setPadding(padding, padding, padding, padding);
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
imageView.setImageResource(GalImages[position]);
((ViewPager) container).addView(imageView, 0);
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
if (arg0.getId() == R.id.destination) {
Intent intent = new Intent(context, DetailDestinationActivity.class);
context.startActivity(intent);
}
}
});
return imageView;
}
}
Try to use a View Flipper, follow this tutorial to do that
Viee Flipper
I have ViewPager and FragmentPagerAdapter and I want to show a big amount of images with good quality. I have custom FragmentPagerAdapter and I put into it array with paths to images and then i am creating fragment for every image. The problem is in that when I have a lot of images scrolling between ViewPager items becomes too slow. What are the ways to make fast scrolling?
Resize images in a separate thread?
Use FragmentStatePagerAdapter?
Something else?
You can use PagerAdapter override destroyItem it works well for huge images:
private class ImagePagerAdapter extends PagerAdapter{
#Override
public int getCount() {
return mPhotos.size();
}
#Override
public boolean isViewFromObject(View view, Object obj) {
return obj.equals(view);
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager)container).removeView((View)object);
}
#Override
public Object instantiateItem(ViewGroup view, int position) {
View imageLayout = getLayoutInflater().inflate(R.layout.item_pager_image, view, false);
ImageView imageView = (ImageView) imageLayout.findViewById(R.id.image);
// set up imageView
view.addView(imageLayout, 0);
return imageLayout;
}
}