7

Can someone help me with my Syntax? I am dynamically adding some views(custom view) to a FrameLayout that is already defined in XML. those custom view's are different type or same type. I'm able to add views to the screen but unable delete a particular view (either it is same type or different) from ViewGroup. those custom view having onTouch().

Here i'm facing problem : unable to trigger for long click, always takes touch listener

I have to create two options here if user selects a view when long click on that

  1. change background color
  2. delete view.

EDIT : according to tao suggestion i am able to get index of long pressed view if there is no touch listener's to view. but i have to implement touch listeners along with long press...

How can I do this?

4

5 回答 5

3

好的,您的问题是“无法触发长按,总是需要触摸监听器”,但这还不够。我需要更多细节:

  1. 您应该使用哪个视图来处理长按、父视图或子视图?
  2. 你用哪个监听器来处理长按,android.view.View.setOnLongClickListener 或 android.view.GestureDetector?

实际上我上周做了同样的工作。我的经验是:不使用 android.view.View.setOnLongClickListener 也不使用 android.view.GestureDetector,自己处理对父视图的长按。View.java 就是一个很好的例子。


编辑:

我手头没有编译器,所以我只输入伪代码,它会自行处理长按,对于真实代码,View.java 会给你最好的答案。

首先,您需要一个 runnable 来实现您的操作

class CheckForLongPress implements Runnable {
    public void run() {
        if (getParent() != null) {
            // show toast
        }
    }
}

其次,修改你的 onTouchEvent 以检测长按

boolean onTouchEvent(...) {
    switch (event.getAction()) {
        case MotionEvent.ACITON_DOWN:
            // post a delayed runnable to detecting long press action.
            // here mPendingCheckForLongPress is instance of CheckForLongPress
            postDelayed(mPendingCheckForLongPress, ViewConfiguration.getLongPressTimeout());
            ...
            break;

        case MotionEvent.ACTION_MOVE:
            // cancel long press action
            if (distance(event, lastMotionEvent) > mTouchSlop) {
                removeCallbacks(mPendingCheckForLongPress);
            }

            ...

            break;

        case MotionEvent.ACTION_UP:
            // cancel long press action
            removeCallbacks(mPendingCheckForLongPress);

            ...

            break;

再次编辑:

以下是真实代码,不是伪代码,非常简单,显示了如何在 View.onTouchEvent() 中处理长按,希望对您有所帮助。

public class ItemView extends View {

    public ItemView(Context context) {
        super(context);
    }   

    public ItemView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public ItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    Runnable mLongPressDetector = new Runnable() {
            public void run() {
                Toast.makeText(getContext(), "Hello long press", Toast.LENGTH_SHORT).show();
            }
        };

    MotionEvent mLastEvent;

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        mLastEvent = MotionEvent.obtain(event);

        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            postDelayed(mLongPressDetector, ViewConfiguration.getLongPressTimeout());
            break;

        case MotionEvent.ACTION_MOVE:
            final int slop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
            if (Math.abs(mLastEvent.getX() - event.getX()) > slop ||
                Math.abs(mLastEvent.getY() - event.getY()) > slop) {
                removeCallbacks(mLongPressDetector);
            }
            break;

        case MotionEvent.ACTION_UP:
            removeCallbacks(mLongPressDetector);
            break;

        }
        return true;
    }

}
于 2012-07-13T01:27:19.307 回答
3

如果您的每个视图都有一个删除按钮或类似的东西,您可以这样做:

yourLayout.setTag(view);

在您的删除部分:

yourLayout.setOnLongClickListener(new Button.OnLongClickListener() {
        public boolean onLongClick(View view) {
...
...
yourLayout.removeView((View) view.getTag());
return _value;
}

我希望它可以帮助你。

于 2012-07-11T12:30:48.940 回答
1

看这里:

http://developer.android.com/reference/android/view/View.html

有一个 int getId() 的方法。所以每个视图都有一个唯一的标识符。你可以用它来永远查看。

于 2012-07-09T05:17:58.023 回答
1

在这里,我的问题是,当用户触摸该视图时,如何从视图组中识别所选视图。

我假设TwoPointsDraw&OnePointDraw正在扩展 View。

因此,在这种情况下,您可以做的是在创建 TwoPointsDraw.assign 唯一的 Id 或 Tag 对象之后。

TwoPointsDraw drawView = new TwoPointsDraw(context);
drawView.setTag("unique identifier"); <-Must be object type
drawView.setId(unique id); <-must be integer type

当您单击特定视图时。您可以使用它来检查其身份。

view.getTag()或者view.getId()

片段应该看起来像

circle.setOnLongClickListener(new OnLongClickListener() {

    @Override
    public boolean onLongClick(View v) {
        Log.i("Long", v.getTag().toString());
        return false;
    }
});

希望这对您有所帮助。

于 2012-07-09T05:18:27.667 回答
1

为了您OnLongClickListener的工作,当您在 onTouch 函数中检测到长按时,您应该返回 false !

例如:

在您的 GestureDetector 中,为您的 onLongClickListener 提供机会,您应该:

@Override
public boolean onDown(MotionEvent ev) {
    return false;
    // return super.onTouchEvent(ev);
}

否则:

在您的视图中,实现该setOnLongClickListener函数并保存onLongClickListener传递的实例。每当检测到长按时,调用实例的onLongClick函数。

于 2012-07-12T22:45:03.460 回答