How to create Android Fragment as list item? -
i want create android app fragment
list item. please me if know. in advance...
if want make custom list elements, example, setting list items layout contains more 1 things like, imagesviews, text views etc. every item have different view. doing this:
step 1 : in adapter, setting layouts row elements.
here's code that:
here have 2 different layouts.. 1 main row , other elements. (other layouts see in getview()).
class youradapter extends baseadapter { context context; string[] data; private layoutinflater inflater = null; public youradapter(context context, string[] data) { this.context = context; this.data = data; inflater = (layoutinflater) context .getsystemservice(context.layout_inflater_service); } @override public int getcount() { return data.length; } @override public object getitem(int position) { return data[position]; } @override public long getitemid(int position) { return position; } @override public view getview(int position, view convertview, viewgroup parent) { view vi = convertview; if (vi == null) vi = inflater.inflate(r.layout.row, null); if (position == 0) { vi = inflater.inflate(r.layout.help_row1, null); } if (position == 1) { vi = inflater.inflate(r.layout.help_row_2, null); } if (position == 2) { vi = inflater.inflate(r.layout.help_row_3, null); } if (position == 3) { vi = inflater.inflate(r.layout.help_row_4, null); } return vi; } }
step 2 : how oncreate() looks like:
note : when setting adapter, can see there 4 strings there 4 layouts been used.
now, instead of using 4 layouts, can have 1 custom layout number of items in listview custom layout. depending on array size or string array length. in case, using 4 layouts not doing extra.
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); requestwindowfeature(window.feature_no_title); setcontentview(r.layout.help); textview close = (textview) findviewbyid(r.id.close_button); close.setonclicklistener(this); mactivity = this; listview = (listview) findviewbyid(r.id.helplistview); madapter = new youradapter(this, new string[] { "item1", "item2", "item3", "item4" }); listview.setadapter(madapter); }
step 3 : layouts been used example:
help.xml
<?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="wrap_content" android:orientation="vertical" > <textview android:id="@+id/textview1" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/header_gradient" android:gravity="center" android:text="help page" android:textappearance="?android:attr/textappearancelarge" android:textcolor="#0099cc" android:textsize="24sp" android:textstyle="bold" /> <listview android:id="@+id/helplistview" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="0.63" android:divider="#000000" android:dividerheight="6dp" > </listview> <textview android:id="@+id/close_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/header_gradient" android:gravity="center" android:text="close" android:textappearance="?android:attr/textappearancelarge" android:textcolor="#0099cc" android:textsize="24sp" android:textstyle="bold" /> </linearlayout>
row.xml : using view here.
<?xml version="1.0" encoding="utf-8"?> <view xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/view" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > </view>
and finally, 1 of layouts:
help_row1
<?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="vertical" android:padding="10dp" android:background="#666666"> <textview android:id="@+id/help_text_1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="step 1:" android:textcolor="#0099cc" android:layout_margintop="10dp" android:background="@drawable/header_gradient" android:textappearance="?android:attr/textappearancelarge" /> <textview android:id="@+id/help_text_2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="set destination entering number or select contact contact list." android:textappearance="?android:attr/textappearancelarge" /> <textview android:id="@+id/help_text_3" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="for example: enter number in empty text field or select destination choosing contact contact list." android:layout_margintop="10dp" android:textappearance="?android:attr/textappearancelarge" /> <imageview android:id="@+id/imageview1" android:layout_width="match_parent" android:layout_height="100dp" android:layout_margintop="5dp" android:src="@drawable/asset1" /> </linearlayout>
that's it. can use example build own custom listview. in case, can use fragments , use different data view.
hope example gives bit of idea on how can use fragment row listview.
if can make answer better, helpful me too..thanks.:)
Comments
Post a Comment