android - How to add tabs to listview? -
i need add 2 tabs in listing , not know how it, wanted add 1 in first row , after fourth item separates: trend , genre, saw adding new xml layout need because can not, text between items..
my main activity:
public class inicio extends appcompatactivity { private listview listaitens; private string[] itens = { "home", "playlist", "trend", "conta", "live", "rock", "hip hop", "pop", "house", "gospel music", "metal", "alt rock", "sport", "game" }; integer[] imgid={ r.drawable.home, r.drawable.playlist, r.drawable.trend, r.drawable.conta, r.drawable.live, r.drawable.rock, r.drawable.hiphop, r.drawable.pop, r.drawable.house, r.drawable.gospel, r.drawable.metal, r.drawable.alternative_rock, r.drawable.sport, r.drawable.game, }; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_inicio); listaitens = (listview) findviewbyid(r.id.listview); customlistadapter adapter=new customlistadapter(this, itens, imgid); listaitens=(listview)findviewbyid(r.id.listview); listaitens.setadapter(adapter); listaitens.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> adapterview, view view, int position, long id) { if (position == 0) { intent intent = new intent(inicio.this, insmainactivity.class); intent.putextra("link", "https://m.youtube.com/"); startactivity(intent);
my adapter:
public class customlistadapter extends arrayadapter<string> { private final activity context; private final string[] itens; private final integer[] imgid; public customlistadapter(inicio context, string[] itens, integer[] imgid) { super(context, r.layout.mylist, itens); this.context=context; this.itens=itens; this.imgid=imgid; } public view getview(int position,view view,viewgroup parent) { layoutinflater inflater=context.getlayoutinflater(); view rowview=inflater.inflate(r.layout.mylist, null,true); textview txttitle = (textview) rowview.findviewbyid(r.id.item); imageview imageview = (imageview) rowview.findviewbyid(r.id.icon); textview extratxt = (textview) rowview.findviewbyid(r.id.textview1); txttitle.settext(itens[position]); imageview.setimageresource(imgid[position]); extratxt.settext("access "+itens[position]); return rowview; }
xml listview
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <imageview android:id="@+id/icon" android:layout_width="60dp" android:layout_height="60dp" android:padding="5dp" /> <linearlayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <textview android:id="@+id/item" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="medium text" android:textappearance="?android:attr/textappearancemedium" android:layout_marginleft="10dp" android:layout_margintop="5dp" android:padding="2dp" android:textcolor="#000000" /> <textview android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="textview" android:layout_marginleft="10dp"/> </linearlayout> </linearlayout>
if can me, thank much.
you have create separate xml
files:
- one create row layout
- another tab
and make changes adapter below
public class customlistadapter extends arrayadapter<string> { private static final int type_item = 0; private static final int type_tab = 1; private final activity context; private final string[] itens; private final integer[] imgid; public customlistadapter(inicio context, string[] itens, integer[] imgid) { super(context, r.layout.mylist, itens); this.context=context; this.itens=itens; this.imgid=imgid; } public view getview(int position,view view,viewgroup parent) { int type = getitemviewtype(position); layoutinflater inflater=context.getlayoutinflater(); if (view == null) { // inflate layout according view type layoutinflater inflater = (layoutinflater) ctx.getsystemservice(context.layout_inflater_service); if (type == type_item) { view = inflater.inflate(r.layout.your_item_layout, parent, false); } else { view = inflater.inflate(r.layout.your_tab_layout, parent, false); } textview txttitle = (textview) view.findviewbyid(r.id.item); imageview imageview = (imageview) view.findviewbyid(r.id.icon); textview extratxt = (textview) view.findviewbyid(r.id.textview1); /** * tab layout textview */ if (type == type_tab) { textview tab = (textview) view.findviewbyid(r.id.tab); tab.settext("your text"); } txttitle.settext(itens[position]); imageview.setimageresource(imgid[position]); extratxt.settext("access "+itens[position]); return view; } /** * override function, returns number of types of views in list. * in case 2 */ @override public int getviewtypecount() { return 2; } /** * override function, returns item return @ position */ @override public int getitemviewtype(int position) { //return type = tab according position if (position == 0 || position == 4) { return type_tab; } else { return type_item; } }
Comments
Post a Comment