在scrollview上检测滑动

时间:2016-06-23 12:06:47

标签: android scrollview ontouchlistener

我正在尝试使用此代码检测scrollview上的左右滑动。

只检测到leftToRight滑动,我无法进行RightToLeft滑动。  即使滚动滚动视图,它也会检测为leftToRight滑动。

我从中得到了这段代码 http://techin-android.blogspot.in/2011/11/swipe-event-in-android-scrollview.html

public class Programs extends AppCompatActivity implements View.OnTouchListener {

TextView title, desc;

static final int MIN_DISTANCE = 100;
private float downX;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    setContentView(R.layout.program);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    title = (TextView) findViewById(R.id.title_tv);
    desc = (TextView) findViewById(R.id.desc_tv);
    ScrollView s = (ScrollView) findViewById(R.id.content_scroll);
    s.setOnTouchListener(this);

    try {
      //setting title & desc
    } catch (Exception e) {
        e.printStackTrace();
    } 
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // TODO Auto-generated method stub
    try {
        getMenuInflater().inflate(R.menu.settingmenu, menu);
        if (menu != null)
            menu.findItem(R.id.share_menu).setVisible(true);
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // TODO Auto-generated method stub
    switch (item.getItemId()) {
        case R.id.settings:
            Intent setting = new Intent(Programs.this, Settings.class);
            startActivity(setting);
            break;
        case R.id.share_menu:
            try {
                Intent ourIntent = new Intent(Programs.this, Share_app.class);
                ourIntent.putExtra(
                        "send", "\n\n" + title.getText().toString()
                                + "\n\n" + desc.getText().toString());
                startActivity(ourIntent);
            } catch (Exception e) {
                e.printStackTrace();
            }
            break;
        case android.R.id.home:
            this.finish();
            break;
    }
    return true;
}

@Override
public boolean onTouch(View v, MotionEvent event) {
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN: {
            downX = event.getX();
        }
        case MotionEvent.ACTION_UP: {
            float upX = event.getX();

            float deltaX = downX - upX;

            // swipe horizontal?
            if (Math.abs(deltaX) > MIN_DISTANCE) {
                // left or right
                if (deltaX < 0) {
                    this.onLeftToRightSwipe();
                    return true;
                }
                if (deltaX > 0) {
                    this.onRightToLeftSwipe();
                    return true;
                }
            } else {
                Log.d("swipes", "Swipe was only " + Math.abs(deltaX) + " long, need at least " + MIN_DISTANCE);
            }

        }
    }
    return false;
}

public final void onRightToLeftSwipe() {
    Log.d("swipes", "RightToLeftSwipe!");
}

public void onLeftToRightSwipe() {
    Log.d("swipes", "LeftToRightSwipe!");
}
}

任何帮助?谢谢!

0 个答案:

没有答案