向左/向右滑动不起作用

时间:2013-11-25 22:32:19

标签: android swipe gesture

我正在尝试左右滑动,发送一个吐司,确认滑动的方向。我在网上找到了一个关于如何做到这一点的简单代码,它只与一个文本视图完美配合。然而,当我尝试将这个想法实现到我一直在研究的项目中时,我在滑动屏幕时没有得到任何结果。下面的许多代码可能与问题无关,但我决定显示我的整个代码以防万一。谢谢。

   package com.example.final_project;

   import java.util.Map;


import android.app.Activity;
import android.app.ActivityOptions;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

    Button B1,B2;
    EditText Text1;
    String Text;
    String Preference;
    String RestarauntChoices= "https://maps.google.com/maps?q=";
    private GestureDetector gestureDetector;
    Double Latitude, Longitude;
    public static String MY_PREFS = "finalproject.my_prefs";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        gestureDetector = new GestureDetector(
                new SwipeGestureDetector());        
        Text1=(EditText)findViewById(R.id.editText1);

    }

      @Override
      public boolean onTouchEvent(MotionEvent event) {
        if (gestureDetector.onTouchEvent(event)) {
          return true;
        }
        return super.onTouchEvent(event);
      }

      private void onLeftSwipe() {
        Toast.makeText(getApplicationContext(), "Left", Toast.LENGTH_LONG).show();
      }

      private void onRightSwipe() {
            Toast.makeText(getApplicationContext(), "Right", Toast.LENGTH_LONG).show();
      }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        /*
        //Must Be Ran on a Device. If using Emulator it crashes
        //Due to lack of Google Maps Application
        if(item.getItemId() == R.id.menu_mapLocation){
        String uriBegin = "geo:" + Latitude + "," + Longitude;
        String query = Latitude + "," + Longitude;
        String encodedQuery = Uri.encode(query);
        String uriString = uriBegin + "?q=" + encodedQuery + "&z=16";
        Uri uri = Uri.parse(uriString);
        Intent intent =new Intent(android.content.Intent.ACTION_VIEW, uri); 
        startActivity(intent); 
        }
        */
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu){
    /*
        menu.clear();
        getMenuInflater().inflate(R.menu.main, menu);
        SharedPreferences prefs = getSharedPreferences(MY_PREFS, Activity.MODE_PRIVATE);

        for(Map.Entry<String,?> entry : prefs.getAll().entrySet()){
        menu.add(entry.getValue().toString());
        }
    */
        return super.onPrepareOptionsMenu(menu);
    } 

    public void GpsClick(View view){
    /*
        LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        LocationListener mlocListener = new MyLocationListener();
        mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 10000, 0, mlocListener);
        Toast.makeText(getApplicationContext(), Text, Toast.LENGTH_SHORT).show();
    */
    }

    public void FindPartyClick(View view){

/*      
        Preference=Text1.getText().toString();
        SharedPreferences prefs = getSharedPreferences(MY_PREFS, Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putString(Preference, Preference);
        editor.commit();


        Intent intent = new Intent(MainActivity.this, SearchClass.class); 
        String Address=Text1.getText().toString();
        if(Address.length()!=5 && Address.matches("\\d+"))
            Toast.makeText( getApplicationContext(),"Zip Code must be 5 digits" , Toast.LENGTH_SHORT ).show();
        else{
        intent.putExtra("message", Address);
        Bundle bndlanimation = ActivityOptions.makeCustomAnimation(getApplicationContext(), R.anim.animation,R.anim.animation2).toBundle();
        startActivity(intent, bndlanimation);
        }
        //startActivity(intent);
 */
}

      // Private class for gestures
      private class SwipeGestureDetector 
              extends SimpleOnGestureListener {
        // Swipe properties, you can change it to make the swipe 
        // longer or shorter and speed
        private static final int SWIPE_MIN_DISTANCE = 120;
        private static final int SWIPE_MAX_OFF_PATH = 200;
        private static final int SWIPE_THRESHOLD_VELOCITY = 200;

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2,
                             float velocityX, float velocityY) {
          try {
            float diffAbs = Math.abs(e1.getY() - e2.getY());
            float diff = e1.getX() - e2.getX();

            if (diffAbs > SWIPE_MAX_OFF_PATH)
              return false;

            // Left swipe
            if (diff > SWIPE_MIN_DISTANCE
            && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
               MainActivity.this.onLeftSwipe();

            // Right swipe
            } else if (-diff > SWIPE_MIN_DISTANCE
            && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
              MainActivity.this.onRightSwipe();
            }
          } catch (Exception e) {
            Log.e("YourActivity", "Error on gestures");
          }
          return false;
        }
      }


/* Class My Location Listener */
/*
    public class MyLocationListener implements LocationListener{

    @Override

        public void onLocationChanged(Location loc){
            loc.getLatitude();
            loc.getLongitude();
            setGPS(loc.getLatitude(),loc.getLongitude());
        }

        public String setGPS(double Lat, double Long){
            Latitude=Lat;
            Longitude=Long;
            Text = "My current location is: " + "Latitude = " + Lat + "Longitude = " + Long;
            return Text;
        }

        @Override
        public void onProviderDisabled(String provider)
        {
        Toast.makeText( getApplicationContext(),"Gps Disabled" , Toast.LENGTH_SHORT ).show();

        }

        @Override
        public void onProviderEnabled(String provider)
        {
        Toast.makeText( getApplicationContext(), "Gps Enabled", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {

        }


    }/* End of Class MyLocationListener */


}/* End of Main Activity */

不同文件中的类

package com.example.final_project;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;

public class SearchClass extends Activity{

    String msg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.searchpage);
        Intent intent2 = getIntent();
        msg = intent2.getStringExtra("message");

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


    public void onResume(){
        super.onResume();
    }

}

2 个答案:

答案 0 :(得分:0)

将OnTouchListener设置为Activity可能不是一个好主意,但如果您基本上只想使用整个屏幕来捕捉滑动,have a look at this

答案 1 :(得分:0)

所以问题是我有一个滚动视图。当我关闭ScrollView时,滑动工作......开到下一个问题。