NullPointerException
NullPointerException

Reputation: 37681

How to launch OnTouchListener on the elements of a ScrollView but only when the user is not Scrolling?

i have a huge problem managing my horizontal scrollview. It haves inside a horizontal linear layout with a lot of vertical linear layouts wich haves a text and a image.

I want that when the user press on the vertical linearlayouts, the ontouchlistener must be called launching a new feature of my app.

The problem is that when the user scrolls the scrollview, the ontouchlistener of the verticallinearlayouts is being called, and i dont want that.

How can this be solved?

Thanks

My code:

final HorizontalScrollView hsv = new HorizontalScrollView(this);
    RelativeLayout.LayoutParams hsvParams = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
    hsvParams.addRule(RelativeLayout.BELOW,6);
    hsv.setBackgroundColor(0x55383838);
    hsv.setLayoutParams(hsvParams); 
    hsv.setVisibility(View.GONE); //inicialmente está oculto

    LinearLayout hsvll = new LinearLayout(this);  
    hsvll.setOrientation(LinearLayout.HORIZONTAL);  
    hsvll.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    hsvll.setPadding(0, 0, 0, 5);

    createThumbnails();

    List <LinearLayout> thumbnailLinearLayouts = new ArrayList<LinearLayout>(); 


for (int i=1; i<=squareGLSurfaceView.pages.size();i++){ //el tope es <=pages.size() porque hay que recordar que se ha añadido una página en blanco al principio, por lo que la última página será = .size(). Nos guiamos por pages.size en lugar de thumbnails.size() porque thumbnails.size() puede haber crecido para añadir una página en blanco al final, que no queremos mostrar
            LinearLayout ll = new LinearLayout(this);  
            ll.setOrientation(LinearLayout.VERTICAL);  
            ll.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
            ll.setPadding(10, 0, 10, 0);
            thumbnailLinearLayouts.add(ll);     

            TextView tv = new TextView(this);  
            tv.setText(""+(i));
            tv.setGravity(Gravity.RIGHT);
            ll.addView(tv);

            ImageView iv = new ImageView(this);
            iv.setImageBitmap(thumbnails.get(i));
            ll.addView(iv);

            hsvll.addView(ll);  
        }

hsv.addView(hsvll);
    rl.addView(hsv);

    for (int i=0;i<thumbnailLinearLayouts.size();i++){
        final int auxIndex=i;
        thumbnailLinearLayouts.get(i).setOnTouchListener(new OnTouchListener(){
            @Override
            public boolean onTouch(View v, MotionEvent event) {                 
                MagazineStatus.currentPage=auxIndex;
                hsv.setVisibility(View.GONE);
                return true;                }                               
        });
    }

Upvotes: 0

Views: 1282

Answers (1)

Aurelian Cotuna
Aurelian Cotuna

Reputation: 3081

The problem seems to be that you are using OnTouchListener. Try to use the OnClickListener instead.

onClick() - Is called when the user either touches the item (when in touch mode), or focuses upon the item with the navigation-keys or trackball and presses the suitable "enter" key or presses down on the trackball.

onTouch() - Is called when the user performs an action qualified as a touch event, including a press, a release, or any movement gesture on the screen (within the bounds of the item).

You can read more about the difference between touch and click here.

Upvotes: 2

Related Questions