相同的代码不适用于不同的活动

时间:2014-09-13 07:45:50

标签: java android android-activity ontouchevent

我有这个代码,应该在我的一个活动中捕捉滑动事件:

private float x1,x2;
static final int MIN_DISTANCE = 150;
@Override
public boolean onTouchEvent(MotionEvent event)
{
    switch(event.getAction())
    {
        case MotionEvent.ACTION_DOWN:
            x1 = event.getX();
            break;
        case MotionEvent.ACTION_UP:
            x2 = event.getX();
            float deltaX = x2 - x1;

            if (Math.abs(deltaX) > MIN_DISTANCE)
            {
                // Left to Right swipe action
                if (x2 > x1)
                {
                    Toast.makeText(this, "Left to Right swipe [Previous]", Toast.LENGTH_SHORT).show ();
                    this.overridePendingTransition(R.anim.animation_enter, R.anim.animation_leave);

                }

                // Right to left swipe action
                else
                {
                    Toast.makeText(this, "Right to Left swipe [Next]", Toast.LENGTH_SHORT).show ();
                }

            }
            else
            {
                // TO DO
            }
            break;
    }
    return super.onTouchEvent(event);
}

现在我只是在另一个活动中移动了相同的工作代码,当我向左或向右滑动时,它没有显示Toast消息。什么可能导致这个问题?我知道我错过了一些非常小的东西,但我无法在妈妈那里发现它。

编辑非工作活动:

public class BulgarianSayings extends Activity {

    private float x1,x2;
    static final int MIN_DISTANCE = 150;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bulgarian_sayings);
        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }
    }


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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_bulgarian_sayings, container, false);
            return rootView;
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event)
    {
        switch(event.getAction())
        {
            case MotionEvent.ACTION_DOWN:
                x1 = event.getX();
                break;
            case MotionEvent.ACTION_UP:
                x2 = event.getX();
                float deltaX = x2 - x1;

                if (Math.abs(deltaX) > MIN_DISTANCE)
                {
                    // Left to Right swipe action
                    if (x2 > x1)
                    {
                        Toast.makeText(this, "Left to Right swipe [Previous]", Toast.LENGTH_SHORT).show ();
                        this.overridePendingTransition(R.anim.animation_enter, R.anim.animation_leave);

                    }

                    // Right to left swipe action
                    else
                    {
                        Toast.makeText(this, "Right to Left swipe [Next]", Toast.LENGTH_SHORT).show ();
                    }

                }
                else
                {
                    // consider as something else - a screen tap for example
                }
                break;
        }
        return super.onTouchEvent(event);
    }
}

1 个答案:

答案 0 :(得分:1)

您应该在视图上附加onTouchListener以使其正常工作。

例如,如果要在整个视图上实现此功能,可以执行以下操作:

// First make an id for your View (LinearLayout, RelativeLayout, etc.) 
// and then decaler it as a view.
View view = findViewById(R.id.rellayout); 

// then append onTouchListener on it
view.setOnTouchListener(Your OnTouchListener Class or Method);

其他方式是在活动之上这样工作:

public class BulgarianSayings extends Activity implements OnTouchListener {

//Your Other parts