When we do something similar to a personal home page application, we may encounter such a need, the effect is as follows
I believe you should see what the effect is, that is, as the list slides, the transparency of the title bar above will change. In iOS, there are a lot of software that has this effect, and below, let's look at how this effect is achieved.
First look at the directory of the project
We can see that the directory structure is very simple, because I this place is the use of Xlistview instead of ListView, there are a lot of documents are xlistview from the belt, so it appears that more documents, if not used Xlistview, please first Baidu Xlistview see.
In this, we need to focus on only one file, is mainactivity, our key code is here, below, we look at the code implementation
/** * Transparent variable title bar * * @author Zhao Kaiqiang * * @Time 2014-6-20 a.m. 11:46:42/public class Mainactivity extends ACT
Ivity implements Onscrolllistener {private Xlistview listView;
The layout of the title bar is private relativelayout rl_title;
ListView's head layout private View Headerview;
Head layout of highly private int headerheight;
Private Layoutinflater Inflater;
Private Handler Handler = new Handler ();
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Rl_title = (relativelayout) Findviewbyid (r.id.rl_title);
ListView = (Xlistview) Findviewbyid (r.id.list);
Rl_title.getbackground (). Setalpha (0);
Inflater = Layoutinflater.from (this);
Headerview = inflater.inflate (R.layout.header_listview, NULL);
Add header Layout Listview.addheaderview (Headerview);
Listview.setadapter (New Myadapter ());
Set the scrolling monitor listview.setonscrolllistener (this);
Setting can refresh and load more listview.setpullloadenable (true); Listview.setpullrefreshenable(true); Listview.setxlistviewlistener (New Ixlistviewlistener () {@Override public void Onrefresh () {//Pure mock refresh process Handler
. postdelayed (New Runnable () {@Override public void run () {Listview.stoprefresh ();
}, 500); @Override public void Onloadmore () {//Pure simulated loading process handler.postdelayed (new Runnable () {@Override public
void Run () {listview.stoploadmore ();
}, 500);
}
}); ///Custom Adapter private class Myadapter extends Baseadapter {///default display 10 item @Override public int GetCount () {return
10;
@Override public Object getitem (int position) {return position;
@Override public long getitemid (int position) {return position; @Override public View getview (int position, View Convertview, ViewGroup parent) {if (Convertview = null) {CONV
Ertview = inflater.inflate (r.layout.item_list, NULL);
return convertview;
@Override public void onscrollstatechanged (Abslistview view, int scrollstate) {
}///Most important method, the transparency of the title bar changes in this way @Override public void onscroll (Abslistview listView, int firstvisibleitem, int visibl Eitemcount, int totalitemcount) {//To determine whether the header layout is currently displayed at the top, because the Xlistview has a refresh control, so the position of the header layout is 1, that is, the second if (Firstvisibleitem = = 1) {//
Get header layout View view = Listview.getchildat (0);
if (view!= null) {//Get the opposite number of the uppermost position of the header layout now int top =-view.gettop ();
Gets the height of the header layout headerheight = View.getheight (); When this condition is met, the header layout is at the top of the Xlistview first control, only this time we adjust the transparency if (top <= headerheight && top >= 0) {//get current bit
Percentage of the height of the header layout float f = (float) Top/(float) headerheight;
Rl_title.getbackground (). Setalpha ((int) (f * 255));
Notification title bar refresh display Rl_title.invalidate ();
}} else if (Firstvisibleitem > 1) {rl_title.getbackground (). Setalpha (255);
else {rl_title.getbackground (). Setalpha (0);
}
}
}
Is the code not complex? We only need to implement the Onscroll method can, in here, complete our headerview position detection, and then through the Headerview height and display height to calculate the ratio, set to titlebar background picture transparency.
is not very magical, hope everybody likes.