老师,我这个代码运行老师挂掉,麻烦您帮我看下那出错了,谢谢。
package com.example.pageradapter;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.PagerTabStrip;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {
private List<View> listview;
private ViewPager pager;
private List<String> titlelist;
private PagerTabStrip tab;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//初始化viewpager控件
pager = (ViewPager) findViewById(R.id.pager);
//准备数据源
View view1 = View.inflate(this, R.layout.view1, null);
View view2 = View.inflate(this, R.layout.view2, null);
View view3 = View.inflate(this, R.layout.view3, null);
View view4 = View.inflate(this, R.layout.view4, null);
listview = new ArrayList<View>();
listview.add(view1);
listview.add(view2);
listview.add(view3);
listview.add(view4);
//为页卡设置标题
titlelist = new ArrayList<String>();
titlelist.add("第一页");
titlelist.add("第二页");
titlelist.add("第三页");
titlelist.add("第四页");
//1.创建适配器
MyPagerAdapter adapter = new MyPagerAdapter(listview,titlelist);
//3.pager加载适配器
pager.setAdapter(adapter);
}
}
-------------------------------------------------------------------------------------------
package com.example.pageradapter;
import java.util.List;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
public class MyPagerAdapter extends PagerAdapter {
private List<View> listview;
private List<String> titlelist;
public MyPagerAdapter(List<View> listview, List<String> titlelist){
this.listview = listview;
this.titlelist = titlelist;
}
//放回页面(页卡)的数量
@Override
public int getCount() {
// TODO Auto-generated method stub
return listview.size();
}
//view是否是来自于对象
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return arg0 == arg1;
}
//实例化页卡
@Override
public Object instantiateItem(ViewGroup container, int position) {
container.addView(listview.get(position));
return listview.get(position);
}
//销毁一个页卡
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(listview.get(position));
}
@Override
public CharSequence getPageTitle(int position) {
// TODO Auto-generated method stub
return titlelist.get(position);
}
}
-------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
>
</android.support.v4.view.ViewPager>
<android.support.v4.view.PagerTabStrip
android:id="@+id/tab"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</android.support.v4.view.PagerTabStrip>
</LinearLayout>
------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第一个页面"
android:layout_gravity="center"
/>
</LinearLayout>
。。。。。。