9

I have a viewpager using the following adapter and after it's loaded the first view appears empty but it's not. If I scroll to the next view and then scroll back I can see the content.

import android.content.Context;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;
import java.util.ArrayList;

public class MyPagerAdapter extends PagerAdapter {

    Context context;

    public ArrayList<TextView> mViews;

    int layoutResourceId;

    public MyPagerAdapter(Context context) {
        mViews = new ArrayList<TextView>();

    }

    @Override
    public void destroyItem(View view, int arg1, Object object) {
        ((ViewPager)view).removeView((View)object);
    }

    @Override
    public void finishUpdate(View arg0) {

    }

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

    @Override
    public Object instantiateItem(View view, int position) {
        View myView = mViews.get(position);
        ((ViewPager)view).addView(myView);
        return myView;
    }

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

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

    }

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

    @Override
    public void startUpdate(View arg0) {

    }

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }

}

And here is the code that implements it from my activity...

public void resetPages() {
    if (mPageText == null)
        return;
    mPagerAdapter.mViews.clear();
    for (int i = 0; i < mPageText.size(); i++) {
        TextView tv = new TextView(this);
        tv.setTextSize(mSelectedFontSize);
        tv.setText(mPageText.get(i));
        tv.setId(i);
        tv.setTag(i);
        tv.invalidate();
        mPagerAdapter.mViews.add(tv);
    }

mPagerAdapter.notifyDataSetChanged();
mViewPager.refreshDrawableState();
}
Josh
  • 123
  • 6
  • what is the `mResizeTextView` ? – Kurosawa Hiroyuki Sep 12 '12 at 01:31
  • mResizeTextView is a custom textview placed used to calculate how much text will fit in the given space. I have a long string that I need to display accross multiple pages. It's currently only visible for debugging puposes. – Josh Sep 12 '12 at 02:06
  • I have removed mResizeTextView line from the above code. It isnt necesary to change it's visibility to gone.. Still having the same issue. – Josh Sep 12 '12 at 16:24
  • I did not realize that the project was using an old support library. I updated to v 13 and I am still unable to see the first view until i scroll away from it and then scroll back to it. – Josh Sep 12 '12 at 20:57
  • Anyone ever find a solution for this? Its driving me absolutely NUTS!!! – Chris Apr 16 '14 at 12:54
  • @Tiago - are you using dynamic height based viewpager? – Govind Nov 23 '16 at 09:35
  • @Chris - are you using dynamic height viewpager? – Govind Nov 23 '16 at 09:36

4 Answers4

0

tried adding followind onCreate method and my code works ("TEXT 0" is appeared at launch). maybe its only because the resetPages() is called at the inappropreate timing?

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mPageText = new LinkedList<CharSequence>();
    for (int i=0; i<10; i++) {
        mPageText.add("TEXT "+i);
    }
    this.mPagerAdapter = new MyPagerAdapter(this);
    this.mViewPager = (ViewPager)this.findViewById(R.id.viewpager);
    this.mViewPager.setAdapter(mPagerAdapter);
    resetPages();
}
Kurosawa Hiroyuki
  • 1,217
  • 2
  • 14
  • 22
0

I'm not sure about this but if you use ViewGroup for your view in instantiateItem then it might work i used viewgroup and its working fine.

@Override
public Object instantiateItem(View view, int position) {
    View myView = mViews.get(position);
    ((ViewPager)view).addView(myView);
    return myView;
}

hope my answer helps..

Ari
  • 1,296
  • 1
  • 18
  • 36
0

The issue must be because view is taking time to load content and view is not updated after content is being loaded.Try adding

 handler.postDelayed(r=new Runnable()
        {
            @Override
            public void run() {
                if (mPosition == position)
                {

                    notifyDataSetChanged();
                   handler.removeCallbacks(r);


                }
            }
        }, 1000);

inside instantiateItem of adapter so that view is updated after 1 second when it is being created.mPosition is the postion of the first view that is being opened.If you are opening viewpager by clicking on item in list then you need to pass the postion of the item that is being selected when calling adapter..else u can set mPostion as 0 if the first item is always initially opened in viewpager

Android Developer
  • 9,157
  • 18
  • 82
  • 139
0

Well I just faced a problem somewhat like this a few mins ago.

The problem in my case was simply with the image. I re-arranged the order of the slideshow and I found that that particular image was still blank. So just replace the image, worked for me :)

  • also there are libraries that are used to carry out these transitions to increase its speed, try using picas/picasa / Glider to ease the process – Justin Joseph Aug 02 '19 at 11:19
  • You can [edit] your answer instead of writing additional info in comment – barbsan Aug 02 '19 at 11:43