简体   繁体   English

ViewPager和PageTitleStrip

[英]ViewPager and PageTitleStrip

So,I am trying to create Swipe tabs using ViewPager from suppoer.v4 library. 因此,我正在尝试使用suppoer.v4库中的ViewPager创建“滑动”选项卡。

Everything works as meant,BUT the title-bar and tabs are not showing. 一切正常,但标题栏和选项卡未显示。 I know that the tabs bar is there because like 2-3 pixels are shown of it (I set a specific color,that's how I know), but it's not fully shown, nor is the title. 我知道标签栏在那里,是因为它显示了2-3个像素(我设置了一种特定的颜色,这就是我所知道的),但是它没有完全显示,标题也没有。

I will link my MainActivity.java ,activity_main.xml and if anyone asks,I will add the two fragments.But I doubt that is the problem as they have nothing in there besides a linear layout and a textView. 我将链接我的MainActivity.java,activity_main.xml,如果有人询问,我将添加两个片段。但是我怀疑这是问题所在,因为它们除了线性布局和textView外什么都没有。

MainActivity.java: MainActivity.java:

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;

public class MainActivity extends FragmentActivity {

    ViewPager viewPager=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        viewPager = (ViewPager) findViewById(R.id.viewPager);
        FragmentManager fm = getSupportFragmentManager();
        viewPager.setAdapter(new Adaptor(fm));
    }

    class Adaptor extends FragmentPagerAdapter{


        Fragment fragment = null;

        public Adaptor(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int i) {

            if(i==0){
                fragment = new OpiniaTa();
            }
            if(i==1){
                fragment = new Test();
            }

            return fragment;
        }

        @Override
        public int getCount() {
            return 2;
        }

        @Override
        public CharSequence getPageTitle(int position) {

            if(position == 0){
                return "Opinia ta";
            }
            if(position == 1){
                return "test";
            }

            return null;
        }
    }

}

activity_main.xml: activity_main.xml:

<android.support.v4.view.ViewPager
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/viewPager"
    android:layout_height="match_parent"
    android:layout_width="match_parent">

    <android.support.v4.view.PagerTitleStrip
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/titleTabs"
        android:background="#33B5E5"
        android:foregroundGravity="top"
        android:paddingTop="4dp"
        android:paddingBottom="4dp"
        >
    </android.support.v4.view.PagerTitleStrip>

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

If any other information needed, let me know, I will be as prompt as possible. 如果需要其他任何信息,请告诉我,我会尽快提供。

Could you post a screenshot of the result. 您可以张贴结果的屏幕截图吗? I am wondering if its just the content in tabs or the tabs itself are not displayed. 我想知道是否仅显示选项卡中的内容或选项卡本身。

There is an issue on 23.0.0 with text in tab missing https://code.google.com/p/android/issues/detail?id=183127 https://code.google.com/p/android/issues/detail?id=184431 在23.0.0上存在一个问题,选项卡中的文本缺少https://code.google.com/p/android/issues/detail?id=183127 https://code.google.com/p/android/issues/ detail?id = 184431

Maybe your issue is not related to this. 也许您的问题与此无关。 Just want to make sure. 只想确保即可。

Alternatively, use the below link as suggested the poster. 或者,使用以下链接,如海报所示。 Probably wrap_content to height... 可能将wrap_content设置为高度...

How can we work-around the blank title in PagerTitleStrip and PagerTabStrip? 我们如何解决PagerTitleStrip和PagerTabStrip中的空白标题?

I think in Adaptor class, you should make a constructor include list of fragments. 我认为在Adaptor类中,应该使constructor包含片段列表。 The following code is what I am doing this. 以下代码是我正在执行的操作。 Hope this help. 希望能有所帮助。

public class PagerAdapter extends FragmentPagerAdapter {

    private  List<Fragment> fragments;

    public PagerAdapter(FragmentManager fm, List<Fragment> fragmentList) {
        super(fm);

        this.fragments = fragmentList;
    }

    @Override
    public Fragment getItem(int i) {
        return this.fragments.get(i);
    }

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

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0:
                return "Opinia ta";
            case 1:
                return "test";
            default:
                return null;
        }
    }
}

And in MainActivity class: MainActivity类中:

 private  PagerAdapter mPagerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.viewpager_layout);
        initializePaging();
    }

    private void initializePaging() {
        List<Fragment> fragments = new Vector<Fragment>();
        fragments.add(Fragment.instantiate(this, WebServiceFragment.class.getName()));
        fragments.add(Fragment.instantiate(this, CMCSMOFragment.class.getName()));

        mPagerAdapter = new PagerAdapter(this.getSupportFragmentManager(), fragments);

        ViewPager pager = (ViewPager) findViewById(R.id.viewpager);
        pager.setAdapter(mPagerAdapter);
    }

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

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