Project requirements: Customize TextView for countdown and textview for countdown
Currently, this requirement is as follows:
I have a ListView. Each list item is displayed in a layout such as a large picture on the top, an audio player 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. Then, I directly create a unique MediaPlayer in the activity to which the ListView belongs. However, this is not a problem, all audio playback conflicts have been resolved.
The problem is that the audio is playing in the middle. I need to click the play button on the left, and there is a time remaining on the right to change. Of course, when I click pause, the remaining time will also be paused.
First, we use the principle of the shopping cart function to implement button operations. Implementation of the Android shopping cart Function
How can the remaining time displayed by TextView in a list item also change?
I found many methods and finally asked a group owner of the Android Communication Group to solve the problem.
How to implement it:
That is, you can customize a TextView to display the remaining time in the list item. Each time you refresh the adapter, you can give the custom TextView an initial value, then, you can customize the TextView to reduce the initial value by one per second and display it on the TextView.
Let's take a look at the 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; // whether to execute the run method 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 ();}}
Key code for calling an adapter:
// 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 (); // The TextView internal thread starts to run} else {// If the audio stops playing holder. list_detail_music_play.stopRun (); // The internal thread of TextView stops running}
The learning content comes from:
Custom countdown Control