longClick监听器的持续时间

问题描述 投票:4回答:1

我想减少我的列表视图响应长的Click Listener的时间。可以缩短点击持续时间吗?

getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view,
                    final int position, long id) {

                    if(selectedHabit){
                        Intent intent = new Intent(parent.getContext(),AddScheduleEventActivity.class );
                        startActivityForResult(intent, CREATE_EVENT);
                        return true;
                    }



                return false;
            }
        });
android onlongclicklistener
1个回答
5
投票

您可以使用OnTouchListener:

    private int lastTouchedViewId = -1;
    private long duration = System.currentTimeMillis();
    private long LONG_CLICK_DURATION = 1000;

...

view.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {


                switch (motionEvent.getAction()) {


                    case MotionEvent.ACTION_DOWN:
                        if (lastTouchedViewId != view.getId()) {
                            lastTouchedViewId = view.getId();
                            duration = System.currentTimeMillis();
                        }
                        else
                        {

                            if(duration-System.currentTimeMillis()> LONG_CLICK_DURATION)

                            doStuff();
                        }
                        return true;

                    case MotionEvent.ACTION_UP:
                        lastTouchedViewId = -1;
                        return true;
                }


                return false;
            }
        });

0
投票

这是有关如何创建自己的TouchListsners来管理VideoView上的Click和LongClick的示例示例。在此示例中,我将单击的数据的idex和视图的索引传递给侦听器(在后面,我在数组中有多个VideoView映射到数据列表,例如在Adapter中)

/**
 * Simple OnTouchListenerIndexed with Indexes for VideoView ClickListeners (should be in Util, jerome ?)
 * You have to handle longClick and click by yourself
 */
private abstract class OnTouchListenerIndexed implements OnTouchListener {
    int dataIndex = INDEX_NOT_DEFINED;
    int imageViewIndex = INDEX_NOT_DEFINED;
    long timeActionDown;
    AtomicBoolean stillNotConsumed=new AtomicBoolean(true);

    public OnTouchListenerIndexed(int dataIndex, int imageViewIndex) {
        this.dataIndex = dataIndex;
        this.imageViewIndex = imageViewIndex;
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if(event.getAction() == MotionEvent.ACTION_DOWN){
            timeActionDown=System.currentTimeMillis();
            stillNotConsumed.set(true);
            v.postDelayed(new Runnable() {
                @Override
                public void run() {
                    if(stillNotConsumed.get()){
                        onLongTouch(dataIndex, imageViewIndex);
                    }
                }
            },1000);
            return true;
        }else if(event.getAction() == MotionEvent.ACTION_UP){
            long timeActionUp=System.currentTimeMillis();
            stillNotConsumed.set(false);
            if(timeActionUp-timeActionDown>1000){
                //une seconde plus tard
                return onLongTouch(dataIndex, imageViewIndex);
            }else{
                return onTouch(dataIndex, imageViewIndex);
            }

        }else{
            //don't consume it
            return false;
        }
    }


    public abstract boolean onTouch(int dataIndex, int imageViewIndex);
    public abstract boolean onLongTouch(int dataIndex, int imageViewIndex);
}
© www.soinside.com 2019 - 2024. All rights reserved.