我想創建一個應用程序,左右滑動圖像,所以我使用viewPager爲此,我運行此代碼它得到成功運行,但空白屏幕沒有任何反應。圖像刷卡不工作在Android視圖頁面
這是我homeSwipe類
public class homeSwipe extends Activity {
ViewPager viewPager;
customPagerAdapter customPagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_swipe);
viewPager = (ViewPager) findViewById(R.id.pager);
customPagerAdapter = new customPagerAdapter(homeSwipe.this);
viewPager.setAdapter(customPagerAdapter);
}
}
這是我的layout.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:orientation="vertical">
<android.support.v4.view.ViewPager 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:id="@+id/pager">
</android.support.v4.view.ViewPager>
</RelativeLayout>
這是我customPagerAdapter類
public class customPagerAdapter extends PagerAdapter {
private int imgres[] ={R.drawable.graypatternbackground,R.drawable.home,R.drawable.homescreen,R.drawable.redwall};
private Context context;
private LayoutInflater layoutInflater;
public customPagerAdapter(Context context){
this.context =context;
}
@Override
public int getCount() {
return imgres.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return (view == (LinearLayout)object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
layoutInflater = (LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
View item_view =(View)layoutInflater.inflate(R.layout.swipelayout,container,false);
ImageView imgView = (ImageView)item_view.findViewById(R.id.imageView);
imgView.setImageResource(imgres[position]);
return item_view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((LinearLayout)object);
}
}
這是我swipelayout.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">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageView"/>
</LinearLayout>
試試這個教程http://blog.sqisland.com/2012/09/android-swipe-image-viewer-with-viewpager.html –