簡體   English   中英

OnItemClickListener和Custom OnTouchListener

[英]OnItemClickListener and Custom OnTouchListener

我有一個列表視圖列表,我想點擊它,並觸發OnItemClickListener。 雖然我同時希望能夠刷每個視圖並進行自定義操作。 這意味着我必須在ArrayAdapter中為每個視圖創建自己的OnTouchEvent。

有沒有辦法讓兩個人一起工作,這樣我就可以進行自定義操作,例如刷一個項目並輕松點擊該項目

這非常類似於Android Recent Activities的處理方式 - 您知道,它們會顯示所有最近打開的應用程序的列表,可以刷卡以刪除或點擊打開。 看看他們的代碼,我想你會得到一個非常好的主意: https//github.com/android/platform_frameworks_base/tree/master/packages/SystemUI/src/com/android/systemui/recent

您的類可以實現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