区分setOnClickListener和setOnTouchListener

时间:2011-11-02 10:23:36

标签: android onclick imageview viewflipper ontouchlistener

我需要一些帮助。在我的Android应用程序中,我有一个包含一些LinearLayout的ViewFlipper。在每个LinearLayout上我都有一个ImageView。为了在屏幕之间切换,我使用以下代码:

LinearLayout layMain = (LinearLayout) findViewById(R.id.layout_main);
        layMain.setOnTouchListener((OnTouchListener) this);

// --------------

public boolean onTouch(View arg0, MotionEvent arg1) {

        // Get the action that was done on this touch event
        switch (arg1.getAction()) {
        case MotionEvent.ACTION_DOWN: {
            // store the X value when the user's finger was pressed down
            downXValue = arg1.getX();
            break;
        }

        case MotionEvent.ACTION_UP: {
            // Get the X value when the user released his/her finger
            float currentX = arg1.getX();

            // going backwards: pushing stuff to the right
            if (downXValue < currentX) {
                // Get a reference to the ViewFlipper
                ViewFlipper vf = (ViewFlipper) findViewById(R.id.details);
                // Set the animation
                vf.setOutAnimation(AnimationUtils.loadAnimation(this,
                        R.anim.push_right_out));
                vf.setInAnimation(AnimationUtils.loadAnimation(this,
                        R.anim.push_right_in));

                // Flip!
                vf.showPrevious();
            }

            // going forwards: pushing stuff to the left
            if (downXValue > currentX) {
                // Get a reference to the ViewFlipper
                ViewFlipper vf = (ViewFlipper) findViewById(R.id.details);
                // Set the animation
                // vf.setInAnimation(AnimationUtils.loadAnimation(this,
                // R.anim.push_left_in));
                vf.setOutAnimation(AnimationUtils.loadAnimation(this,
                        R.anim.push_left_out));
                vf.setInAnimation(AnimationUtils.loadAnimation(this,
                        R.anim.push_left_in));
                // Flip!
                vf.showNext();
            }
            break;
        }
        }

        // if you return false, these actions will not be recorded
        return true;
    }

每个屏幕都包含一个ImageView.So,当我切换屏幕时,imageview正在改变。现在我想实现这个:当点击一个图像时,一个url是打开的。这是我的代码:

ViewFlipper vf = (ViewFlipper) findViewById(R.id.details);

        for (i = 0; i < jArray.length(); i++) {
            LinearLayout l = new LinearLayout(this);
            l.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                    LayoutParams.FILL_PARENT));
            l.setBackgroundColor(0x000000);
            l.setOrientation(LinearLayout.VERTICAL);
            vf.addView(l);

            ImageView img = new ImageView(this);
            img.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                    LayoutParams.FILL_PARENT));
            Drawable image1 = ImageOperations(getBaseContext(), url[i]);
            img.setImageDrawable(image1);
            System.out.println("target" + target[i]);
            img.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri
                            .parse("http://google.com"));
                    startActivity(browserIntent);
                }
            });

            l.addView(img);
        }

问题是,每当我触摸屏幕时,网址都会打开,现在我无法在屏幕之间切换。如何做到这一点,使onClick()和Touch()之间的差异?

请帮帮我.. 提前致谢

2 个答案:

答案 0 :(得分:3)

我认为你使用图像View作为线性布局的背景,似乎Activity处理ImageView的触摸,为imageView实现你的功能setTouch监听器,我们可以使用OntouchListener中的以下代码间接处理图像视图的点击,< / p>

if (downXValue == currentX) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri
                        .parse("http://google.com"));
                startActivity(browserIntent);}

答案 1 :(得分:0)

我遇到了你的问题,但问题是,对于切换屏幕你应该使用Viewflipper onTouchlistener,如:

 mFlipper.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View arg0, MotionEvent arg1) {
                     //// your touch code

而不是Linearlayout touch listeners

然后您可以使用normal touch listeners for your Imageview events

希望它有所帮助。