Project requirements: Customize TextView for countdown and textview for countdown

Source: Internet
Author: User

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

Related Article

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.