使用布局inflater更新搜索栏

时间:2013-01-05 19:33:47

标签: android layout seekbar

我有一个需要从服务更新的搜索栏。但是光标根本不移动。 这是代码(你可以看到很多布局,因为我只需要你需要的代码,但这些布局中还有其他视图对象):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="top"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_alignParentBottom="true"
        android:layout_marginTop="-80dp"
        android:gravity="center_horizontal"
        android:orientation="vertical" >


        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal" >

            <SeekBar
                android:id="@+id/seekBarTime"
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical" />

        </LinearLayout>

    </LinearLayout>

</RelativeLayout>

爪哇

public class MediaPlayerService extends Service implements Runnable {

    private MediaPlayer mediaPlayer = null;
    private SeekBar seekBarTime;
    private Handler handler;
    private Runnable runnable;

    @Override
    public void onCreate() {
        //SET SEEKBAR
        LayoutInflater inflater = ( LayoutInflater ) getSystemService( LAYOUT_INFLATER_SERVICE );
        View layout = inflater.inflate( R.layout.play_song , null);
        seekBarTime = (SeekBar) layout.findViewById(R.id.seekBarTime);
        seekBarTime.setOnSeekBarChangeListener(this);

        handler = new Handler();
        runnable = this;
        handler.post(runnable);
    }

    // Seekbar
    public void run() {     
        if(mediaPlayer.isPlaying()) {


            int currentPosition = 0;
            int total = mediaPlayer.getDuration();
            seekBarTime.setMax(total);
            currentPosition = mediaPlayer.getCurrentPosition();

            Log.w("disgustingapps", currentPosition + " / " + total);

            seekBarTime.setProgress(currentPosition);

        }

        handler.postDelayed(runnable, 1000);
    }
}

logcat向我保证currentPosition和total不为空。然而什么都没发生 你能帮助我吗? 提前致谢

1 个答案:

答案 0 :(得分:0)

它应该工作。我测试了你的run()方法,并在我身边和SeekBar很好地更新了 你在哪里创建并启动MediaPlayer

run()方法正在检查媒体播放器是否正在播放:

if(mediaPlayer.isPlaying()) {
...
}

但我没有看到mediaPlayer().start()