当我在我的代码中编写mViewPager.setonclicklistener时,它无法正常工作。如何实施 ViewPager的setOnClickListener。滑动图像工作正常。在此项中是网址列表(图片)
请帮帮我。
这是我的代码:
GalleryUrlActivity.Java
UrlPagerAdapter pagerAdapter = new UrlPagerAdapter(this, items);
pagerAdapter.setOnItemChangeListener(new OnItemChangeListener()
{
@Override
public void onItemChange(int currentPosition)
{
title.setText(itemNames.get(currentPosition));
descripiton.setText(itemdesc.get(currentPosition));
//Toast.makeText(GalleryUrlActivity.this, itemNames.get(currentPosition), Toast.LENGTH_SHORT).show();
}
});
mViewPager = (GalleryViewPager)findViewById(R.id.viewer);
mViewPager.setOffscreenPageLimit(3);
mViewPager.setAdapter(pagerAdapter);
UrlPageAdapter.java
public class UrlPagerAdapter extends BasePagerAdapter {
public UrlPagerAdapter(Context context, List<String> resources)
{
super(context, resources);
}
@Override
public void setPrimaryItem(ViewGroup container, int position, Object object) {
super.setPrimaryItem(container, position, object);
((GalleryViewPager)container).mCurrentView = ((UrlTouchImageView)object).getImageView();
}
@Override
public Object instantiateItem(ViewGroup collection, int position){
final UrlTouchImageView iv = new UrlTouchImageView(mContext);
iv.setUrl(mResources.get(position));
iv.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
collection.addView(iv, 0);
return iv;
}
}
GalleryViewPager.java
public class GalleryViewPager extends ViewPager {
PointF last;
public TouchImageView mCurrentView;
public GalleryViewPager(Context context) {
super(context);
}
public GalleryViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
private float[] handleMotionEvent(MotionEvent event)
{
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
last = new PointF(event.getX(0), event.getY(0));
break;
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_UP:
PointF curr = new PointF(event.getX(0), event.getY(0));
return new float[]{curr.x - last.x, curr.y - last.y};
}
return null;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_UP)
{
super.onTouchEvent(event);
}
float [] difference = handleMotionEvent(event);
if (mCurrentView.pagerCanScroll()) {
return super.onTouchEvent(event);
}
else {
if (difference != null && mCurrentView.onRightSide && difference[0] < 0) //move right
{
return super.onTouchEvent(event);
}
if (difference != null && mCurrentView.onLeftSide && difference[0] > 0) //move left
{
return super.onTouchEvent(event);
}
if (difference == null && ( mCurrentView.onLeftSide || mCurrentView.onRightSide))
{
return super.onTouchEvent(event);
}
}
return false;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_UP)
{
super.onInterceptTouchEvent(event);
}
float [] difference = handleMotionEvent(event);
if (mCurrentView.pagerCanScroll()) {
return super.onInterceptTouchEvent(event);
}
else {
if (difference != null && mCurrentView.onRightSide && difference[0] < 0) //move right
{
return super.onInterceptTouchEvent(event);
}
if (difference != null && mCurrentView.onLeftSide && difference[0] > 0) //move left
{
return super.onInterceptTouchEvent(event);
}
if (difference == null && ( mCurrentView.onLeftSide || mCurrentView.onRightSide))
{
return super.onInterceptTouchEvent(event);
}
}
return false;
}
}
UrlTouchImageView.java
public class UrlTouchImageView extends RelativeLayout {
protected ProgressBar mProgressBar;
protected TouchImageView mImageView;
protected Context mContext;
public UrlTouchImageView(Context ctx)
{
super(ctx);
mContext = ctx;
init();
}
public UrlTouchImageView(Context ctx, AttributeSet attrs)
{
super(ctx, attrs);
mContext = ctx;
init();
}
public TouchImageView getImageView() { return mImageView; }
@SuppressWarnings("deprecation")
protected void init() {
mImageView = new TouchImageView(mContext);
LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
mImageView.setLayoutParams(params);
this.addView(mImageView);
mImageView.setVisibility(GONE);
mProgressBar = new ProgressBar(mContext, null, android.R.attr.progressBarStyleHorizontal);
params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_VERTICAL);
params.setMargins(30, 0, 30, 0);
mProgressBar.setLayoutParams(params);
mProgressBar.setIndeterminate(false);
mProgressBar.setMax(100);
this.addView(mProgressBar);
}
public void setUrl(String imageUrl)
{
new ImageLoadTask().execute(imageUrl);
}
//No caching load
public class ImageLoadTask extends AsyncTask<String, Integer, Bitmap>
{
@Override
protected Bitmap doInBackground(String... strings) {
String url = strings[0];
Bitmap bm = null;
try {
URL aURL = new URL(url);
URLConnection conn = aURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
int totalLen = conn.getContentLength();
InputStreamWrapper bis = new InputStreamWrapper(is, 8192, totalLen);
bis.setProgressListener(new InputStreamProgressListener()
{
@Override
public void onProgress(float progressValue, long bytesLoaded,
long bytesTotal)
{
publishProgress((int)(progressValue * 100));
}
});
bm = BitmapFactory.decodeStream(bis);
bis.close();
is.close();
} catch (Exception e) {
e.printStackTrace();
}
return bm;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
if (bitmap == null)
{
mImageView.setScaleType(ScaleType.CENTER);
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.no_photo);
mImageView.setImageBitmap(bitmap);
}
else
{
mImageView.setScaleType(ScaleType.MATRIX);
mImageView.setImageBitmap(bitmap);
}
mImageView.setVisibility(VISIBLE);
mProgressBar.setVisibility(GONE);
}
@Override
protected void onProgressUpdate(Integer... values)
{
mProgressBar.setProgress(values[0]);
}
}
}