MAGx2
MAGx2

Reputation: 3189

onTouch() and clicking ListView element not working together

I'm trying to do this effect:

when finger is touching view (which is in ListView) view is making bigger (by modifying LayoutParams). When view is released it shrinks and open new Activity.

To accomplish this I wrote this code:

public final class MakeImageBiggerOnTouchListener implements View.OnTouchListener {

    @Override
    public boolean onTouch(final View view, MotionEvent event) {

        switch (event.getActionMasked()) {
            case ACTION_DOWN:
                 if(view.getHeight() >= (int) (mStartHeight * sScaleY)) {
                    return true;
                }

                ValueAnimator animator = ValueAnimator.ofInt(view.getHeight(), (int) (mStartHeight * sScaleY));

                final ViewGroup.LayoutParams lp = view.getLayoutParams();
                animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator valueAnimator) {
                        lp.height = (Integer) valueAnimator.getAnimatedValue();
                        view.setLayoutParams(lp);

                        imageLp.height = (Integer) valueAnimator.getAnimatedValue();
                        image.setLayoutParams(imageLp);
                    }
                });
                animator.setInterpolator(new AccelerateInterpolator());
                animator.start();

                Log.d("xxx", "ACTION_DOWN");

                return true;
            case ACTION_CANCEL:
                makeSmaller();

                return true;
            case ACTION_UP:
                makeSmaller();

                return false;
            default:
                return false;
        }
    }
}

And this in activity that has ListView:

    mPlacesListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent intent = new Intent(MainActivity.this, SecondActivity.class);
            MainActivity.this.startActivity(intent);
        }
    });

My problem is that activity opening doesn't work. View makes bigger when pressed and smaller when release/moved finger but after releasing new Activity doesn't open.

Upvotes: 0

Views: 203

Answers (1)

Rod_Algonquin
Rod_Algonquin

Reputation: 26198

problem:

View.OnTouchListener 

By the time you add it to the ListView's View it will then use it as an ontouch listener replacing the setOnItemClickListener function to the view. So basically it will instead use the MakeImageBiggerOnTouchListener for the item touch event of the view completely ignoring the setOnItemClickListener.

solution:

you need to switch activity in your switch statement instead and pass the context to the MakeImageBiggerOnTouchListener so you can use that context to call the startActivity and the instance of your main activity.

        case ACTION_CANCEL:
            makeSmaller();
            //call activity here
            return true;
        case ACTION_UP:
            makeSmaller();
            //call activity here

Upvotes: 2

Related Questions