在android中创建一个viewpager图库

时间:2015-03-10 01:43:38

标签: android android-layout android-viewpager android-linearlayout android-imageview

我正在创建一个简单的viewpager图库,有一个viewpager,每个页面都有一个触摸图像视图,可以缩放和平移图像。

我已成功在其上显示图像。问题是,当我交换图片时,缩放级别保留。例如。当我放大第一页时,我尝试换到第二页,然后返回第一页,缩放级别不会恢复为默认值。

private 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 show_list.size();
    }

    @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.activity_zoom_item, container, false);

        final TouchImageView imageView = (TouchImageView) itemView.findViewById(R.id.img);

        if (show_list.get(position).contains("http://")) {
            ImageRequest ir = new ImageRequest(show_list.get(position),
                    new Response.Listener<Bitmap>() {
                        @Override
                        public void onResponse(Bitmap response) {
                            imageView.setImageBitmap(response);
                            imageView.resetZoom();
                        }
                    }, 0, 0, null, null);
            gs.addToRequestQueue(ir, "get_img");
        }

        container.addView(itemView);
        return itemView;
    }



    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((LinearLayout) object);
    }
}

触摸图像视图本身具有重置缩放级别的功能,唯一的问题是如何在“页面更改时”获取状态并获取该页面的视图?

由于

1 个答案:

答案 0 :(得分:0)

pager.setOnPageChangeListener(new OnPageChangeListener(){

                @Override
                public void onPageScrollStateChanged(int arg0) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onPageScrolled(int arg0, float arg1, int arg2) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onPageSelected(int arg0) {
                    // TODO Auto-generated method stub
                    View rootview = pager.findViewWithTag(pager.getCurrentItem());
                    TouchImageView imageView = (TouchImageView) rootview.findViewById(R.id.img);
                    imageView.resetZoom();
                }

            });

并且

private 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 show_list.size();
    }

    @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.activity_zoom_item, container, false);

        final TouchImageView imageView = (TouchImageView) itemView.findViewById(R.id.img);

        if (show_list.get(position).contains("http://")) {
            ImageRequest ir = new ImageRequest(show_list.get(position),
                    new Response.Listener<Bitmap>() {
                        @Override
                        public void onResponse(Bitmap response) {
                            imageView.setImageBitmap(response);
                        }
                    }, 0, 0, null, null);
            gs.addToRequestQueue(ir, "get_img");
        }

        itemView.setTag(position);
        container.addView(itemView);
        return itemView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((LinearLayout) object);
    }
}

注意需要使用set tag来获取onPageSelected的视图,感谢ρяσѕρєяK帮助。