区分setOnClickListener和setOnTouchListener的区别。

4

我需要帮助。在我的安卓应用中,我有一个包含一些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。当我切换屏幕时,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);
        }

问题在于每次我触摸屏幕时,URL都会被打开,现在我无法在不同的屏幕之间切换。如何做到这一点,以区分onClick()和onTouch()? 请帮帮我...提前感谢。
2个回答

3

我认为您正在将ImageView用作LinearLayout的背景,似乎Activity会处理ImageView的触摸事件。要实现您的功能,请为imageView设置Touch Listener,并使用以下代码来间接处理imageView的单击事件:

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

0

我明白你的问题,但是要切换屏幕,你应该使用Viewflipper的onTouchlistener,像这样:

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

不要使用 Linearlayout 触摸监听器

你可以使用 普通触摸监听器来处理 ImageView 事件

希望能对你有所帮助。


我为ViewFlipper设置了onTouchlistener,但没有任何变化。它与LinearLayout一样工作。 - Gabrielle
我所做的正是使用Flipper Touch进行切换,以及在LinearLayout中使用ListView OnItemClickFor来处理ListView,目前运行良好。 - Uday
ListView没有onitemclickfor方法。 - Gabrielle
抱歉...我误解了。我没有ListView。我在ViewFlipper中使用LinearLayout的ImageView作为背景。 - Gabrielle

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接