Android : Viewpager show only clicked image and not the successive images

on Saturday, October 11, 2014


I've a trouble with Viewpager, after update zoom method in this discussion, that is, the viewpager shows only the image clicked and if I swipe to the next image this doesn't appear, I don't know why this happens.


These are the codes:



Viewpager_layout.xml



<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="#00A600"
android:id="@+id/layoutesterno">

<com.ruar.imagegallery.app.ImageViewTouchViewPager
android:id="@+id/imagepager"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@+id/relativeLayout" />

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_gravity="bottom"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/relativeLayout"
android:background="#008A00">

<ImageButton
android:layout_width="60dp"
android:layout_height="fill_parent"
android:id="@+id/saveBtn"
android:background="@drawable/save"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<ImageButton
android:layout_width="60dp"
android:layout_height="62dp"
android:id="@+id/shareBtn"
android:background="@drawable/share"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_centerInParent="true"
android:layout_alignTop="@+id/saveBtn"/>

<ImageButton
android:layout_width="70dp"
android:layout_height="62dp"
android:id="@+id/setasBtn"
android:background="@drawable/setaswallpaper"
android:layout_alignTop="@+id/saveBtn"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />

</RelativeLayout>

<ImageButton
android:layout_width="50dp"
android:layout_height="50dp"
android:id="@+id/info"
android:background="@drawable/info"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />

<ImageButton
android:layout_width="50dp"
android:layout_height="50dp"
android:id="@+id/bugimg"
android:background="@drawable/bug"
android:layout_below="@+id/info"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="20dp"
android:layout_alignLeft="@+id/info"
android:layout_alignStart="@+id/info" />

</RelativeLayout>






Viewpager_activity.java



public class Viewpager_activity extends Activity
{
int position;
int img_id;
Grid_adapter grid_adapter;
int i;
final List<ImageViewTouch> images = new ArrayList<ImageViewTouch>();
public static String[][] galleryeArrayThumb = ImmaginiThumb.thumbs;

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

ImageLoader_Grid imageLoader = new ImageLoader_Grid(this.getApplicationContext());

final Intent intent = getIntent();
img_id = (Integer) intent.getExtras().get("img");
position = (Integer) intent.getExtras().get("id");

grid_adapter = new Grid_adapter(this, galleryArrayThumb[img_id]);

for (i = 0; i < grid_adapter.getCount(); i++)
{
ImageViewTouch imageViewTouch = new ImageViewTouch(getApplicationContext(), null);

Picasso.with(Viewpager_activity.this)
.load(galleryArrayThumb[img_id][i])
.fit()
.centerInside()
.placeholder(R.drawable.loading)
.into(imageViewTouch);

images.add(imageViewTouch);
}

ViewPager viewPager = (ImageViewTouchViewPager) findViewById(R.id.imagepager);
viewPager.setAdapter(new Viewpager_adapter(images));
viewPager.setCurrentItem(position);
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener()
{
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
{
try
{
trimCache(Viewpager_activity.this);
Log.i("VP_cache", "Cache liberata");
}
catch (Exception e)
{
e.printStackTrace();

}
}

@Override
public void onPageSelected(int position1) {
position = position1;
}

@Override
public void onPageScrollStateChanged(int state) {}
});






Viewpager_adapter.java



public class Viewpager_adapter extends PagerAdapter
{
private List<ImageViewTouch> imageViewTouches;

public Viewpager_adapter(List<ImageViewTouch> imageViewTouches)
{
this.imageViewTouches = imageViewTouches;
}

@Override
public int getCount() {
return imageViewTouches.size();
}

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

@Override
public Object instantiateItem(ViewGroup container, int position)
{
ImageViewTouch imageViewTouch = new ImageViewTouch(container.getContext(),null);
imageViewTouch = (ImageViewTouch) imageViewTouches.get(position);
imageViewTouch.setScaleType(ImageViewTouch.ScaleType.MATRIX);

container.addView(imageViewTouch);

return imageViewTouch;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
ImageViewTouchViewPager pager = (ImageViewTouchViewPager) container;
View recycledView = (View) object;
pager.removeView(recycledView);
}
}








Now if I put this code:



ImageViewTouch imageViewTouch = new ImageViewTouch(getApplicationContext(), null);


out of the for loop, then I click an image that isn't at index 0 and i swipe I've this error:



10-11 15:50:47.018 29198-29198/com.ruar.imagegallery.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.ruar.imagegallery.app, PID: 29198
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
at android.view.ViewGroup.addViewInner(ViewGroup.java:3562)
at android.view.ViewGroup.addView(ViewGroup.java:3415)
at android.support.v4.view.ViewPager.addView(ViewPager.java:1309)
at android.view.ViewGroup.addView(ViewGroup.java:3360)
at android.view.ViewGroup.addView(ViewGroup.java:3336)
at com.ruar.imagegallery.app.Viewpager_adapter.instantiateItem(Viewpager_adapter.java:40)
at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:837)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1021)
at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
at android.support.v4.view.ViewPager$3.run(ViewPager.java:249)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
at android.view.Choreographer.doCallbacks(Choreographer.java:574)
at android.view.Choreographer.doFrame(Choreographer.java:543)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)


and the line #40 of viewpager_adapter is this:



container.addView(imageViewTouch);




Instead if I keep that in the for loop, I haven't that error, but the viewpager show only the clicked image, if I swipe to the next image, this doesn't appear.

Thanks in advance, and if you need other codes put them.


0 comments:

Post a Comment