2015-09-15 31 views
0

我想创建一个应用程序,左右滑动图像,所以我使用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>   
+0

试试这个教程http://blog.sqisland.com/2012/09/android-swipe-image-viewer-with-viewpager.html –

回答

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;
}

 

+0

您在实例化(编写的代码),但有错误的方式因为它调用过一次 –