onTouch()并单击ListView元素不能一起工作

时间:2014-08-04 19:30:59

标签: android

我试图这样做:

当手指触摸视图(在ListView中)视图变得更大(通过修改LayoutParams)。当视图被释放时,它会收缩并打开新的Activity。

为了做到这一点,我写了这段代码:

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;
        }
    }
}

这是有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);
        }
    });

我的问题是活动开放不起作用。按下时视图会变大,释放/移动手指时视图会变小但释放新活动后不会打开。

1 个答案:

答案 0 :(得分:2)

<强>问题:

View.OnTouchListener 

当您将其添加到ListView's View时,它会将其用作ontouch侦听器,将setOnItemClickListener函数替换为视图。所以基本上它会使用MakeImageBiggerOnTouchListener来完全忽略setOnItemClickListener视图的项目触摸事件。

<强>溶液

您需要在switch语句中切换活动,并将上下文传递给MakeImageBiggerOnTouchListener,以便您可以使用该上下文调用startActivity和您的实例主要活动。

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