2

Here is my simple PagerAdapter and onCreate() of activity for paging.

public class MPagerAdapter extends PagerAdapter{

    List<ViewGroup> pages = null;

    public MPagerAdapter(List<ViewGroup> pages){
        this.pages = pages;
    }

    @Override
    public Object instantiateItem(ViewGroup collection, int position){
        ViewGroup v = pages.get(position);
        ((ViewPager) collection).addView(v, 0);
        return v;
    }

    @Override
    public void destroyItem(ViewGroup collection, int position, Object view){
        ((ViewPager) collection).removeView((View) view);
    }

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

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

    @Override
    public void finishUpdate(View arg0){
    }

    @Override
    public void restoreState(Parcelable arg0, ClassLoader arg1){
    }

    @Override
    public Parcelable saveState(){
        return null;
    }

    @Override
    public void startUpdate(View arg0){
    }
}

and here is onCreate() of activity in which pager will live.

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_pager);
    getActionBar().setDisplayHomeAsUpEnabled(true);

    Intent intent = getIntent();
    String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
    amount=Integer.parseInt(message);

    Resources res = getResources();
    Drawable background =  res.getDrawable(R.drawable.page_back1);

    BackgroudImage bImage = new BackgroudImage(x, y, background);

    List<MyLayout> pages = new ArrayList<MyLayout>();
    int a = amount/bImage.mPointsList.size();
    for (int i=0; i<=a; i++){
        MyLayout page = new MyLayout(getBaseContext(), bImage);     
        page.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        pages.add(page);
    }
    MPagerAdapter pagerAdapter = new MPagerAdapter(pages);
    ViewPager viewPager = (ViewPager)findViewById(R.id.pager);//new ViewPager(this);
    viewPager.setAdapter(pagerAdapter);
    viewPager.setCurrentItem(1); 
}

OnLayout() method of my custom layout which is used as a control for pages of pagerView

protected void onLayout(boolean changed, int l, int t, int r, int b) {
    if (PagerActivity.amount<=points.size()) {

        for (int i=0; i<PagerActivity.amount; i++){
            ImageView childV = new ImageView(super.getContext());
            childV.setBackgroundResource(R.drawable.lesson_frame_closed);
            ViewGroup.LayoutParams imageParams = new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            childV.setOnClickListener(new View.OnClickListener() {

                  @Override
                  public void onClick(View view) {

                  }

                });
            this.addView(childV, imageParams);
        }
        PagerActivity.amount=0;
    } 

    if ((float)(bgrImage.getBackground().getIntrinsicWidth())/(float)(bgrImage.getBackground().getIntrinsicHeight())<(float)this.getMeasuredWidth()/(float)this.getMeasuredHeight()) {
        scaleFX=(float)this.getMeasuredHeight()/(float)bgrImage.getBackground().getIntrinsicWidth();
        scaleFY=(float)this.getMeasuredHeight()/(float)bgrImage.getBackground().getIntrinsicHeight();
    } else {
        scaleFX=(float)this.getMeasuredWidth()/(float)bgrImage.getBackground().getIntrinsicWidth();
        scaleFY=(float)this.getMeasuredWidth()/(float)bgrImage.getBackground().getIntrinsicHeight();
    }

    for (int i = 0; i < getChildCount(); i++) {
        final View childV = getChildAt(i);
        if (childV.getVisibility() != View.VISIBLE)
        continue;
        childV.layout((int)(points.get(i).getOriginX()*scaleFX), (int)(points.get(i).getOriginY()*scaleFY), childV.getWidth(), childV.getHeight());
    }

}

Can't understand why InstantiateItem() isn't being called. Any helpful idea will be appreciated.

Factory Girl
  • 910
  • 4
  • 18
  • 29

1 Answers1

0

The ViewPager is a View and should be added to the UI. typically by declaring it in a layout file.

Extracted from the SDK samples :

  • the layout file :

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_horizontal"
        android:orientation="vertical"
        android:padding="4dip" >
    
        <android.support.v4.view.ViewPager
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="0px"
            android:layout_weight="1" >
        </android.support.v4.view.ViewPager>
        ...
    
  • the activity class

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_pager);
    
        mAdapter = new MyAdapter(getSupportFragmentManager());
    
        mPager = (ViewPager)findViewById(R.id.pager);
        mPager.setAdapter(mAdapter);
        ...
    

The complete sources are in the extras/android/support/samples/Support4Demos directory of the SDK

bwt
  • 17,292
  • 1
  • 42
  • 60
  • ViewPager is a list of views. It musn't be declared in layout file, pages of pager (Views) must be declared and they are declared in my project. But i can't understand why InstantiateItem isn't being called – Factory Girl Jan 08 '13 at 11:30
  • The `ViewPager` should be declared, **not** the pages : the viewpager ask the adapter to create them when needed. Basically it works like a `ListView` : you declare the `ListView` wich in turn delegate the child creation to the adapter. – bwt Jan 08 '13 at 12:16
  • There are examples in the Support4Demos sample project from the SDK. – bwt Jan 08 '13 at 12:29
  • `ViewPager viewPager = new ViewPager(this); viewPager.setAdapter(pagerAdapter); ` if you see this way i declare ViewPager in my code and set an adapter – Factory Girl Jan 08 '13 at 12:51
  • ok, but if the `ViewPager` is not linked to the UI it has no way to know when to call the adapter. You can do it manually but the standard way is to declare it in a layout. I added an example. Hope this will help you. – bwt Jan 08 '13 at 13:09
  • It works. InstantiateItem is being called when needed. But now i have another problem. Nothing is being displayed. There should be displayed background for each page of ViewPager and other views on the pages. But it is nothing on the display. Could you take another look to editted code? – Factory Girl Jan 10 '13 at 06:32