简体   繁体   English

第一个viewpager页面空白

[英]First viewpager page blank

I have a problem that relates to Android's ViewPager. 我有一个与Android的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. 我目前正在尝试实现一个VerticalViewPager(虽然这与问题无关,因为普通ViewPager会出现同样的问题),自定义的PagerAdapter从我的数据库中获取东西并将其输入到屏幕上。

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? 我在这里发现了类似的问题: 为什么ViewPager中的第一个视图显示为空? and Activity using ViewPager, PagerAdapter and AsyncTask causes a blank view but it seems as if nothing in there is able to help me. 使用ViewPager,PagerAdapter和AsyncTask的Activity会导致一个空白视图,但似乎没有任何内容可以帮助我。

This is my custom PagerAdapter code: 这是我自定义的PagerAdapter代码:

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

I had the same issue with ViewPagers, I used them with Fragments. 我对ViewPagers有同样的问题,我将它们与Fragments一起使用。 The fragment inside first page was always empty second and later pages were fine and first pages was ok when i returned from 2nd or 3rd page. 第一页内的片段总是空的第二页,后来的页面很好,当我从第2页或第3页返回时,第一页都没问题。

I found out the reason was that I loaded the data from database in onCreateView method of the fragment, Views were being created before data could be loaded inside them from database. 我发现原因是我在数据库的onCreateView方法中加载了数据库中的数据,正在创建视图,然后才能从数据库中加载数据。 I changed my code to load data in onStart method of the fragment and everything was fine. 我改变了我的代码,在片段的onStart方法中加载数据,一切都很好。

@Override
public void onStart() {
   getActivity().getLoaderManager().initLoader(LOADER_ID,null, this);
   super.onStart();
}

Hope this helps. 希望这可以帮助。

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

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

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