Learning must start from the foundation, only have a good foundation, we will become more perfect
The following small series will use tabhost production QQ client tab Bar effect (this version of QQ was released in previous years) ....
First we look at the effect:
See this interface, an instant a little joy, but for us to learn the program, theUI is the one hand, the code is also the aspect, today is about the code, so we ignore the UI here
--------------------------------------------------------Ornate Split Line---------------------------------------
Analysis
1) The interface contains a navigation bar (this is called the tab bar), each column contains a page with a ListView
2) The item in theListView is a custom layout
3) Custom Adapter
4) Transfer of information using Intent
5) Use tabhost to switch between pages
Code
Note: Because the content style of three pages is basically the same, so the small series is to take the friend page to tell
1) Create a activity_main(main layout)
<tabhost xmlns:android= "Http://schemas.android.com/apk/res/android" xmlns:tools= "Http://schemas.android.com/tools" android:id= "@android: Id/tabhost" android:layout_width= "Match_ Parent "android:layout_height=" Match_parent "> <!--definition tabhost label--<linearlayout Android:layout_ Width= "Fill_parent" android:layout_height= "fill_parent" android:orientation= "vertical" > <!--define Ta Bweidget Control--<tabwidget android:id= "@android: Id/tabs" android:layout_width= "Fill_paren T "android:layout_height=" wrap_content "/> <!--definition Framlayout Controls--<framelayout Android:id= "@android: Id/tabcontent" android:layout_width= "fill_parent" android:layout_height= "fill _parent "android:padding=" 5DP "/> </linearlayout></tabhost>
2) Create a activity_myfriend, which has a ListView -composed interface, which eliminates
3) Create a custom style Activity_myfriend_item
<?xml version= "1.0" encoding= "Utf-8"?> <!--define the item style of the ListView--><linearlayout xmlns:android= "/http/ Schemas.android.com/apk/res/android "android:layout_width=" match_parent "android:layout_height=" Match_parent "and roid:orientation= "Horizontal" > <!--define arrows picture--<imageview android:id= "@+id/iv" android:layou T_width= "Wrap_content" android:layout_height= "Match_parent"/> <!--define a buddy text control--<textview a Ndroid:id= "@+id/tvtitle" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" and Roid:gravity= "left" android:layout_marginleft= "10dip" android:textcolor= "@color/material_blue_grey_800" Android:textsize= "20sp"/> <!--a text control that defines the number of friends--<textview android:id= "@+id/tvinfo" Android : layout_width= "match_parent" android:layout_height= "wrap_content" android:gravity= "right" Android:tex Tcolor= "@color/material_blue_grey_800 "android:textsize=" 10SP "/></linearlayout>
4) The other two pages are basically the same as this one
5) Create an entity class
Package cn.edu.bzu.entity;/** * Created by monster on 2015/4/15. */public class Myfriend {public String Name; public int imgid; Public String Info; Public Myfriend () { super (); } Public Myfriend (string name, int imgid, string info) { super (); name = name; Imgid = Imgid; info = info; }}
PS: the entity class here is primarily used as a custom adapter
6) Custom Adapter (Focus)
Custom adapters are a focus of the ListView
create
Package Cn.edu.bzu.adapter;import Android.content.context;import Android.media.image;import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;import Android.widget.baseadapter;import Android.widget.imageview;import Android.widget.textview;import Java.util.arraylist;import Java.util.list;import Cn.edu.bzu.TabHost_QQ. R;import cn.edu.bzu.entity.myfriend;/** * Created by monster on 2015/4/15. */public class Myfriendlistadapter extends Baseadapter {private context context; Private List<myfriend>list=new arraylist<myfriend> (); Public Myfriendlistadapter (context context, list<myfriend> List) {this.context = context; This.list = list; } class viewholder{ImageView Image; TextView Name; TextView Info; } @Override public int getcount () {return list.size (); } @Override public Object getItem (int position) {return list.get (position); } @Override Public Long Getitemid (int position) {return position; } @Override public View getView (int position, view Convertview, ViewGroup parent) {Viewholder viewholder; if (convertview==null) {//Initialize friend's item view convertview= layoutinflater.from (context). Inflate (R. Layout.activity_myfriend_item,null); Viewholder=new Viewholder (); Gets the object control in the View viewholder.image= (ImageView) Convertview.findviewbyid (R.ID.IV); Viewholder.name= (TextView) Convertview.findviewbyid (r.id.tvtitle); viewholder.info= (TextView) Convertview.findviewbyid (r.id.tvinfo); Convertview.settag (Viewholder); }else{viewholder= (Viewholder) Convertview.gettag (); }//Set control Properties ViewHolder.Image.setBackgroundResource (List.get (position). Imgid); ViewHolder.Name.setText (List.get (position). Name); ViewHolder.Info.setText (List.get (position). Info); return convertview; }}
Package Cn.edu.bzu.tabhost_qq;import Android.app.activity;import Android.os.bundle;import Android.widget.ListView; Import Java.util.arraylist;import Java.util.list;import Cn.edu.bzu.adapter.myfriendlistadapter;import cn.edu.bzu.entity.myfriend;/** * Created by monster on 2015/4/15. */public class Myfriendactivity extends Activity {private ListView lvfriend; Private List<myfriend>list=new arraylist<myfriend> (); Private Myfriendlistadapter adapter; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_myfriend); GetData (); Data source lvfriend= (ListView) Findviewbyid (r.id.lvfriend); Adapter=new Myfriendlistadapter (myfriendactivity.this,list); Lvfriend.setadapter (adapter); } private void GetData () {List.add (New Myfriend ("Online Friends", R.mipmap.arrow, "10")); List.add (New Myfriend ("classmate Friend", R.mipmap.arrow, "22")); List.add (New Myfriend ("Working friends ", R.mipmap.arrow," 12 ")); }}
Package Cn.edu.bzu.tabhost_qq;import Android.app.tabactivity;import Android.content.intent;import Android.os.bundle;import Android.widget.tabhost;public class Mainactivity extends tabactivity{@Override protected V OID onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Tabhost tabhost=gettabhost ();//Call the Tabhost object in the current activity tabhost.tabspec spec;//definition tabhost Tabspec object Intent i; /* * Set the first tab layout */i=new Intent (Mainactivity.this,myfriendactivity.class); Spec=tabhost.newtabspec ("0"). Setindicator ("Friends"). SetContent (i); Tabhost.addtab (spec); /* * Set a second tab layout */i=new Intent (Mainactivity.this,mygroupactivity.class); Spec=tabhost.newtabspec ("1"). Setindicator ("group"). SetContent (i); Tabhost.addtab (spec); /* * Set a third tab layout */i=new Intent (Mainactivity.this,mydiscussionactivity.class); Spec=tabhost.newtabspec ("2"). Setindicator ("discussion group"). SetContent (i); Tabhost.addtab (spec); Tabhost.setcurrenttab (0);//Set Current TAB}}
9) Register Activity in androidmanifest.xml
10) Gaocheng
---------------------------------------------Ornate Split Line------------------------------------------------
PS: This tutorial we need to know:
1) Use oftabhost , how to use tabhost to switch pages
2) Customizing the Item view of the ListView
3) How to write a custom adapter by inheriting Baseadapter
4) How to get a data source in a custom adapter
5) Meaning of custom adapter methods
6) Use of custom adapters and entity classes
Use Tabhost to make QQ client tab bar effect (Low version QQ)