ViewPager 标题直到我滑动它才会出现 [英] ViewPager Title doesn't appear until I swipe it

查看:28
本文介绍了ViewPager 标题直到我滑动它才会出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习使用 ViewPager 和 PagerTabStrip 来实现导航栏.我已经实现了,我的问题是:每次我重新打开应用程序时,标题不显示,但是我滑动一次后,标题再次出现,然后一切正常.代码如下:

定制适配器

public class MyPagerAdapter extends PagerAdapter {私人列表<查看>视图列表;私人列表<字符串>标题列表;public MyPagerAdapter(List viewList, List titleList){this.viewList = viewList;this.titleList = titleList;}@覆盖公共 int getCount() {返回 viewList.size();}@覆盖公共布尔 isViewFromObject(视图视图,对象 o){返回视图 == o;}@覆盖公共对象实例化项目(视图组容器,整数位置){container.addView(viewList.get(position));返回 viewList.get(position);}@覆盖public void destroyItem(ViewGroup 容器,int 位置,Object 对象){container.removeView(viewList.get(position));}@覆盖公共 CharSequence getPageTitle(int position) {返回 titleList.get(position);}}

.xml 文件:

这是在我滑动到第二页之后:

我真的很沮丧.谢谢!!

解决方案

com.android.support:appcompat-v7:23.0.0中出现的问题.您可以在此处参考 https://code.google.com/p/android/issues/detail?id=183127

在该链接中,谷歌支持团队提到该缺陷将在未来版本中修复.所以现在的解决方案是使用 com.android.support:appcompat-v7:22.2.1 构建项目

更新:如果对您可行,那么您可以继续使用@nidheeshdas 提供的另一种解决方案.我尝试过简单的项目;它工作Activity的onResume()里面@nidheeshdas的修改方案

viewPager.setCurrentItem(1);viewPager.postDelayed(new Runnable() {@覆盖公共无效运行(){viewPager.setCurrentItem(0);}},100);

新更新:如上述 google 问题跟踪链接和 JP Ventura 评论中所述.我已尝试使用新版本的库,问题似乎已解决.

I am learning to use ViewPager and PagerTabStrip to implement navigation bar. I have implemented it, my problem is: every time I open the app fresh, the titles don't show, but after I swipe it once, the titles all appear again, and then everything is normal. code shown below:

Customised Adapter

public class MyPagerAdapter extends PagerAdapter {
    private List<View> viewList;
    private List<String> titleList;

    public MyPagerAdapter(List<View> viewList, List<String> titleList){
        this.viewList = viewList;
        this.titleList = titleList;
    }

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

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

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        container.addView(viewList.get(position));
        return viewList.get(position);
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView(viewList.get(position));
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return titleList.get(position);
    }
}

.xml File:

<android.support.v4.view.ViewPager
    android:id="@+id/viewPager"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center">

    <android.support.v4.view.PagerTabStrip
        android:id="@+id/tab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        />

</android.support.v4.view.ViewPager>

This is the screenshot of "Just clicked the app icon":

And this is after I swiped to the second page:

I'm really frustrated. Thanks!!

解决方案

It is an issue appeared in com.android.support:appcompat-v7:23.0.0. You can refer here https://code.google.com/p/android/issues/detail?id=183127

In that link google support team have mention that defect would be fixed in future releases. So for now solution is build the project using com.android.support:appcompat-v7:22.2.1

Update : If feasible for you then you can go ahead with another solution provided by @nidheeshdas. I have tried on simple project; it work Modified solution of @nidheeshdas inside onResume() of Activity

viewPager.setCurrentItem(1);
    viewPager.postDelayed(new Runnable() {
        @Override
        public void run() {
            viewPager.setCurrentItem(0);
        }
    },100);

New Update: As mentioned in the above google issue tracker link and comments from JP Ventura. I have tried with new version of library and issue seems to be fixed.

这篇关于ViewPager 标题直到我滑动它才会出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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