首先viewpager页空白 [英] First viewpager page blank

查看:158
本文介绍了首先viewpager页空白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个涉及到Android的ViewPager一个问题。目前我正在试图实现一个VerticalViewPager(虽然这是不相关的问题,因为同样的问题与正常ViewPager时)用自定义PagerAdapter,获取的东西从我的数据库并且将其输入到屏幕上。

现在的问题是,我的第一张牌出现,直到我上滑动到下一个页面,然后回来为空白。之后,该内容是存在的。我不能完全肯定发生了什么事情,所以希望有人能阐明这个问题的一些情况。我发现了一个类似的问题在这里: <一href="http://stackoverflow.com/questions/12379942/why-is-the-first-view-in-viewpager-appearing-empty">Why在ViewPager第一个视图显示是空的?  和<一href="http://stackoverflow.com/questions/9525279/activity-using-viewpager-pageradapter-and-asynctask-causes-a-blank-view">Activity使用ViewPager,PagerAdapter和AsyncTask的原因空白视图,但它好像没有在那里能够帮助我。

这是我的自定义PagerAdapter code:

 公共类BeginnerCardCursorPagerAdapter扩展PagerAdapter {
    私人光标指针;
    私人LayoutInflater充气;

    公共BeginnerCardCursorPagerAdapter(上下文的背景下,光标光标){
        this.cursor =光标;
        this.inflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    公共无效swapCursor(光标指针){
        this.cursor =光标;
    }

    @覆盖
    公共无效destroyItem(查看视图,INT位置,Object对象){
        ((VerticalViewPager)视图).removeView((RelativeLayout的)对象);
    }

    @覆盖
    公众诠释getCount将(){
        如果(光标== NULL){
            返回0;
        } 其他 {
            返回90;
        }
    }

    @覆盖
    公共对象instantiateItem(查看视图,INT位置){
        RelativeLayout的布局= NULL;
        INT位置2 =位置;
        如果(位置== 0){
            布局=(RelativeLayout的)inflater.inflate(R.layout.activity_slide_info,NULL);
        }如果(位置== 89){
            布局=(RelativeLayout的)inflater.inflate(R.layout.activity_advertisement_beginner,NULL);
        }
        否则,如果(仓位大于0和放大器;&安培;位置&LT; 89){
            position2--;
            cursor.moveToPosition(位置2);
            布局=(RelativeLayout的)inflater.inflate(R.layout.activity_card,NU​​LL);
            layout.setBackgroundResource(R.drawable.card_beginner);
            TextView的cardTitle =(TextView中)layout.findViewById(R.id.pirate_card_title);
            TextView的cardExample =(TextView中)layout.findViewById(R.id.pirate_card_example);
            TextView的cardDefinition =(TextView中)layout.findViewById(R.id.pirate_card_definition);

            cardTitle.setText(cursor.getString(cursor.getColumnIndex(FlashCardTable.COLUMN_TITLE)));
            cardExample.setText(cursor.getString(cursor.getColumnIndex(FlashCardTable.COLUMN_SENTENCE)));
            cardDefinition.setText(cursor.getString(cursor.getColumnIndex(FlashCardTable.COLUMN_DEFINITION)));
        }
        ((VerticalViewPager)视图).addView(布局);
        返回布局;
    }

    @覆盖
    公共布尔isViewFromObject(查看为arg0,对象ARG1){
        返回将arg0 == ARG1;
    }
}
 

解决方案

这个问题一定是因为卡需要时间来加载从数据库和视图的数据未更新数据正在loaded.Try添加后

  handler.postDelayed(R =新的Runnable()
        {
            @覆盖
            公共无效的run(){
                如果(mPosition ==位置)
                {

                    notifyDataSetChanged();
                   handler.removeCallbacks(r)的;


                }
            }
        },1000);
 

instantiateItem 适配器使视图后,它被创建时1秒更新。 mPosition 是正被opened.If你的第一张牌的现在的位置是通过单击项目列表中打开viewpager那么你需要传递,被选择的呼叫adapter..else时候你设置的项目的现在的位置 mPostion 为0,如果第一个项目总是最初在viewpager打开

I have a problem that relates to Android's ViewPager. I am currently attempting to implement a VerticalViewPager (though that is not relevant to the problem as this same problem occurs with a normal ViewPager) with a custom PagerAdapter that gets stuff from my database and inputs it onto the screen.

The problem is that my first card appears to be blank until I slide it over to the next page and come back. After that the content is there. I'm not exactly sure what was going on so hopefully someone can shed some light on this issue. I found a similar problem here: Why is the first view in ViewPager appearing empty? and Activity using ViewPager, PagerAdapter and AsyncTask causes a blank view but it seems as if nothing in there is able to help me.

This is my custom PagerAdapter code:

public class BeginnerCardCursorPagerAdapter extends PagerAdapter {
    private Cursor cursor;
    private LayoutInflater inflater;

    public BeginnerCardCursorPagerAdapter(Context context, Cursor cursor){
        this.cursor = cursor;
        this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public void swapCursor(Cursor cursor){
        this.cursor = cursor;
    }

    @Override
    public void destroyItem(View view, int position, Object object) {
        ((VerticalViewPager) view).removeView((RelativeLayout) object);
    }

    @Override
    public int getCount() {
        if(cursor == null) {
            return 0;
        } else {
            return 90;
        }
    }

    @Override
    public Object instantiateItem(View view, int position) {
        RelativeLayout layout = null;
        int position2 = position;
        if(position == 0){
            layout = (RelativeLayout) inflater.inflate(R.layout.activity_slide_info, null);
        }if(position == 89){
            layout = (RelativeLayout) inflater.inflate(R.layout.activity_advertisement_beginner, null);
        }
        else if(position > 0 && position < 89){
            position2--;
            cursor.moveToPosition(position2);
            layout = (RelativeLayout) inflater.inflate(R.layout.activity_card, null);
            layout.setBackgroundResource(R.drawable.card_beginner);
            TextView cardTitle = (TextView) layout.findViewById(R.id.pirate_card_title);
            TextView cardExample = (TextView) layout.findViewById(R.id.pirate_card_example);
            TextView cardDefinition = (TextView) layout.findViewById(R.id.pirate_card_definition);

            cardTitle.setText(cursor.getString(cursor.getColumnIndex(FlashCardTable.COLUMN_TITLE)));
            cardExample.setText(cursor.getString(cursor.getColumnIndex(FlashCardTable.COLUMN_SENTENCE)));
            cardDefinition.setText(cursor.getString(cursor.getColumnIndex(FlashCardTable.COLUMN_DEFINITION)));
        }
        ((VerticalViewPager) view).addView(layout);
        return layout;
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == arg1;
    }
}

解决方案

The issue must be because card is taking time to load data from database and view is not updated after data 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 card 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

这篇关于首先viewpager页空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆