我似乎在隐藏和显示ActionBar时遇到问题。 我的应用程序开始时是隐藏的,我希望当用户在屏幕的任何部分向下滑动时将其重新显示,然后将其隐藏然后用户向上滑动。
我尝试通过以下方式实现此目的:
方法1 :在onCreate
View mView = getWindow().getDecorView();
//mView.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_IMMERSIVE;
mView.setSystemUiVisibility(uiOptions);
mView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Toast.makeText(context, "Swiped down", Toast.LENGTH_SHORT).show();
//getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
getSupportActionBar().show();
return true;
case MotionEvent.ACTION_UP:
Toast.makeText(context, "Swiped up", Toast.LENGTH_SHORT).show();
//getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
getSupportActionBar().hide();
return true;
default: return false;
}
//return false;
}
});
方法2 :
@Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getActionMasked();
switch (action) {
case (MotionEvent.ACTION_DOWN):
Toast.makeText(context, "Swiped down", Toast.LENGTH_SHORT).show();
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
getSupportActionBar().show();
return true;
case (MotionEvent.ACTION_UP):
Toast.makeText(context, "Swiped up", Toast.LENGTH_SHORT).show();
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
getSupportActionBar().hide();
return true;
default:
return super.onTouchEvent(event);
}
}
现在是问题所在:
编辑:
通过评论的回复,我设法使滚动条显示和隐藏,但是我仍然遇到#1的问题,即无法在屏幕的任何部分执行操作。
它仅适用于未评估onClickListener
的部分。
答案 0 :(得分:0)
经过几天的调查,我设法解决了这个问题。似乎最好的选择是像这样实现GestureDetector
public class MyGestureDetector extends GestureDetector.SimpleOnGestureListener {
private MainGame mainGame;
MyGestureDetector(MainGame mainGame) {
this.mainGame = mainGame;
}
@Override
public boolean onDown(MotionEvent e) {
Log.d("Gesture", "onDown");
return super.onDown(e);
}
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
Log.d("Gesture", "onSingleTapConfirmed");
return true;
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
Log.d("Gesture", "onSingleTapUp");
return true;
}
@Override
public void onShowPress(MotionEvent e) {
Log.d("Gesture", "onShowPress");
}
@Override
public boolean onDoubleTap(MotionEvent e) {
Log.d("Gesture", "onDoubleTap");
return true;
}
@Override
public boolean onDoubleTapEvent(MotionEvent e) {
Log.d("Gesture", "onDoubleTapEvent");
return true;
}
@Override
public void onLongPress(MotionEvent e) {
Log.d("Gesture", "onLongPress");
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
Log.d("Gesture", "onScroll");
if (e1.getY() < e2.getY()) {
Log.d("Gesture", "Scroll Down");
}
if (e1.getY() > e1.getY()) {
Log.d("Gesture", "Scroll Up");
}
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
if (e1.getY() < e2.getY()) {
Log.d("Gesture", "Swipe Down " + e1.getY() + " - " + e2.getY());
mainGame.getSupportActionBar().hide();
}
if (e1.getY() > e2.getY()) {
Log.d("Gesture", "Swipe Up" + e1.getY() + " - " + e2.getY());
mainGame.getSupportActionBar().hide();
}
return true;
}
}
,并在我的主要活动中将其实现为onTouch
,并在主要活动中为布局的每个部分提供侦听器。
在onCreate
中:
layout.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
mViewSelected = v;
mGestureDetector.onTouchEvent(event);
return false;
}
});
gameScoreP1.setOnTouchListener(this);
gameScoreP2.setOnTouchListener(this);
fullNameP1.setOnTouchListener(this);
fullNameP2.setOnTouchListener(this);
以及覆盖手势的方法:
@Override
public boolean onTouch(View v, MotionEvent event) {
if (myGestureDetector.onSingleTapConfirmed(event)) {
switch (v.getId()) {
case R.id.fullNameP1:
diagUtils.changePlayerNameDialog(1, this);
return false;
case R.id.fullNameP2:
diagUtils.changePlayerNameDialog(2, this);
return false;
case R.id.gameScoreP1:
scoreSetter.manageGameSetPoints(1);
return false;
case R.id.gameScoreP2:
scoreSetter.manageGameSetPoints(2);
return false;
default:
return false;
}
} else return true;
}
现在的问题是,当我在例如gameScoreP1之类的视图上执行拖动动作时,它也会检测到点击。有什么办法可以使这些内容排他性吗?
编辑
设法通过将视图设置为可点击的方式来解决第二个问题,例如:fullNameP1.setClickable(true)