2

我是中国人,用谷歌翻译提问,有一些问题想问你。PagerTabStrip 标题在开始进入页面时没有出现,而当我滑动的时候它显示出来了,它的标题是怎么一开始就显示出来的呢?希望有人能解答,谢谢。

这是 MainActivity.java

public class MainActivity extends Activity {

List<View> views = new ArrayList<View>();
String[] mTitle = {"left", "current", "right"};
View view1, view2, view3;
ViewPager viewPager;


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

    viewPager= (ViewPager) findViewById(R.id.viewpager);

    LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);

    view1 = inflater.inflate(R.layout.left_layout, null);
    view2 = inflater.inflate(R.layout.activity_main, null);
    view3 = inflater.inflate(R.layout.right_layout, null);

    views.add(view1);
    views.add(view2);
    views.add(view3);

    PagerAdapter pagerAdapter=new MyPagerAdapter(mTitle);
    viewPager.setAdapter(pagerAdapter);
    viewPager.setCurrentItem(1);

}

class MyPagerAdapter extends PagerAdapter {

    String [] mTitles;

    public MyPagerAdapter(String [] titles){
        mTitles=titles;
    }

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

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

    @Override
    public CharSequence getPageTitle(int position) {
        return mTitle[position];
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        return null;
    }

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

}

activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <android.support.v4.view.PagerTabStrip
        android:id="@+id/pagertitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        ></android.support.v4.view.PagerTabStrip>

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

4

1 回答 1

2

这是支持库中的一个错误,请参阅另一个问题,在这里问同样的事情:ViewPager Title doesn't appear until I swipe it

于 2015-10-17T17:48:12.653 回答