一般viewpage在xml引用如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<android.support.v4.view.ViewPager
android:id="@+id/guidePages"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/> </LinearLayout>
这样做是没错的,可是运行时找不到android.support.v4.view.ViewPager,已经导入了库文件android-support-v4.jar,
原因:未添加源文件,做法如下:
新建libs文件夹,将android-support-v4.jar放入到该文件夹下,右键该文件夹,选择Build Path->Use as Source Folder,这样问题就迎刃而解了。
原因分析:xml文件引用的是源文件。
附上代码:
main类:
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;public class ViewPageTest2 extends Activity {
/** Called when the activity is first created. */
private ViewPager viewPager = null;
private ArrayList<View> listView = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initViewPager();
}
private void initViewPager() {
viewPager = (ViewPager) findViewById(R.id.guidePages);
listView = new ArrayList<View>();
LayoutInflater inflater = getLayoutInflater();
listView.add(inflater.inflate(R.layout.one, null));
listView.add(inflater.inflate(R.layout.two, null));
listView.add(inflater.inflate(R.layout.three, null));
viewPager.setAdapter(new MyPagerAdapter(listView));
viewPager.setOnPageChangeListener(new MyPagerChangeListener());
} }
MyPagerAdapter类:
import java.util.List;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;public class MyPagerAdapter extends PagerAdapter {
private List<View> list;
public MyPagerAdapter(List<View> list) {
this.list = list;
}
@Override
public void destroyItem(View view, int index, Object arg2) {
// TODO Auto-generated method stub
((ViewPager)view).removeView(list.get(index));
} @Override
public void finishUpdate(View arg0) {
// TODO Auto-generated method stub
} @Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
} @Override
public Object instantiateItem(View view, int index) {
((ViewPager)view).addView(list.get(index), 0);
return list.get(index);
} @Override
public boolean isViewFromObject(View view, Object object) {
// TODO Auto-generated method stub
return view == (object);
} @Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {
// TODO Auto-generated method stub
} @Override
public Parcelable saveState() {
// TODO Auto-generated method stub
return null;
} @Override
public void startUpdate(View arg0) {
// TODO Auto-generated method stub
}
}
MyPagerChangeListener类:
import android.support.v4.view.ViewPager.OnPageChangeListener;
public class MyPagerChangeListener implements OnPageChangeListener
{ @Override
public void onPageScrollStateChanged(int arg0)
{
// TODO Auto-generated method stub
} @Override
public void onPageScrolled(int arg0, float arg1, int arg2)
{
// TODO Auto-generated method stub
} @Override
public void onPageSelected(int arg0)
{
// TODO Auto-generated method stub
}}