Pergunta

So I am trying to implement a gesture on Fling feature on this page. I have already used gestures, but for some reason this code will not run. I do get a warning, but I do not know how to fix it, it says that the variable gesture is not used within the code, although you can clearly see it in the onCreate. Help?

Here is the code:

    package com.example.finisher;

    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    import java.io.InputStream;

    import android.app.Activity;
    import android.content.Intent;
    import android.content.res.AssetManager;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.GestureDetector;
    import android.view.GestureDetector.OnGestureListener;
    import android.view.MotionEvent;
    import android.widget.TextView;

    public class newclass extends Activity implements OnGestureListener{

private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
private GestureDetector gesture;

@Override 
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    TextView textView=new TextView(this);
    setContentView(textView);
    gesture = new GestureDetector(this);

    AssetManager assetManager = getAssets();
    InputStream inputStream =null;
    try {
        inputStream = assetManager.open("texts/myawesometext.txt");
        String text = loadTextFile(inputStream);
        textView.setText(text);
    } catch(IOException e){
        textView.setText("Couldn't Load the file.");
    }
    finally{
        if (inputStream!=null){
            try{
                inputStream.close();
            } catch(IOException e){
                textView.setText("Couldn't Close the File");
            }
        }
    }
}

private String loadTextFile(InputStream inputStream)throws IOException {
    ByteArrayOutputStream bytestream=new ByteArrayOutputStream();
    byte[] bytes= new byte[4096];
    int len=0;
    while((len= inputStream.read(bytes))>0)
        bytestream.write(bytes, 0, len);
    return new String(bytestream.toByteArray(),"UTF8");
}

@Override
public boolean onDown(MotionEvent arg0) {
    // TODO Auto-generated method stub
    return false;
}

@Override
public boolean onFling(MotionEvent start, MotionEvent finish, float xv,
        float yv) {
    Log.v("msg","msg");
    //Y motion only
    if(Math.abs(start.getRawY()-finish.getRawY())>SWIPE_MIN_DISTANCE
            &&Math.abs(yv)>SWIPE_THRESHOLD_VELOCITY){
        //y motion
        if (start.getRawY()>finish.getRawY()){
            //gesture down, page from top
            Intent myIntent = new Intent(newclass.this,MainActivity.class);
            myIntent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
            startActivity(myIntent);
            //animation done in pairs.Bringing top view in
            this.overridePendingTransition(R.anim.slide_bottom_in,
            R.anim.slide_bottom_out);  
        }
    }
return false;
}

@Override
public void onLongPress(MotionEvent arg0) {
    // TODO Auto-generated method stub

}

@Override
public boolean onScroll(MotionEvent arg0, MotionEvent arg1, float arg2,
        float arg3) {
    // TODO Auto-generated method stub
    return false;
}

@Override
public void onShowPress(MotionEvent arg0) {
    // TODO Auto-generated method stub

}

@Override
public boolean onSingleTapUp(MotionEvent arg0) {
    // TODO Auto-generated method stub
    return false;
}


    }
Foi útil?

Solução

is seems to me it's initialized in the onCreate(), but never used. look at this post Fling gesture detection on grid layout

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top