I am developing a simple gallery app .
I have 2 problems :
First problem :
When I touch a specific image in my GridView to show in the fullscreen activity (viewpager activity) it always shows the first image in the GridView gallery!!! not the one I touched!!!
But ViewPager work correctly .
Second problem:
I want to zoom in the ViewPager activity
I tried a library (chrisbanes:phtoview) but it has some issues with ViewPager
Summary :
I want my gallery app to work normal. GridView gallery (you can swipe and pinch to zoom images)
MainActivity.java
public class MainActivity extends AppCompatActivity {
private ViewPager slider;
private SliderAdapter sliderAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
slider=findViewById(R.id.slider);
sliderAdapter=new SliderAdapter(this);
slider.setAdapter(sliderAdapter);
}
private class SliderAdapter extends PagerAdapter{
private Context context;
private LayoutInflater inflater;
public SliderAdapter(Context context) {
this.context = context;
}
int[] list_img={R.drawable.ferari,R.drawable.ferrari,R.drawable.ferrarilogoo,R.drawable.yellowferrariiii,R.drawable.models,R.drawable.applogo,R.drawable.carfront,R.drawable.california,R.drawable.laferrari
};
#Override
public int getCount() {
return list_img.length;
}
#Override
public boolean isViewFromObject(#NonNull View view, #NonNull Object object) {
return view==object;
}
#NonNull
#Override
public Object instantiateItem(#NonNull ViewGroup container, final int position) {
inflater=(LayoutInflater)context.getSystemService(LAYOUT_INFLATER_SERVICE);
View view=inflater.inflate(R.layout.item_slider,container,false);
ImageView imageView=view.findViewById(R.id.img_item);
imageView.setImageResource(list_img[position]);
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent=new Intent(context,Fullscreen.class);
intent.putExtra("Ferrari",position);
startActivity(intent);
}
});
container.addView(view);
return view;
}
#Override
public void destroyItem(#NonNull ViewGroup container, int position, #NonNull Object object) {
container.removeView((ImageView)object);
}
}
}
ImageAdapter.java
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
// if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new ViewGroup.LayoutParams(230, 250));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(3, 3, 3, 3);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// references to our images
public Integer[] mThumbIds = {
R.drawable.ferari, R.drawable.models,
R.drawable.ferrarihd,
R.drawable.ferrari,
R.drawable.yellowferrariiii,
R.drawable.models,
R.drawable.home,
R.drawable.carfront,
R.drawable.villa,
R.drawable.derrarii,
R.drawable.laferrari,
R.drawable.realestate,
R.drawable.carfront,R.drawable.california,R.drawable.laferrari
};
}
FullscreenviewActivity.java
public class FullScreenViewActivity extends AppCompatActivity {
private ViewPager slider;
private SliderAdapter sliderAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_full_screen_view);
slider=findViewById(R.id.slider);
sliderAdapter=new SliderAdapter(this);
slider.setAdapter(sliderAdapter);
slider.setPageTransformer(false, new ViewPager.PageTransformer() {
#Override
public void transformPage(View page, float position) {
// do transformation here
final float normalizedposition = Math.abs(Math.abs(position) - 1);
page.setScaleX(normalizedposition / 2 + 0.5f);
page.setScaleY(normalizedposition / 2 + 0.5f);
//or
// page.setRotationY(position * -30);
}
});
// get intent data
Intent i = getIntent();
// Selected image id
int position = i.getExtras().getInt("id");
ImageAdapter gridViewAdapter = new ImageAdapter(this);
gridViewAdapter.getItemId(position);
}
private class SliderAdapter extends PagerAdapter {
private Context context;
private LayoutInflater inflater;
public SliderAdapter(Context context) {
this.context = context;
}
int[] mThumbIds={ R.drawable.ferari, R.drawable.models,
R.drawable.ferrarihd,
R.drawable.ferrari,
R.drawable.yellowferrariiii,
R.drawable.models,
R.drawable.home,
R.drawable.carfront,
R.drawable.villa,
R.drawable.derrarii,
R.drawable.laferrari,
R.drawable.realestate,
R.drawable.carfront,R.drawable.california,R.drawable.laferrari
};
#Override
public CharSequence getPageTitle (int position) {
return "Ferrari Cars";
}
#Override
public int getCount() {
return mThumbIds.length;
}
#Override
public boolean isViewFromObject(#NonNull View view, #NonNull Object object) {
return view==object;
}
#NonNull
#Override
public Object instantiateItem(#NonNull ViewGroup container, final int position) {
inflater=(LayoutInflater)context.getSystemService(LAYOUT_INFLATER_SERVICE);
View view=inflater.inflate(R.layout.item_slider,container,false);
ImageView imageView=view.findViewById(R.id.img_item);
imageView.setImageResource(mThumbIds[position]);
container.addView(view);
return view;
}
#Override
public void destroyItem(#NonNull ViewGroup container, int position, #NonNull Object object) {
container.removeView((ImageView)object);
}
}
}
Firstly you have the position of image in FullScreenActivity so only do one thing to scroll that image
slider.setCurrentItem(pos);
After
int position = i.getExtras().getInt("id");
And use TouchImageView for zoom
Related
I've made a image slider from viewPager. The image shown in viewPager is stored in raw folder inside the project of android studio.
I want to request for the permission of user to download image, display progress bar when image is downloading and finally a tost message when image is downloaded.
Things I've done:
-I have made a imageview button to download on the viewpager.
-Progressbar with android:visibility="invisible"
-My code in MainActivty where image and download option are shown.
public class MainActivity extends AppCompatActivity {
ViewPager viewPager;
ProgressBar progressBar;
private int[] imageUrls = new int[]{
R.drawable.after_cookie,
R.drawable.before_cookie,
R.drawable.androidparty
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = findViewById(R.id.progress);
viewPager = findViewById(R.id.view_pager);
final ViewPageAdapter adapter = new ViewPageAdapter(this, imageUrls);
viewPager.setAdapter(adapter);
ImageView btnDownload = findViewById(R.id.btnDownload);
btnDownload.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//what should I do? please help :(
}
});
}
}
code of viewpager adapter:
public class ViewPageAdapter extends PagerAdapter {
private Context context;
private int[] imageUrls;
public View currentImageView;
ViewPageAdapter(Context context, int[] imageUrls) {
this.context = context;
this.imageUrls = imageUrls;
}
#Override
public int getCount() {
return imageUrls.length;
}
#Override
public boolean isViewFromObject(#NonNull View view, #NonNull Object object) {
return view == object;
}
#NonNull
#Override
public Object instantiateItem(#NonNull ViewGroup container, int position) {
ImageView view = new ImageView(context);
Picasso.get()
.load(imageUrls[position])
.into(view);
container.addView(view);
return view;
}
#Override
public void destroyItem(#NonNull ViewGroup container, int position, #NonNull Object object) {
container.removeView((View) object);
}
#Override
public void setPrimaryItem(ViewGroup container, int position, Object object) {
currentImageView = (View)object;
}
}
I followed this tutorial to make an image slider with images url but the slider is empty.
Here is my adapter's code:
public class ViewPagerAdapterUrl extends PagerAdapter {
private Context context;
private LayoutInflater layoutInflater;
private ArrayList<String> photosUrl= new ArrayList<>();
public ViewPagerAdapterUrl(Context context, ArrayList<String> photosUrl){
this.context = context;
this.photosUrl = photosUrl;
}
#Override
public int getCount() {
return photosUrl.size();
}
#Override
public boolean isViewFromObject(#NonNull View view, #NonNull Object object) {
return view == object;
}
#Override
public Object instantiateItem(#NonNull ViewGroup container, int position) {
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.viewpager_layout, null);
ImageView imageView = (ImageView)view.findViewById(R.id.imageView);
Picasso.get().load(photosUrl.get(position)).into(imageView);
container.addView(imageView);
return imageView;
}
#Override
public void destroyItem(#NonNull ViewGroup container, int position, #NonNull Object object) {
container.removeView((View)object);
}
}
Here is the activity where i want to show the adapter:
public class UrlActivity extends AppCompatActivity {
private Advertisement adv;
private ViewPager viewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_url);
viewPager = (ViewPager)findViewById(R.id.viewPager);
//adv is a custom object that contain an arraylist<String> of url.
adv = (Advertisement)getIntent().getSerializableExtra("adv");
ViewPagerAdapterUrl viewPagerAdapterUrl = new ViewPagerAdapterUrl(this, adv.getPhotosUrl());
viewPager.setAdapter(viewPagerAdapterUrl);
}
}
EDIT
I found this in debug but it isn't show like an error:
method int android.support.v7.widget.DropDownListView.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
I have image slider in android studio, but images array from inside project.
I want to use images from JSON, how to the best solution..
this is my adapter
public class adapter_image_slider extends PagerAdapter {
Context mContext;
public adapter_image_slider(Context context) {
this.mContext = context;
}
#Override
public int getCount() {
return sliderImagesId.length;
}
private int[] sliderImagesId = new int[]{
R.drawable.imghome1, R.drawable.imghome2, R.drawable.imghome3,
};
#Override
public boolean isViewFromObject(View v, Object obj) {
return v == ((ImageView) obj);
}
#Override
public Object instantiateItem(ViewGroup container, int i) {
ImageView mImageView = new ImageView(mContext);
mImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
mImageView.setImageResource(sliderImagesId[i]);
((ViewPager) container).addView(mImageView, 0);
return mImageView;
}
#Override
public void destroyItem(ViewGroup container, int i, Object obj) {
((ViewPager) container).removeView((ImageView) obj);
}
}
and this is my main activity :
public class activity_img_slider_json extends Fragment {
private static ViewPager mPager;
private static int currentPage = 0;
private static final Integer[] XMEN= {R.drawable.imghome1,R.drawable.imghome2,R.drawable.imghome3,};
private ArrayList<Integer> XMENArray = new ArrayList<Integer>();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.activity_img_slider_json,null);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
super.onCreate(savedInstanceState);
init();
}
private void init() {
for(int i=0;i<XMEN.length;i++)
XMENArray.add(XMEN[i]);
mPager = (ViewPager)getActivity().findViewById(R.id.pager);
mPager.setAdapter(new adapter_img_slider_json(getActivity(), XMENArray));
CircleIndicator indicator = (CircleIndicator)getActivity().findViewById(R.id.indicator);
indicator.setViewPager(mPager);
// Auto start of viewpager
final Handler handler = new Handler();
final Runnable Update = new Runnable() {
public void run() {
if (currentPage == XMEN.length) {
currentPage = 0;
}
mPager.setCurrentItem(currentPage++, true);
}
};
}
}
Thanks For Attention.
I have a pageradapter that i am using to display a slideshow of images but it crashes when i set a lower off screen limit.
Here is my code
public class ProjectPicturesAdapter extends PagerAdapter {
private List<String> pictures;
private LayoutInflater inflater;
private final RequestManager glide;
public ProjectPicturesAdapter(RequestManager glide, LayoutInflater inflater, List<String> pictures) {
this.pictures = pictures;
this.glide = glide;
this.inflater = inflater;
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
View image = inflater.inflate(R.layout.slider_image, container, false);
ImageView myImage = (ImageView) image.findViewById(R.id.image);
glide.load(pictures.get(position))
.centerCrop()
.crossFade()
.placeholder(R.drawable.project_placeholder)
.into(myImage);
container.addView(myImage);
return myImage;
}
#Override
public void destroyItem(ViewGroup container, int position, Object view) {
container.removeView((View) view);
}
#Override
public int getCount() {
return pictures.size();
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view.equals(object);
}
}
The way i am initializing the adapter is this
ProjectPicturesAdapter picturesAdapter = new
ProjectPicturesAdapter(Glide.with(this), LayoutInflater.from(this),
images);
ViewPager pager = (ViewPager) findViewById(R.id.viewPager);
pager.setAdapter(picturesAdapter);
pager.setOffscreenPageLimit(1);
If there are more than 2 images then it crashes,i could always increase the off screen limit but what happens if there are 20 pictures,i cannot keep the offscreen limit as 20.
My crash log is gives an error saying The specified child already has a parent. You must call removeView() on the child's parent first.
Update ProjectPicturesAdapter as below:
public class ProjectPicturesAdapter extends PagerAdapter {
private List<String> pictures;
private LayoutInflater inflater;
private final RequestManager glide;
public ProjectPicturesAdapter(RequestManager glide, LayoutInflater inflater, List<String> pictures) {
this.pictures = pictures;
this.glide = glide;
this.inflater = inflater;
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
ViewGroup view = (ViewGroup) inflater.inflate(R.layout.slider_image, container, false);
ImageView myImage = (ImageView) view.findViewById(R.id.image);
glide.load(pictures.get(position))
.centerCrop()
.crossFade()
.placeholder(R.drawable.project_placeholder)
.into(myImage);
container.addView(view);
return view;
}
#Override
public void destroyItem(ViewGroup container, int position, Object view) {
container.removeView((View) view);
}
#Override
public int getCount() {
return pictures.size();
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
}
If your aim is to show the carousel?
Just use this library and your problem will be solved https://github.com/sayyam/carouselview
The problem is caused by this:
#Override
public void destroyItem(ViewGroup container, int position, Object view)
{
container.removeView((View) view);
}
I recommend to use FragmentStatePagerAdapter instead of PagerAdapter.
public static class MyAdapter extends FragmentStatePagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
#Override
public int getCount() {
return NUM_ITEMS;
}
#Override
public Fragment getItem(int position) {
return ArrayListFragment.newInstance(position);
}
}
mAdapter = new MyAdapter(getFragmentManager());
mPager = (ViewPager)findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
I am trying to use this example on the official android tutorial site and translating it into a gridview inside a fragment that uses picasso to load images.
I am looking at my code and I can't figure out what is wrong. The images simply won't load and all I get is a blank screen. What am I missing here?
My fragment code:
public class MainFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main, container, false);
GridView gridView = (GridView) view.findViewById(R.id.gridview);
gridView.setAdapter(new ImageAdapter(getActivity().getApplicationContext()));
return view;
}
}
My custom adapter:
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c){
mContext = c;
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
ImageView imageView;
if(view == null){
imageView = new ImageView(mContext);
}
else{
imageView = (ImageView) view;
}
Picasso.with(mContext).setLoggingEnabled(true);
Picasso.with(mContext)
.load(eatFoodyImages[i])
.fit()
.into(imageView);
return imageView;
}
#Override
public int getCount() {
return 0;
}
public static String[] eatFoodyImages = {
"http://i.imgur.com/rFLNqWI.jpg",
"http://i.imgur.com/C9pBVt7.jpg",
"http://i.imgur.com/rT5vXE1.jpg",
"http://i.imgur.com/aIy5R2k.jpg",
"http://i.imgur.com/MoJs9pT.jpg",
"http://i.imgur.com/S963yEM.jpg",
"http://i.imgur.com/rLR2cyc.jpg",
"http://i.imgur.com/SEPdUIx.jpg",
"http://i.imgur.com/aC9OjaM.jpg",
"http://i.imgur.com/76Jfv9b.jpg",
"http://i.imgur.com/fUX7EIB.jpg",
"http://i.imgur.com/syELajx.jpg",
"http://i.imgur.com/COzBnru.jpg",
"http://i.imgur.com/Z3QjilA.jpg",
};
}
My activity:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(savedInstanceState == null){
getSupportFragmentManager()
.beginTransaction().add(R.id.mainfragment, new MainFragment())
.commit();
}
}
}
#Override
public int getCount() {
return eatFoodyImages.length;
}