简体   繁体   English

为什么ViewPager中的第一个视图显示为空?

[英]Why is the first view in ViewPager appearing empty?

I have a viewpager using the following adapter and after it's loaded the first view appears empty but it's not. 我有一个使用以下适配器的viewpager,在加载后,第一个视图显示为空,但事实并非如此。 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();
}

tried adding followind onCreate method and my code works ("TEXT 0" is appeared at launch). 尝试添加followind onCreate方法,我的代码工作(“TEXT 0”出现在启动时)。 maybe its only because the resetPages() is called at the inappropreate timing? 也许只是因为在不合适的时机调用resetPages()?

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

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. 我不确定这一点但是如果你在instantiateItem中使用ViewGroup作为你的视图那么它可能工作我使用viewgroup并且它工作正常。

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

hope my answer helps.. 希望我的回答有帮助..

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. 在适配器的instantiateItem内部,以便在instantiateItem视图后1秒后更新视图。 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 mPosition是正在打开的第一个视图的位置。如果您通过单击列表中的项目打开viewpager,则需要在调用适配器时传递正在选择的项目的位置。这样您可以将mPostion设置为0如果第一项始终在viewpager中最初打开

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 :) 所以只需替换图像,为我工作:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM