我正在尝试将GestureDetector添加到我的应用中的某个活动中。我已经尝试了youtube的几种不同的方法,stackoverflow没有工作。没有错误显示,当我滑动或使用任何手势时没有任何反应。我一直把调试日志放在路上,没有人在logcat中注册我现在都没有想法了。这是我主要感兴趣的onFling()方法。
我正在尝试两种方法。我已经删除了一些代码并对其进行了更改,以使读者更加友好和相关。
如果有人可以帮助我很高兴。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical">
<!-- LinearLayout has two children, Toolbar & DrawerLayout.
DrawerLayout has two children RelativeLayout (main content container)
LinearLayout is the root element so the Navigationdrawer does not open over the Toolbar.
The DrawerLayout was the root element before this.-->
<!-- adding the toolbar layout -->
<include
android:id="@+id/toolbar"
layout="@layout/toolbar"/>
<android.support.v4.widget.DrawerLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/mainDrawerLayout">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/backgroundImg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@drawable/nipple"
android:contentDescription="@string/content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView android:id="@+id/main_tv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:gravity="center"
android:text="Hello"/>
</RelativeLayout>
<fragment android:id="@+id/fragment_navigation_drawer"
android:layout_width="280dp"
android:layout_height="match_parent"
android:layout_gravity="start"
app:layout="@layout/fragment_navigation_drawer"
android:name="com.ansgar.amazingfacts.activities.FragmentNavigationDrawer"
tools:layout="@layout/fragment_navigation_drawer"/>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
方法1定义自定义侦听器类
public class MainActivity extends AppCompatActivity implements FragmentNavigationDrawer.FragmentNavigationDrawerListener {
private ImageView image;
private TextView mTextView;
private Toolbar mToolbar;
private FragmentNavigationDrawer drawerFragment;
GestureDetectorCompat gesture;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image = (ImageView) findViewById(R.id.backgroundImg);
mTextView = (TextView) findViewById(R.id.main_tv);
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
//Fragment for navigation drawer
drawerFragment = (FragmentNavigationDrawer) getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
//This method is created because we need to pass a few things from MainActivity to the FragmentNavigationDrawer
drawerFragment.sepUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(mainDrawerLayout), mToolbar);
drawerFragment.setDrawerListener(this);
this.gesture = new GestureDetectorCompat(this, new CustomGestureClass());
}//onCreate()
@Override
public boolean onTouchEvent(MotionEvent event) {
this.gesture.onTouchEvent(event);
Log.d("GESTURE", "OnTouchEvent");
Toast.makeText(getApplicationContext(), "Touch event", Toast.LENGTH_SHORT).show();
return super.onTouchEvent(event);
}
class CustomGestureClass extends GestureDetector.SimpleOnGestureListener {
//SimpleOnGestureListener is listener for what we want to do and how
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
//return super.onFling(e1, e2, velocityX, velocityY);
Log.d("GESTURE", "OnFling");
float sensitivity = 50;
//swipe left check
if (e1.getX() - e2.getX() > sensitivity) {
Log.d("GESTURE", "swipe left");
return true;
}
//swipe right check
if (e2.getX() - e1.getX() > sensitivity) {
Log.d("GESTURE", "Swipe Right");
Toast.makeText(getApplicationContext(), "Swipe right gesture", Toast.LENGTH_SHORT).show();
return true;
}
//swipe check down
if (e1.getY() - e2.getY() > sensitivity) {
Log.d("GESTURE", "Swipe Down");
Toast.makeText(getApplicationContext(), "Swipe down gesture", Toast.LENGTH_SHORT).show();
return true;
}
//swipe up check
if (e2.getY() - e1.getY() > sensitivity) {
Log.d("GESTURE", "Swipe Up");
Toast.makeText(getApplicationContext(), "Swipe up gesture", Toast.LENGTH_SHORT).show();
return true;
}
return true;
//return false;
}
@Override
public boolean onDown(MotionEvent e) {
Log.d("GESTURE", "OnDown");
//return super.onDown(e);
return true;
}
}//CustomGestureClass
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
//setting the title of actionBar
public void setTitle(String title){
getSupportActionBar().setTitle(title);
}
public void onCreateDrawer() {}
@Override
public void onDrawerItemSelected(View view, int position){
displayView(position);
//Log.d("DRAWER ITEM SELECTED", "the index number of item Drawer RecyclerView "+ position);
switch(position){
case 0:
break;
}
}
private void displayView(int position) {}
}//MainActivity.class
方法2
public class MainActivity extends AppCompatActivity implements FragmentNavigationDrawer.FragmentNavigationDrawerListener,
GestureDetector.OnGestureListener {
private ImageView image;
private TextView mFactTextView;
private Toolbar mToolbar;
private FragmentNavigationDrawer drawerFragment;
GestureDetectorCompat gesture;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image = (ImageView) findViewById(R.id.backgroundImg);
mTextView = (TextView) findViewById(R.id.main_tv);
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
//Fragment for navigation drawer
drawerFragment = (FragmentNavigationDrawer) getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
//This method is created because we need to pass a few things from MainActivity to the FragmentNavigationDrawer
drawerFragment.sepUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(mainDrawerLayout), mToolbar);
drawerFragment.setDrawerListener(this);
this.gesture = new GestureDetectorCompat(this, this);
}//onCreate()
@Override
public boolean onTouchEvent(MotionEvent event) {
this.gesture.onTouchEvent(event);
Log.d("GESTURE", "OnTouchEvent");
Toast.makeText(getApplicationContext(), "Touch event", Toast.LENGTH_SHORT).show();
return super.onTouchEvent(event);
}
@Override
public boolean onDown(MotionEvent e) {
Log.d("GESTURE", "OnDown");
return true;
}
@Override
public void onShowPress(MotionEvent e) {
Log.d("GESTURE", "OnShowPress");
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
Log.d("GESTURE", "OnSingleTapUp");
return true;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
Log.d("GESTURE", "OnScroll");
return true;
}
@Override
public void onLongPress(MotionEvent e) {
Log.d("GESTURE", "OnLongPress");
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
Log.d("GESTURE", "OnFling");
return true;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void onCreateDrawer() {}
@Override
public void onDrawerItemSelected(View view, int position){
displayView(position);
//Log.d("DRAWER ITEM SELECTED", "the index number of item Drawer RecyclerView "+ position);
switch(position){
case 0:
break;
}
}
private void displayView(int position) {}
}//MainActivity.class
答案 0 :(得分:0)
Concerning to the document of onTouchEvent(event) method:“当触摸屏事件未被其下的任何视图处理时调用。这对于处理在窗口范围之外发生的触摸事件非常有用,其中没有查看接收它。“。所以你应该检查:
有一个子视图处理了活动中的事件与否(这意味着此子视图的onTouchEvent(event)方法返回 “的真强>”)。如果不是,请转到第2个
应返回活动的onTouchEvent(event)方法
“ true ”。(根据文件:“如果你有,则返回true
消耗了这个事件,假如你还没有。默认实现
始终返回false “ - &gt;使用return super.onTouchEvent(event);
始终返回” false “)
希望它可以帮到你