我在文本视图中显示的String-array项目中有一些动物名称。我想要的是,当我向左滑动时,字符串数组中的下一个动物名称将显示在文本视图中,当我向右滑动,将显示以前的名称。我可以使用计数器管理左侧滑动事件,但不知道如何管理右侧滑动。
OnSwipeTouchListener.java
public class OnSwipeTouchListener implements OnTouchListener {
protected final GestureDetector gestureDetector;
public OnSwipeTouchListener (Context ctx){
gestureDetector = new GestureDetector(ctx, new GestureListener());
}
private final class GestureListener extends SimpleOnGestureListener {
private static final int SWIPE_THRESHOLD = 100;
private static final int SWIPE_VELOCITY_THRESHOLD = 100;
@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();
}
}
} else {
if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
if (diffY > 0) {
onSwipeBottom();
} else {
onSwipeTop();
}
}
}
} catch (Exception exception) {
exception.printStackTrace();
}
return result;
}
}
public void onSwipeRight() {
}
public void onSwipeLeft() {
}
public void onSwipeTop() {
}
public void onSwipeBottom() {
}
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
// TODO Auto-generated method stub
return false;
}
}
animals.java
public class animals extends Activity {
TextView tv;
int counter = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.animals);
tv= (TextView) findViewById(R.id.tv_animals);
tv.setOnTouchListener(new OnSwipeTouchListener(null) {
public void onSwipeTop() {
Toast.makeText(getApplicationContext(), "top", Toast.LENGTH_SHORT).show();
}
public void onSwipeRight() {
// how to swipe right? Like if 4th animal name is getting displayed and i swipe right , then the 3rd name should be visible
}
public void onSwipeLeft() {
String[] mer = getResources().getStringArray(
R.array.animals);
tv.setText(mer[counter]);
counter++;
}
public void onSwipeBottom() {
Toast.makeText(getApplicationContext(), "bottom", 1000).show();
}
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
});
}
}
答案 0 :(得分:0)
使用此代码:
public class MainActivity extends Activity {
TextView gestureEvent;
ArrayList<String> ana=new ArrayList<String>();
public static int k=0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for(int i=0;i<10;i++)
ana.add(String.valueOf(i));
gestureEvent = (TextView)findViewById(R.id.gesture);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
return gestureDetector.onTouchEvent(event);
}
SimpleOnGestureListener simpleOnGestureListener
= new SimpleOnGestureListener(){
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
String swipe = "";
float sensitvity = 50;
// TODO Auto-generated method stub
if((e1.getX() - e2.getX()) > sensitvity){
swipe += ana.get(k++);
}else if((e2.getX() - e1.getX()) > sensitvity){
swipe += ana.get(k--);
}else{
swipe += "\n";
}
if((e1.getY() - e2.getY()) > sensitvity){
swipe += ana.get(k++);
}else if((e2.getY() - e1.getY()) > sensitvity){
swipe += ana.get(k--);
}else{
swipe += "\n";
}
gestureEvent.setText(swipe);
return super.onFling(e1, e2, velocityX, velocityY);
}
};
GestureDetector gestureDetector
= new GestureDetector(simpleOnGestureListener);
}