Android - VerticalSeekBar - onStopTrackingTouch()无效

时间:2014-02-10 20:40:09

标签: java android listener progress seekbar

大家好!我是Android开发的新手,在尝试为搜索栏的拇指设置一个特定位置时我遇到了麻烦。

当用户松开拇指时,我想将搜索栏的进度设置为它的初始位置(在本例中为4),为了做到这一点,我正在尝试使用onStopTrackingTouch方法,但看起来它不是当用户释放搜索栏拇指时被调用。 Eclipse没有指出错误,程序运行正常,唯一的问题是当我松开拇指时,它不会返回到初始位置。

细节:我正在使用Vertical Seek Bar,这可能是问题的原因吗?

这是活动代码:

package renatox.play.bluetoothcommander;

import java.io.IOException;
import java.io.OutputStream;
import java.util.UUID;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.SeekBar;

public class MainActivity extends Activity implements SensorEventListener {

private VerticalSeekBar acelerador;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Sets the Main Activity layout
    setContentView(R.layout.activity_main);

    // Gets the VerticalSeekBar
    acelerador = (VerticalSeekBar) findViewById(R.id.acelerador);

    acelerador
            .setOnSeekBarChangeListener(new VerticalSeekBar.OnSeekBarChangeListener() {
// I've tried using SeekBar.OnSeekBarChangeListener() and VerticalSeekBar.OnSeekBarChangeListener() 

                @Override
                public void onStopTrackingTouch(SeekBar arg0) {
                    acelerador.setProgress(4);
                    Log.i("Acelerador", "O usuário soltou o acelerador");
                }

                @Override
                public void onStartTrackingTouch(SeekBar arg0) {
                    Log.i("Acelerador", "O usuário pegou o acelerador");
                }

                // Pega os valores do acelerador quando mudam
                @Override
                public void onProgressChanged(SeekBar arg0, int arg1,
                        boolean arg2) {
                    int velocidade = acelerador.getProgress();
                    Log.i("Acelerador", "Velocidade atual: " + velocidade);
                }
            });
}

这是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:background="@drawable/fundo"
android:keepScreenOn="true"
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=".MainActivity" >

<renatox.play.bluetoothcommander.VerticalSeekBar
    android:id="@+id/acelerador"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_alignParentRight="true"
    android:layout_marginBottom="30dp"
    android:layout_marginRight="5dp"
    android:layout_marginTop="30dp"
    android:max="8"
    android:maxHeight="0dp"
    android:minHeight="0dp"
    android:progress="4"
    android:progressDrawable="@drawable/progress"
    android:thumb="@drawable/thumb" />

</RelativeLayout>

这是VerticalSeekBar类

package renatox.play.bluetoothcommander;

import android.content.Context;

import android.graphics.Canvas;

import android.util.AttributeSet;

import android.view.MotionEvent;

import android.widget.SeekBar;

public class VerticalSeekBar extends SeekBar {

protected OnSeekBarChangeListener changeListener;

public VerticalSeekBar(Context context) {

    super(context);

}

public VerticalSeekBar(Context context, AttributeSet attrs, int defStyle) {

    super(context, attrs, defStyle);

}

public VerticalSeekBar(Context context, AttributeSet attrs) {

    super(context, attrs);

}

protected void onSizeChanged(int w, int h, int oldw, int oldh) {

    super.onSizeChanged(h, w, oldh, oldw);

}

@Override
protected synchronized void onMeasure(int widthMeasureSpec,
        int heightMeasureSpec) {

    super.onMeasure(heightMeasureSpec, widthMeasureSpec);

    setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());

}

protected void onDraw(Canvas c) {

    c.rotate(-90);

    c.translate(-getHeight(), 0);

    super.onDraw(c);

}

@Override
public boolean onTouchEvent(MotionEvent event) {

    if (!isEnabled()) {

        return false;

    }

    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:

        setSelected(true);
        setPressed(true);
        if (changeListener != null)
            changeListener.onStartTrackingTouch(this);
        break;

    case MotionEvent.ACTION_UP:

        setSelected(false);
        setPressed(false);
        if (changeListener != null)
            changeListener.onStopTrackingTouch(this);
        break;

    case MotionEvent.ACTION_MOVE:

        int progress = getMax()
                - (int) (getMax() * event.getY() / getHeight());
        setProgress(progress);
        onSizeChanged(getWidth(), getHeight(), 0, 0);
        if (changeListener != null)
            changeListener.onProgressChanged(this, progress, true);
        break;

    case MotionEvent.ACTION_CANCEL:
        break;
    }

    return true;

}

}

任何人都可以给我一些可能发生的事情吗?

非常感谢!!

5 个答案:

答案 0 :(得分:2)

以为我会将此作为官方发布&#34;回答&#34;因为它帮助了我的代码。 (Renato Henz回答)

  

我刚刚将其添加到&#34; VerticalSeekBar.java&#34;:@Override public void setOnSeekBarChangeListener(OnSeekBarChangeListener mListener) { this.changeListener = mListener; }如果您需要任何进一步的帮助,请告诉我。祝你的代码好运!

我发现添加此代码以及前面提到的代码可以解决一些问题:

 @Override
 public synchronized void setProgress(int progress) {
      super.setProgress(progress); 
      onSizeChanged(getWidth(), getHeight(), 0, 0);     
 }

答案 1 :(得分:1)

当我在VerticalSeekBar.java中添加aboce setOnSeekBarChangeListner代码时,myControl.setOnseekBarChangeListener代码完全停止工作。即OnProgrressChanged停止 我正在尝试将我的活动中的代码启用Toast fires(只是一个占位符)

@Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            Toast.makeText(APG.this, "Change OD Done", Toast.LENGTH_SHORT).show();

        }

答案 2 :(得分:1)

您好,如果您更改此代码,它将起作用。

            case MotionEvent.ACTION_UP:
             super.onTouchEvent(event);
            // your code here.
            return true;

答案 3 :(得分:0)

您必须在OnSeekBarChangeListener中定义VerticalSeekbar,然后才能运作

在VerticalSeekBar中尝试

private OnSeekBarChangeListener onChangeListener;
    @Override
    public void setOnSeekBarChangeListener(OnSeekBarChangeListener onChangeListener){
        this.onChangeListener = onChangeListener;
    }

答案 4 :(得分:0)

如果您错过了,只需在onTouchEvent中调用。

super.onTouchEvent(event);