Gan*_*esh 6 android toolbar navigation-drawer
我在我的应用程序中实现了Android Navigation抽屉.当用户触摸导航抽屉的外侧时,我可以打开/关闭抽屉.当用户触摸/单击导航抽屉侧时,您是否可以帮助我检测触摸/点击事件.我需要在那个事件中执行一些功能.请查看附带的屏幕截图.
任何帮助都会得到满足.
您必须在dispatchTouchEvent()方法中处理触摸位置.查看更多关于触摸层次这里
@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);
}
Run Code Online (Sandbox Code Playgroud)
您可以使用onDrawerClosed
当您触摸外部屏幕时,
onDrawerClosed将在导航抽屉关闭后呼叫
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
//do here
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3779 次 |
| 最近记录: |