我在我的應用程序中實現了Android導航抽屜。當用戶觸摸導航抽屜的外側時,我可以打開/關閉抽屜。當用戶觸摸/點擊導航抽屜的一側時,您能幫助我們檢測觸摸/點擊事件嗎?我需要在這種情況下執行一些功能。 請檢查附件截圖。 任何幫助將appriciated。如何檢測導航抽屜外的觸摸事件
5
A
回答
7
你必須處理在dispatchTouchEvent()
方法的觸摸位置收盤後會調用。檢查更多關於觸摸層次結構here
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
if (mDrawerLayout.isDrawerOpen(mRightDrawerListView)) {
View content = findViewById(R.id.right_drawer);
int[] contentLocation = new int[2];
content.getLocationOnScreen(contentLocation);
Rect rect = new Rect(contentLocation[0],
contentLocation[1],
contentLocation[0] + content.getWidth(),
contentLocation[1] + content.getHeight());
View toolbarView = findViewById(R.id.toolbar);
int[] toolbarLocation = new int[2];
toolbarView.getLocationOnScreen(toolbarLocation);
Rect toolbarViewRect = new Rect(toolbarLocation[0],
toolbarLocation[1],
toolbarLocation[0] + toolbarView.getWidth(),
toolbarLocation[1] + toolbarView.getHeight());
if (!(rect.contains((int) event.getX(), (int) event.getY())) && !toolbarViewRect.contains((int) event.getX(), (int) event.getY())) {
isOutSideClicked = true;
} else {
isOutSideClicked = false;
}
} else {
return super.dispatchTouchEvent(event);
}
} else if (event.getAction() == MotionEvent.ACTION_DOWN && isOutSideClicked) {
isOutSideClicked = false;
return super.dispatchTouchEvent(event);
} else if (event.getAction() == MotionEvent.ACTION_MOVE && isOutSideClicked) {
return super.dispatchTouchEvent(event);
}
if (isOutSideClicked) {
//make http call/db request
Toast.makeText(this, "Hello..", Toast.LENGTH_SHORT).show();
}
return super.dispatchTouchEvent(event);
}
0
您可以使用onDrawerClosed
,當你觸摸屏幕之外
onDrawerClosed
的導航 抽屜
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
//do here
}
+1
感謝您的回答。 onDrawerClosed(View view)將在很多場景中調用。這對我不起作用。 – Ganesh
+0
可能會幫助[this](http://stackoverflow.com/questions/22590247/android-navigation-drawer-doesnt-pass-ontouchevent-to-activity) –
相關問題
- 1. Android外包導航抽屜
- 2. 導航抽屜中的onClick事件
- 3. 導航抽屜
- 4. 導航抽屜交叉點家事件
- 5. 如何檢測導航抽屜是否打開?
- 6. 如何檢測多點觸摸事件?
- 7. 檢測觸摸事件
- 8. ActionbarSherlock導航抽屜
- 9. Android導航抽屜?
- 10. 從導航抽屜
- 11. Android導航抽屜
- 12. 導航抽屜,ResourceNotFoundException
- 13. 如何檢測活動境外的觸摸事件
- 14. 導航抽屜與導航抽屜片段
- 15. 帶導航抽屜的片段導航
- 16. 的Android弧導航抽屜
- 17. 如何讓導航抽屜完全不關閉左側抽屜?
- 18. Android導航抽屜:如何根據抽屜列表項選擇
- 19. 檢測導航抽屜內部片段的可見性
- 20. 帶導航抽屜的空指針。未檢測到值分配
- 21. 如何實現導航抽屜
- 22. 如何在導航中抽屜
- 23. 如何更改導航抽屜圖標?
- 24. 如何構建導航抽屜?
- 25. 如何從導航抽屜菜單項
- 26. 如何定製谷歌導航抽屜?
- 27. 如何刪除導航抽屜圖標
- 28. 在導航抽屜中導航錯誤?
- 29. 如何創建具有「抽屜外觀」行爲的導航欄
- 30. 檢測具有窗口的服務中的導航按鈕的觸摸事件
超酷的解決方案。保存我的時間! – Ganesh