Viewpager不显示连续图像

时间:2014-10-11 13:55:25

标签: java android android-viewpager

我在更新缩放方法in this discussion之后遇到了Viewpager的问题,也就是说,viewpager只显示点击的图片,如果我滑动到下一张图片,则不会显示,I不知道为什么会这样。

这些是代码:

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);
    }
}



ImageViewTouchViewPager.java

public class ImageViewTouchViewPager extends ViewPager {

    private static final String TAG = "ImageViewTouchViewPager";
    public static final String VIEW_PAGER_OBJECT_TAG = "image#";

    private int previousPosition;

    private OnPageSelectedListener onPageSelectedListener;

    public ImageViewTouchViewPager(Context context) {
        super(context);
        init();
    }

    public ImageViewTouchViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public void setOnPageSelectedListener(OnPageSelectedListener listener) {
        onPageSelectedListener = listener;
    }

    @Override
    protected boolean canScroll(View v, boolean checkV, int dx, int x, int y) {
        if (v instanceof ImageViewTouch) {
            return ((ImageViewTouch) v).canScroll(dx);
        } else {
            return super.canScroll(v, checkV, dx, x, y);
        }
    }

    public interface OnPageSelectedListener {

        public void onPageSelected(int position);

    }

    private void init() {
        previousPosition = getCurrentItem();

        setOnPageChangeListener(new SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                if (onPageSelectedListener != null) {
                    onPageSelectedListener.onPageSelected(position);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                if (state == SCROLL_STATE_SETTLING && previousPosition != getCurrentItem()) {
                    try {
                        ImageViewTouch imageViewTouch = (ImageViewTouch)
                                findViewWithTag(VIEW_PAGER_OBJECT_TAG + getCurrentItem());
                        if (imageViewTouch != null) {
                            imageViewTouch.zoomTo(1f, 300);
                        }

                        previousPosition = getCurrentItem();
                    } catch (ClassCastException ex) {
                        Log.e(TAG, "This view pager should have only ImageViewTouch as a children.", ex);
                    }
                }
            }
        });
    }
}




现在,如果我把这段代码:

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

在for循环中,然后我点击了一个不在索引0处的图像,我轻扫了这个错误:

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)

并且viewpager_adapter的第40行是:

container.addView(imageViewTouch);


相反,如果我将其保留在for循环中,我没有出现错误,但是viewpager只显示点击的图像,如果我滑动到下一个图像,则不会显示。
在此先感谢,如果您需要其他代码,请

0 个答案:

没有答案