1

所以我试图在这个页面上实现一个关于 Fling 功能的手势。我已经使用过手势,但由于某种原因,这段代码无法运行。我确实收到了一个警告,但我不知道如何修复它,它表示代码中没有使用可变手势,尽管您可以在 onCreate.xml 中清楚地看到它。帮助?

这是代码:

    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;
}


    }
4

1 回答 1

0

在我看来,它是在 onCreate() 中初始化的,但从未使用过。看这篇文章在网格布局上进行 Fling 手势检测

于 2013-01-24T20:50:52.847 回答