如何引用回创建的viewPager以动态添加更多imageViews

时间:2014-01-07 09:20:37

标签: java android dynamic android-viewpager

我有一个PagerAdapter,它显示从MainActivity类传递的字符串数组中收集的图像。我想这样做,以便将来可以引用此PagerAdapter,以动态创建更多视图。我很好奇这是怎么做到的?我的PagerAdapter代码(转换后的 ArrayList是从MainActivity类传递的代码):

    public class ViewPagerAdapter extends PagerAdapter {
    // Declare Variables
    Context context;
    ArrayList<String> converted;
    ArrayList<View> views = new ArrayList<View>();
    LayoutInflater inflater;
    public ImageView imgzoomer;
    public ViewPagerAdapter(Context context, ArrayList<String> converted) {
        this.context = context;
        this.converted = converted;


    }
    @Override
    public int getCount() {
        return converted.size();
    }
    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == ((RelativeLayout) object);
    }
    @Override
    public void setPrimaryItem(ViewGroup container, int position, Object object) {
        super.setPrimaryItem(container, position, object);
            if(position==converted.size()-1){
                // Declare Variables and Views
                // Capture position and set to the ImageView
                Bitmap bitmap = BitmapFactory.decodeFile(converted.get(position));
                bitmap.recycle();
                imgzoomer.setImageBitmap(bitmap);

                // Add layout_fullscreen_image.xml to ViewPager
                container.removeAllViewsInLayout();
                container.removeAllViews();
                container.addView(container, position+1);
                notifyDataSetChanged();
            }
    }
    @Override
    public Object instantiateItem(ViewGroup container, int position) {

        // Declare Variables and Views
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View itemView = inflater.inflate(R.layout.layout_fullscreen_image, container,false);
        imgzoomer = (ImageView) itemView.findViewById(R.id.zoomer);

        // Capture position and set to the ImageView
        Bitmap bitmap = BitmapFactory.decodeFile(converted.get(position));
        imgzoomer.setImageBitmap(bitmap);

        // Add layout_fullscreen_image.xml to ViewPager
        container.addView(itemView, 0);
        return itemView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        // Remove viewpager_item.xml from ViewPager
        container.removeView(imgzoomer);
    }
}

我想在MainActivityClass中再次引用ViewPager的部分:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //get the view
        setContentView(R.layout.viewpager_main);
        ImageView imgzoomer;
        //Doinbackground ASYNCTASK (returns the converted arraylist, which is the location of the pics in the storage)
        new XMLParse().execute(url);

        //Locate ViewPager
        viewPager = (ViewPager) findViewById(R.id.pager);

        //PageChangeListener
        viewPager.setOnPageChangeListener(new OnPageChangeListener() {
            @Override

                            //THIS PART HERE<------------

            public void onPageScrolled(int position, float v, int i2) {
                if(position>=converted.size()-1){
                }
            }

            @Override
            public void onPageSelected(int position) {
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
    }

0 个答案:

没有答案