In mobile phone development, we will encounter such a problem: we need to display a lot of information on the mobile phone screen, but the mobile phone screen is a little small. How can we display it when there is a lot of content, how can we use a limited space to display more information? We can use the slidingdrawer class provided by the Android system. With the slidingdrawer class, we can use slidingdrawer to achieve drawer effects. This is the legendary Android drawer effect. See the reference code below, the following code has been tested on this site. You can copy it to the Eclipse project to run it. Paste the code below.
First, let's take a look at the running effect:
To create a project, prepare several images as follows:
1.handle_normal.png
Download
2.handle_focused.png
Download
3.handle_pressed.png
Download
4.list_selector_background_pressed.png
Download
The slices are all placed in drawable-hdpi/drawable-ldpi/drawable-mdpi/drawable-xhdpi.
Then let's look at how to write the code:
1. Define a resource file handle. XML in the ES/drawable directory. The reference code is as follows:
<? XML version = "1.0" encoding = "UTF-8"?>
<Selector xmlns: Android = http://schemas.android.com/apk/res/android>
<Item Android: state_window_focused = "false" Android: drawable = "@ drawable/handle_normal"/>
<Item Android: state_focused = "true" Android: drawable = "@ drawable/handle_focused"/>
<Item Android: state_pressed = "true" Android: drawable = "@ drawable/handle_pressed"/>
</Selector>
2. Define a resource file listview_selected.xml In the ES/drawable directory. The reference code is as follows:
<? XML version = "1.0" encoding = "UTF-8"?>
<Selector xmlns: Android = http://schemas.android.com/apk/res/android>
<Item Android: state_pressed = "true"
Android: drawable = "@ drawable/list_selector_background_pressed"/>
</Selector>
3. Reference code in the main. xml file:
<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android = http://schemas.android.com/apk/res/android
Android: Orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<Slidingdrawer
Android: Id = "@ + ID/slidingdrawer"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: Orientation = "horizontal"
Android: handle = "@ + ID/handle"
Android: content = "@ + ID/content">
<Button
Android: Id = "@ + ID/handle"
Android: layout_width = "wrap_content"
Android: layout_height = "fill_parent"
Android: Background = "@ drawable/handle"/>
<Listview
Android: Id = "@ + ID/content"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"/>
</Slidingdrawer>
</Linearlayout>
4. Create listview_layout.xml In the Res/layout file. For the file, refer to the Code:
<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android = http://schemas.android.com/apk/res/android
Android: Orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: Background = "# ffffff">
<Linearlayout
Android: Orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: Background = "@ drawable/listview_selected"
Android: padding = "6px">
<Textview
Android: Id = "@ + ID/webname"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: textsize = "20px"
Android: textcolor = "#000000" type = "codeph" text = "/codeph"/>
<Textview
Android: Id = "@ + ID/webdescript"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: textsize = "16px"
Android: textcolor = "#000000" type = "codeph" text = "/codeph"/>
</Linearlayout>
</Linearlayout>
5. Reference code corresponding to the Java file (in this example, androidslidingdraweractivity. Java:
Public class androidslidingdraweractivity extends activity {
Private listview mylistview;
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Setupviews ();
}
Private void setupviews (){
Mylistview = (listview) findviewbyid (R. Id. content );
Mylistview. setadapter (New listviewadapter ());
}
Private class listviewadapter extends baseadapter {
// 10 rows are returned. The number of rows in the listview depends on the getcount () method.
@ Override
Public int getcount (){
Return 10;
}
@ Override
Public object getitem (INT arg0 ){
Return NULL;
}
@ Override
Public long getitemid (INT arg0 ){
Return 0;
}
@ Override
Public View getview (INT position, view V, viewgroup parent ){
Final layoutinflater Inflater = layoutinflater. From (getapplicationcontext ());
If (V = NULL ){
V = Inflater. Inflate (R. layout. listview_layout, null );
}
Textview mywebname = (textview) v. findviewbyid (R. Id. webname );
Textview mywebdescript = (textview) v. findviewbyid (R. Id. webdescript );
Mywebname. settext ("android Development Learning Network" + position );
Mywebdescript. settext ("slidingdrawer Usage Details" + position );
Return V;
}
}
}
Run the command to see the effect.