Android to achieve listview sliding fade the top bar instance code _android

Source: Internet
Author: User

I was in the development of the time encountered such a demand, that is, in the ListView slide, need to the top of the column from transparent slowly to become opaque state, is the following effect

The first time to think very simple, nothing more than listening to the listview of the sliding distance, and then based on the distance to calculate the ratio of transparency, it can be, but in fact it is indeed done

It may be impossible to get direct access to the sliding distance of the ListView, and it needs to be dynamically computed.

The following posted all the code, do not want to code word, the recent cold, head dizzy, but also pain, the code more intuitive some

private void Initlistener () {Lvlist.setonscrollchangelistener (new View.onscrollchangelistener () {@Override public vo 
ID Onscrollchange (View v, int scrollx, int scrolly, int oldscrollx, int oldscrolly) {}}); Lvlist.setonscrolllistener (New Abslistview.onscrolllistener () {@Override public void onscrollstatechanged ( Abslistview view, int scrollstate) {} @Override public void onscroll (Abslistview view, int firstvisibleitem, int visib Leitemcount, int totalitemcount) {//Logger.mylog ("firstvisibleitem=" + Firstvisibleitem + "visibleitemcount=" + Visible 
ItemCount + "totalitemcount=" + totalitemcount); 
Logger.mylog (Getscrooly () + "top=" + gettop ()); 
Tvtext.setalpha (Getalphafloat (Getscrooly ())); 
} 
}); 
/** * Get Gradient transparent value * @param dis * @return/public float getalphafloat (int dis) {int step = 5000; 
if (dis = = 0) {return 0.0f; 
if (Dis < step) {return (float) (DIS * (1.0/step)); 
}else{return 1.0f; }/** * Get up-sliding distance * @return distance/public int GetscrooLY () {View c = lvlist.getchildat (0); 
if (null = = c) {return 0; 
int firstvisibleposition = Lvlist.getfirstvisibleposition (); 
int top = C.gettop (); /** * Declare that the top value here is always the distance of the first distance shown in the ListView entry, * and this representation in the coordinates is a negative number, so you need to take an absolute value for IT/return firstvisibleposition * C.G 
Etheight () + math.abs (top); 
/** * Get the height of the first distance * @return distance top/private int gettop () {View c = lvlist.getchildat (0); 
if (null = = c) {return 0; 
return C.gettop ();  }

The above is a small set of Android to introduce the implementation of ListView sliding fade the top column instance code, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.