2012-10-12 36 views
0

我有一個ViewPager從遠程服務器加載圖像。我的問題是,我不知道如何給它一個功能,一旦選定了特定的位置(未點擊)。比方說,我刷視圖尋呼機,直到它到達像圖像編號5的特定圖像,我想顯示一些吐司說「圖像5」。ViewPager和Toast

pager.setOnItemClickListener(new OnItemClickListener() { 
public void onItemClick(AdapterView<?> arg0, View arg1, int position,long id) { 





public class ImagePagerActivity extends BaseActivity { 

    private ViewPager pager; 

    private DisplayImageOptions options; 

    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.ac_image_pager); 
    Bundle bundle = getIntent().getExtras(); 
    String[] imageUrls = bundle.getStringArray(Extra.IMAGES); 
    int pagerPosition = bundle.getInt(Extra.IMAGE_POSITION, 0); 

    options = new DisplayImageOptions.Builder() 
       .showImageForEmptyUri(R.drawable.image_for_empty_url) 
       .cacheOnDisc() 
       .imageScaleType(ImageScaleType.IN_SAMPLE_INT) 
       .build(); 

    pager = (ViewPager) findViewById(R.id.pager); 
    pager.setAdapter(new ImagePagerAdapter(imageUrls)); 
    pager.setCurrentItem(pagerPosition); 

    } 

    protected void onStop() { 
    imageLoader.stop(); 
    super.onStop(); 
    } 

    private class ImagePagerAdapter extends PagerAdapter { 

    private String[] images; 
    private LayoutInflater inflater; 

    ImagePagerAdapter(String[] images) { 
    this.images = images; 
    inflater = getLayoutInflater(); 
    } 

    public void destroyItem(View container, int position, Object object) { 
    ((ViewPager) container).removeView((View) object); 
    } 

    public void finishUpdate(View container) { 
    } 

    public int getCount() { 
    return images.length; 
    } 

    public Object instantiateItem(View view, int position) { 
       final View imageLayout = inflater.inflate(R.layout.item_pager_image, null); 
       final ImageView imageView = (ImageView) imageLayout.findViewById(R.id.image); 
       final ProgressBar spinner = (ProgressBar) imageLayout.findViewById(R.id.loading); 

    imageLoader.displayImage(images[position], imageView, options, new ImageLoadingListener() { 

    public void onLoadingStarted() { 
    spinner.setVisibility(View.VISIBLE); 
    } 


    public void onLoadingFailed(FailReason failReason) { 
    String message = null; 
    switch (failReason) { 
    case IO_ERROR: 
    message = "Input/Output error"; 
    break; 
    case OUT_OF_MEMORY: 
    message = "Out Of Memory error"; 
    break; 
    case UNKNOWN: 
    message = "Unknown error"; 
    break; 
    } 
    Toast.makeText(ImagePagerActivity.this, message, Toast.LENGTH_SHORT).show(); 

    spinner.setVisibility(View.GONE); 
           imageView.setImageResource(android.R.drawable.ic_delete); 
        } 


    public void onLoadingComplete(Bitmap loadedImage) { 
    spinner.setVisibility(View.GONE); 
    Animation anim = AnimationUtils.loadAnimation(ImagePagerActivity.this, R.anim.fade_in); 
    imageView.setAnimation(anim); 
    anim.start(); 
    } 

    public void onLoadingCancelled() { 
    // Do nothing 
    } 
    }); 

    ((ViewPager) view).addView(imageLayout, 0); 
    return imageLayout; 
    } 

    public boolean isViewFromObject(View view, Object object) { 
    return view.equals(object); 
    } 

    public void restoreState(Parcelable state, ClassLoader loader) { 
    } 
    public Parcelable saveState() { 
    return null; 
    } 

    public void startUpdate(View container) { 
    } 
    } 
    } 

回答

1

您可以實現onPageChangeListener,比你可以使用這個pageChageListener設爲您的ViewPager(setOnPageChangeListener())...

public class ImagePagerActivity extends BaseActivity implements onPageChangeListener { 
    private ViewPager pager; 
    // other variables here 

    public void onCreate(Bundle savedInstanceState) { 
     // create view Pager 
     // other variables 
     pager.setOnPageChangeListener(this); 
    } 

    // methods for Page Change Listener 
    @Override 
    public void onPageSelected(int pos) { 
     if (pos==5) { 
      //show Toast here 
     } 
    } 

    @Override 
    public void onPageScrollStateChanged(int arg0) { 
     // do Nothing 
    } 

    @Override 
    public void onPageScrolled(int arg0, float arg1, int arg2) { 
     // do Noting 
    } 

}// class def ends here 

編輯..

使用此手短版本

Pager.setOnPageChangeListener(new OnPageChangeListener() { 

    @Override 
    public void onPageSelected(int arg0) { 
     if (pos==5) { 
      //show Toast here 
     } 
    } 

    @Override 
    public void onPageScrolled(int arg0, float arg1, int arg2) { 
     // TODO Auto-generated method stub 
    } 

    @Overridee3public void onPageScrollStateChanged(int arg0) { 
     // TODO Auto-generated method stub 
    } 
}); 
+0

我試圖用「setOnPageChangeListener()」,但它給我一個錯誤,它說:「該方法setOnPageChangeListener(ViewPager.OnPageChangeListener )在類型ViewPager不適用於參數(ImagePagerActivity)「 –

+0

您需要使用viewPager實例調用它... ie viewPager.setOnPageChangeListener()... – aProgrammer

+0

我做了,這就是我得到 –

0

您應該使用OnPageChangeListener

有可用於獲取當前位置的onPageSelected(int position)方法。

喜歡的東西

pager.setOnPageChangeListener(this); 

然後讓你的ImagePagerActivity實現OnPageChangeListener接口:

public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { 
} 

public void onPageSelected(int position) { 
    Toast.makeText(getApplicationContext(), "image " + position, Toast.LENGTH_SHORT).show(); 
} 

public void onPageScrollStateChanged(int state) { 
}