Android Project Combat (10): Custom Countdown TextView

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.