我是中国人,使用谷歌翻译提问,有一些问题要问你。 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>