OnTouchListener没有被解雇

时间:2013-08-26 14:57:17

标签: java android eclipse gesture ontouchlistener

请查看以下代码

XML代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".ParagraphReader" >

    <ScrollView
        android:id="@+id/paragraph_reader_scroll_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <TextView
            android:id="@+id/paragraph_reader_txt"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="30"
            android:singleLine="false"
            android:enabled="true"
            >


        </TextView>

    </ScrollView>






</RelativeLayout>

Java代码

package k.k;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View.OnTouchListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class ParagraphReader extends Activity {

    private TextView paraText;
    private DatabaseConnector database;
    private List<String>paraList;
    private int currentQuestion;

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

        paraText = (TextView)findViewById(R.id.paragraph_reader_txt);
        paraText.setOnTouchListener(paraSwiped);

        paraList = new ArrayList<String>();
        database = DatabaseHandler.getInstance(this);

        //Get the Paragraph list
        int listNumber = getIntent().getIntExtra("PARAGRAPH_LIST", 0);

        Toast.makeText(this, "Selected Paragraph: "+listNumber, Toast.LENGTH_LONG).show();

        paraList = database.getParagraphList(listNumber);
        Toast.makeText(this, "ParaList size "+paraList.size(), Toast.LENGTH_LONG).show();

        //Toast.makeText(this, "Size: "+paraList.size(), Toast.LENGTH_LONG).show();
        paraText.setText(paraList.get(0));

    }

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


    //The Event Handler for the Paragraph Text holder
    OnTouchListener paraSwiped = new OnSwipeTouchListener()
    {
        public boolean onSwipeRight() 
         {
            Toast.makeText(ParagraphReader.this, "Right: "+paraList.size(), Toast.LENGTH_SHORT).show();


            return true;
         }

          public boolean onSwipeLeft() 
          {
              Toast.makeText(ParagraphReader.this, "Left: "+paraList.size(), Toast.LENGTH_SHORT).show();

                return true;
          }


    };

}

在这里,您可以看到我已向OnTouchListener实施了TextView。以下是OnTouchListener类的代码。此代码由SO Member之一构建。

package k.k;
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 = new GestureDetector(new GestureListener());

    public boolean onTouch(final View v, final MotionEvent event) {
        return gestureDetector.onTouchEvent(event);

    }

    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 super.onDown(e);
        }

        @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) {
                            result = onSwipeRight();
                        } else {
                            result = onSwipeLeft();
                        }
                    }
                } else {
                    if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
                        if (diffY > 0) {
                            result = onSwipeBottom();
                        } else {
                            result = onSwipeTop();
                        }
                    }
                }
            } catch (Exception exception) {
                exception.printStackTrace();
            }
            return result;
        }
    }

    public boolean onSwipeRight() {
        return false;
    }

    public boolean onSwipeLeft() {
        return false;
    }

    public boolean onSwipeTop() {
        return false;
    }

    public boolean onSwipeBottom() {
        return false;
    }
}

我只需要左右滑动,但由于某种原因,滑动不起作用。是的,它不起作用,这意味着什么也没发生。我不明白如何纠正这个问题。我知道SO成员开发的代码正在运行,因为我在另一个活动中使用它。

1 个答案:

答案 0 :(得分:1)

您可以尝试使用android:clickable =“true”使文本视图可单击。不确定这会解决问题,但值得一试。我不确定android启用了什么,所以它可能会做同样的事情