Home » Android » android – Why is the first view in ViewPager appearing empty?

android – Why is the first view in ViewPager appearing empty?

Posted by: admin June 15, 2020 Leave a comment

Questions:

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();
}
How to&Answers:

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

Answer:

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..

Answer:

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

Answer:

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 🙂