我有一个ViewPager
从远程服务器加载图像。我的问题是,一旦选择了特定位置(未点击),我就不知道如何给它一个功能。让我们说,我滑动视图寻呼机,直到它到达特定图像,如图像编号5,我想显示一些Toast说"图像5"。
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) {
}
}
}
答案 0 :(得分:1)
您可以实施onPageChangeListener
,但您可以使用(setOnPageChangeListener()
)将此pageChageListener设置为ViewPager ...
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
}
});
答案 1 :(得分: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) {
}