Android project combat (10): TextView with custom countdown

Original: Android project combat (10): TextView with custom countdown

Project summary

--------------------------------------------------------------------------------------------

There is such a requirement:

I have a ListView, each list item is laid out as shown above, with a large picture display at the top, an audio playback in the middle, and a description text at the bottom.

In this interface, you can only click the play button of one list item at a time, so I directly create a unique MediaPlayer in the activity to which the ListView belongs. However, this is not a problem, and all audio playback conflicts are resolved.

 

The problem is the audio playback in the middle. I need to click the play button on the left, and then there is a remaining time on the right to follow the change. Of course, when I click pause, the remaining time should also be paused.

First of all, the principle of the shopping cart function is used here to realize the operation of the button. Implementation of Android shopping cart function

How to realize the remaining time displayed by the TextView in a certain list item also changes?

After looking for a lot of methods, I finally asked the owner of an Android communication group to solve it.

How to achieve it:

That is, to customize a TextView to display the remaining time in the list item, as long as each time the adapter is refreshed, give the custom TextView an initial value, and then the TextView can customize the initial value by one per second and display it in the Above the TextView.

 

Then look at this custom TextView

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.TextView;


public class ShowTiemTextView extends TextView implements Runnable{

    private boolean run = false ; //Do you think the run method is executed
     private  int time;         
 public ShowTiemTextView(Context context) {
        super(context);
    }

    public ShowTiemTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public  void setTime( int time){ //Set the initial value
         this .time = time;  
    }
    public boolean isRun(){
        return run;
    }
    public void beginRun(){
        this.run = true;
        run();
    }
    public  void stopRun () {
         this .run = false ;
    }
    @Override
        public void run() {
        if (run){
            ComputeTime();
            this.setText(time / 60 + "'" + time % 60);
            postDelayed(this, 1000);
        }else{
            removeCallbacks(this);
        }
    }

    private void ComputeTime(){
        time--;
        if (time==0)
            stopRun ();
    }
}

The adapter calls the key code:

// holder.list_detail_music_play is a TextView
holder.list_detail_music_play.setTime(audiolength);
 if ( list.get (position).isPlaying()){ //If the audio is playing
     holder.list_detail_music_play.beginRun(); //TextView internal thread starts running
} else { //If the audio stops playing
     holder.list_detail_music_play.stopRun(); //TextView internal thread stops running
}

 

Learning content from:

Custom countdown control

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325033989&siteId=291194637