如何在viewpager中使用滑动设置onclick并行?

时间:2014-04-17 14:43:41

标签: java android android-viewpager

在这里,我开发了一个应用程序来查看全屏图像。我能够通过滑动来开发它以移动到下一个图像。我使用了一个viewpager元素。 我如何使用onclick动作来查看做什么。(删除,分享等等。)

我的代码如下所示,

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_full_screen);

    Thread child=new Thread(){
        @Override
        public void run() {
            viewPager = (ViewPager) findViewById(R.id.pager);
            utils = new Utils(getApplicationContext());

            Intent i = getIntent();
            int position = i.getIntExtra("position", 0);

            adapter = new FullScreenImageAdapter(FullScreenViewActivity.this,utils.getFilePaths());
            viewPager.setAdapter(adapter);
            viewPager.setCurrentItem(position);//show the selected

            btnMail=(Button)findViewById(R.id.btnMailThis);
            btnRate=(Button)findViewById(R.id.btnRate);

            btnMail.setVisibility(View.INVISIBLE);
            btnRate.setVisibility(View.INVISIBLE);



        }
    };
    child.start();
}

FullScreenImageAdapter.java如下所示

public class FullScreenImageAdapter extends PagerAdapter {

private Activity _activity;
private ArrayList<String> _imagePaths;
private LayoutInflater inflater;

// constructor
public FullScreenImageAdapter(Activity activity,
        ArrayList<String> imagePaths) {
    this._activity = activity;
    this._imagePaths = imagePaths;
}

@Override
public int getCount() {
    return this._imagePaths.size();
}

@Override
public boolean isViewFromObject(View view, Object object) {
    return view == ((RelativeLayout) object);
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
    TouchImageView imgDisplay;
    //Button btnClose;

    inflater = (LayoutInflater) _activity
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View viewLayout = inflater.inflate(R.layout.layout_full_image, container,
            false);

    imgDisplay = (TouchImageView) viewLayout.findViewById(R.id.imgDisplay);
    //btnClose = (Button) viewLayout.findViewById(R.id.btnClose);

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    Bitmap bitmap = BitmapFactory.decodeFile(_imagePaths.get(position), options);
    imgDisplay.setImageBitmap(bitmap);
    /* 
    // close button click event
    btnClose.setOnClickListener(new View.OnClickListener() {            
        @Override
        public void onClick(View v) {
            _activity.finish();
        }
    });*/

    ((ViewPager) container).addView(viewLayout);

    return viewLayout;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    ((ViewPager) container).removeView((RelativeLayout) object);

}

提前致谢..!

1 个答案:

答案 0 :(得分:0)

您可以创建自己的继承ViewPager的类并覆盖onInterceptTouchEvent,如下所示:

@Override
public boolean onInterceptTouchEvent(MotionEvent arg0) {
    // TODO Auto-generated method stub

    if(arg0.getAction() == MotionEvent.ACTION_UP) {
        //Your code here
        return false;

    } else {
        //Do this to keep swipes working. It will also make vertical swiping work. You can avoid the latter by handling MotionEvent action and x,y directions.   
        return super.onInterceptTouchEvent(arg0);
    }

}

不要忘记用com.example.yourpackagename.YourViewPagerClass替换xml中的ViewPager对象