简体   繁体   English

OnItemClickListener和Custom OnTouchListener

[英]OnItemClickListener and Custom OnTouchListener

I have a list of views on a list view that I would like to click on and have the OnItemClickListener get triggered. 我有一个列表视图列表,我想点击它,并触发OnItemClickListener。 Though at the same time I want to be able to swipe each view and have a custom action occur. 虽然我同时希望能够刷每个视图并进行自定义操作。 This means that I had to create our own OnTouchEvent for each view when it is made in the ArrayAdapter. 这意味着我必须在ArrayAdapter中为每个视图创建自己的OnTouchEvent。

Is there a way to have both of those working together, so that I can have a custom action such as swiping an item and clicking on the item occur easily 有没有办法让两个人一起工作,这样我就可以进行自定义操作,例如刷一个项目并轻松点击该项目

This is very similar to how android Recent Activities are handled - you know, they show a list of all recently opened apps, they can be swiped to remove, or clicked to open. 这非常类似于Android Recent Activities的处理方式 - 您知道,它们会显示所有最近打开的应用程序的列表,可以刷卡以删除或点击打开。 Check out their code, I think you'll get a pretty good idea: https://github.com/android/platform_frameworks_base/tree/master/packages/SystemUI/src/com/android/systemui/recent 看看他们的代码,我想你会得到一个非常好的主意: https//github.com/android/platform_frameworks_base/tree/master/packages/SystemUI/src/com/android/systemui/recent

You class can implement both View.OnTouchListener, AdapterView.OnItemClickListener 您的类可以实现View.OnTouchListener,AdapterView.OnItemClickListener

@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
    if(motionEvent.getAction() == MotionEvent.ACTION_UP){
        Log.d(TAG, "ontouch: UP");

     **// Here you can figure if it was simple item click event.
        // We return false only when user touched once on the view. 
        // this will be handled by onItemClick listener.**

        if(lastAction == -1){
            lastAction = MotionEvent.ACTION_UP;
            view.clearFocus();
            return true;
        }
        return false;
    }
    else if(motionEvent.getAction() == MotionEvent.ACTION_DOWN){
        Log.d(TAG, "ontouch: DOWN");
        return false;
    }
    else {
        // This is all action events. 
        lastAction = -1;
        return true;
    }
}



@Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
     // We come here after onTouch event figured out that its a simple touch event and needs to be handled here.
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM