我想创建一个向左/向右滑动图片的应用程序,所以我使用viewPager为此运行此代码它成功运行但空白屏幕没有任何反应。
这是我的homeSwipe Class
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>
答案 0 :(得分:0)
This is custompagerAdapter.
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
public class PropTypePagerAdapter extends FragmentStatePagerAdapter {
int noOfTab;
public PropTypePagerAdapter(FragmentManager fm ,int noOfTab) {
super(fm);
this.noOfTab=noOfTab;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new PropTabType(0);
case 1:
return new PropTabType(1);
case 2:
return new PropTabType(2);
case 3:
PropTabType service = new PropTabType(3);
return service;
}
}
@Override
public int getCount() {return noOfTab;
}
}
And this is logic for swapping image
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_prop_type_tab,container, false);
fragImg = (ImageView) view.findViewById(R.id.fragImg);
if (position == 0) {
fragImg.setImageResource(R.drawable.buy);
} else if (position == 1) {
fragImg.setImageResource(R.drawable.rent1);
} else {
fragImg.setImageResource(R.drawable.hostel);
}
return view;
}