为什么我的PagerTabStrip不显示ViewPager正常显示
//这里是activity
package zhangmincong.scau.edu.cn.viewpager;
import android.support.v4.view.PagerTabStrip;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private List<View> list;
private View view1,view2,view3,view4;
private MyAdapter myAdapter;
private ViewPager viewPager;
private List<String> titleList;
private PagerTabStrip pagerTabStrip;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list=new ArrayList<View>();
view1=View.inflate(this,R.layout.view,null);
view2=View.inflate(this,R.layout.view2,null);
view3=View.inflate(this,R.layout.view3,null);
view4=View.inflate(this,R.layout.view4,null);
viewPager= (ViewPager) findViewById(R.id.ViewPager);
pagerTabStrip= (PagerTabStrip) findViewById(R.id.tab);
list.add(view1);
list.add(view2);
list.add(view3);
list.add(view4);
titleList=new ArrayList<String>();
titleList.add("第一页");
titleList.add("第二页");
titleList.add("第三页");
titleList.add("第四页");
myAdapter=new MyAdapter(list,titleList);
viewPager.setAdapter(myAdapter);
}
}
//这里是MyAdapter
package zhangmincong.scau.edu.cn.viewpager;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import java.util.List;
public class MyAdapter extends PagerAdapter {
private List<View> viewList;
private List<String> titleList;
public MyAdapter(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 object) {
return object == view;
}
@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
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context="zhangmincong.scau.edu.cn.viewpager.MainActivity">
<android.support.v4.view.ViewPager
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ViewPager">
<android.support.v4.view.PagerTabStrip
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tab"
android:gravity="top">
</android.support.v4.view.PagerTabStrip>
</android.support.v4.view.ViewPager>
</LinearLayout>
//View的xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is View1"
android:id="@+id/textView"
android:layout_gravity="center" />
</LinearLayout>