The results we are going to achieve today are as follows:
Of course, the number of labels in the list is controllable, and we need to customize the view to achieve a manageable number of tags.
We customize an abstract class to inherit linearlayout and implement the interfaces we define:
Basetagview:
/** * tag, inheriting this abstract class * @author Linhai Gu * * @param <T> */public abstract class Basetagview<t> extends LinearLayout Implements Selecttag<t>{private context Mcontext;public Basetagview (context context, AttributeSet attrs, int Defstyle) {Super (context, attrs, defstyle); mcontext = Context;initview ();} Public Basetagview (context context, AttributeSet Attrs) {Super (context, attrs); mcontext = Context;initview ();} Public Basetagview (Context context) {super (context); mcontext = Context;initview ();} private void Initview () {setorientation (linearlayout.horizontal);} /** * Add tag with * @param datas data source * @param num Displays the number of labels */public void Addtag (list<t> datas,int num) {removeallviews (); L Inearlayout.layoutparams params = new Linearlayout.layoutparams (LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);p arams.setmargins (0, 0, 0), if (datas.size () > num) {for (int i = 0; i < Num i++) {T data = Datas.get (i); TextView TextView = (TextView) Layoutinflater.froM (mcontext). Inflate (R.layout.tagtext_view, null); Textview.settext (Gettag (data)); Textview.setlayoutparams (params ); AddView (TextView);}} else {for (int i = 0; i < datas.size (); i++) {T data = Datas.get (i); TextView TextView = (TextView) layoutinflater.from (Mcontext). Inflate (R.layout.tagtext_view, null); Textview.settext ( Gettag (data)); Textview.setlayoutparams (params); AddView (TextView);}}}
Define the interface Selecttag:
/** * * @author Linhai Gu * * @param <T> */public interface selecttag<t> {/** * get tag Contents * @param data * @re Turn */public abstract String gettag (T data);}
Here's why generics are used, because what we need is a common control, not one that is written for an interface.
you need to import a TextView layout file in the Addtag method in the Basetagview abstract class:
Tagtext_view:
<?xml version= "1.0" encoding= "Utf-8"? ><textview xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" match_parent " android:layout_height=" Match_parent " android: background= "@drawable/text_line" android:textsize= "12DP" android:textcolor= "#999999" android: padding= "2DP" ></TextView>
Text_line: Is a border that is specially labeled to TextView outside, of course, you can make some good-looking label styles as needed
<?xml version= "1.0" encoding= "Utf-8"? ><shape xmlns:android= "Http://schemas.android.com/apk/res/android" > <solid android:color= "@android: Color/transparent"/> <corners android:topleftradius= "1DP" android:toprightradius= "1DP" android:bottomrightradius= "1DP" android:bottomleftradius= "1DP"/> <stroke android:width= "1DP" android:color= "#999999"/> </shape>
Above our common control is basically done.
At this point, suppose you have a project. The first page gets a list of labels from the server and displays them in a list, with the values of the labels stored in a PageObject object:
PageObject:
/** * Suppose this is the tag data requested by a page * @author Linhai Gu * */public class PageObject implements serializable{private static final long SE Rialversionuid = 100230230204204021l;public String tagName;}
Then we're going to show these tags, so we can create different tags for different modules, assuming that the first interface has a label view like this:
public class Pageonetagview extends Basetagview<pageobject>{public Pageonetagview (context context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle);} Public Pageonetagview (context context, AttributeSet Attrs) {Super (context, attrs);} Public Pageonetagview (Context context) {super (context);} public void settaglist (arraylist<pageobject> list,int num) {addtag (list,num);} @Overridepublic String Gettag (pageobject data) {return data.tagname;}}
The subclass definition Settaglist method passes in the list of tags we want to display and displays the number of rows.
Here's what makes our main layout:
Mainactivity:
public class Mainactivity extends Activity {private datapter datapter=new datapter ();p rivate arraylist<pageobject > List=new arraylist<pageobject> ();p rivate ListView lv_tag; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); InitData (); Initview ();} /** * Assume that this is the list of tags obtained from the service */private void InitData () {for (int i=0;i<10;i++) {pageobject obj=new pageobject (); obj.tagname= "Label" +i;list.add (obj);}} private void Initview () {lv_tag= (ListView) Findviewbyid (r.id.lv_list); Lv_tag.setadapter (Datapter);} Class Datapter extends Baseadapter {@Overridepublic int getcount () {return 23;} @Overridepublic Object getItem (int position) {return position;} @Overridepublic long Getitemid (int position) {return position;} @Overridepublic view GetView (int position, view Convertview, ViewGroup parent) {Viewholder viewh;if (Convertview = = null) {VIEWH = new Viewholder (); Convertview = Layoutinflater.from (mainactivity.this). Inflate (R.layoUt.list_item, parent, false); Viewh.tag = (Pageonetagview) Convertview.findviewbyid (R.id.page_tag); Convertview.settag (VIEWH);} else {VIEWH = (Viewholder) Convertview.gettag ();} /* Add a label and display 4 tabs */viewh.tag.settaglist (list, 4); return Convertview;}} Class Viewholder {public pageonetagview tag;}}
Activity_main:
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" match_parent " android:layout_height=" Match_parent " android:paddingbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_ Horizontal_margin " android:paddingright=" @dimen/activity_horizontal_margin " android:paddingtop=" @dimen /activity_vertical_margin " tools:context=" com.bill.MainActivity "> <listview android:id=" @+ Id/lv_list " android:layout_width=" fill_parent " android:layout_height=" fill_parent "> </ Listview></relativelayout>
List_item: (Import the class we inherit from Basetagview)
<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" match_parent " android:layout_height=" match_parent "> < ImageView android:id= "@+id/iv_logo" android:layout_width= "wrap_content" android:layout_height= " Wrap_content " android:src=" @drawable/ic_launcher "/> <com.bill.page1.pageonetagview android: Id= "@+id/page_tag" android:layout_width= "wrap_content" android:layout_height= "Wrap_content " Android:layout_centervertical= "true" android:layout_marginleft= "10DP" android:layout_torightof= "@id/ Iv_logo "> </com.bill.page1.PageOneTagView></RelativeLayout>
personal GitHub Project address: Https://github.com/LinhaiGu/TabView
Reprint Please specify source:http://blog.csdn.net/hai_qing_xu_kong/article/details/45919871 Emotional Control _
Learn how to create custom labels with Android custom controls (39)