Original: Android Project Combat (10): Custom Countdown TextView
Project Summary
--------------------------------------------------------------------------------------------
There is a need for:
I have a ListView, every list item layout such as, top a big picture show, middle an audio play, the bottom of a descriptive text.
In this interface, you can only click on the play button of a list item at a time, then I create a unique mediaplayer directly from the activity that the ListView belongs to, however, this is not a problem, all audio aspects of the playback conflict are resolved
The problem is in the middle of the audio playback, I need to click on the left side of the play button, and then there is a time remaining on the right to follow the change, of course, when the pause, the remaining time to pause
First, the use of the shopping cart function of the principle of implementation of the button operation. The implementation of the Android shopping cart function
How do you implement a list item in which the remaining time of the TextView display changes as well?
Looking for a lot of methods, finally asked a group of Android communication group to solve.
How to achieve it:
That is, customize a textview in the list items to show the remaining time, as long as each time you refresh the adapter, give this custom textview an initial value, and then TextView can self-customize to reduce the initial value per second, shown on TextView.
So look at this custom TextView
Import Android.content.context;import Android.util.attributeset;import android.util.log;import Android.widget.TextView; Public classShowtiemtextview extends TextView implements runnable{PrivateBoolean run =false;//feel whether to execute the Run methodPrivate intTime ; PublicShowtiemtextview (Context context) {super (context); } PublicShowtiemtextview (Context context, AttributeSet Attrs) {Super (context, attrs); } Public voidSetTime (intTime ) {//Set initial value This. Time =Time ; } PublicBoolean Isrun () {returnrun; } Public voidBeginrun () { This. Run =true; Run (); } Public voidStoprun () { This. Run =false; } @Override Public voidrun () {if(Run) {computetime (); This. SetText (Time/ -+"'"+ Time% -);postdelayed ( This, +); }Else{removecallbacks ( This); } } Private voidComputetime () { 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 audio is playing Holder.list_detail_music_play.beginRun (); TextView internal thread starts running}else{ //If audio stops playing Holder.list_detail_music_play.stopRun (); TextView internal thread stops running}
Learning content from:
Custom Countdown Control
Android Project Combat (10): Custom Countdown TextView