基本上,我试图实现OnTouchListener来监听滑动手势。我为此创建了一个类OnSwipeTouchListener.java,并且我在GalleryActivity.java中调用了这个类,这是发生错误的地方。我一直收到这个错误:无法解析符号' ThemeSelectionActivity',我按Alt Enter但没有任何反应。我该如何解决这个问题。
以下是出现错误的GalleryActivity.java的代码:
package com.mydomain.sample;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.Toast;
import java.util.ArrayList;
public class GalleryActivity extends AppCompatActivity {
GridView gv_folder, gridView;
RelativeLayout rootLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gallery);
gv_folder = (GridView)findViewById(R.id.gallery_gridview_folders);
rootLayout = (RelativeLayout) findViewById(R.id.galleryRootLayout);
rootLayout.setOnTouchListener(new OnSwipeTouchListener(ThemeSelectionActivity.this) {
@Override
public void onSwipeRight() {
super.onSwipeRight();
gv_folder.setVisibility(View.INVISIBLE);
}
});
}
}
这是OnSwipeTouchListener.java类
package com.mydomain.sample;
import android.content.Context;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
public class OnSwipeTouchListener implements OnTouchListener {
private final GestureDetector gestureDetector;
public OnSwipeTouchListener(Context ctx) {
gestureDetector = new GestureDetector(ctx, new GestureListener());
}
@Override
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
private final class GestureListener extends SimpleOnGestureListener {
private static final int SWIPE_THRESHOLD = 1;
private static final int SWIPE_VELOCITY_THRESHOLD = 1;
@Override
public boolean onDown(MotionEvent e) {
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
boolean result = false;
try {
float diffY = e2.getY() - e1.getY();
float diffX = e2.getX() - e1.getX();
if (Math.abs(diffX) > Math.abs(diffY)) {
if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (diffX > 0) {
onSwipeRight();
} else {
onSwipeLeft();
}
}
result = true;
} else if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
if (diffY > 0) {
onSwipeBottom();
} else {
onSwipeTop();
}
}
result = true;
} catch (Exception exception) {
exception.printStackTrace();
}
return result;
}
}
public void onSwipeRight() {
}
public void onSwipeLeft() {
}
public void onSwipeTop() {
}
public void onSwipeBottom() {
}
}