Android Listfragment Instance Demo (custom adapter)

Source: Internet
Author: User

The previous article introduced Listfragment, in which the ListView does not have a custom adapter, and actually often uses custom adapters in real-world development, which is a more complex list of data presentation. So this article added a custom adapter for the display of the ListView data.

Realize:

On the left is a button in the activity, and clicking on the button will show the corresponding data list on the right fragment.


Code Show:

Layout file:

Activity_main:

<linearlayout 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 "Tools:context =". Mainactivity "> <linearlayout android:id=" @+id/left "android:layout_width=" 0DP "Android:layo ut_height= "Match_parent" android:layout_weight= "1" android:orientation= "vertical" android:background            = "#DDCCFF" > <button android:id= "@+id/button1" android:layout_width= "Match_parent" android:layout_height= "Wrap_content" android:text= "Show list"/> </LinearLayout> <linearlayo UT android:id= "@+id/right" android:layout_width= "0DP" android:layout_height= "Match_parent" and roid:layout_weight= "2" android:orientation= "vertical" android:background= "#DDFFCC" > </linearlayout ></LinearLayout>

Article.xml:

<linearlayout 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 "    tools:context= ". Mainactivity ">    <listview        android:id=" @id/android:list "        android:layout_width=" Match_parent "        android:layout_height= "Wrap_content" >    </ListView></LinearLayout>

Item.xml:

<linearlayout 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 "    tools:context= ". Mainactivity ">   <textview        android:id=" @+id/textview "       android:layout_width=" Wrap_content       " android:layout_height= "Wrap_content"       android:textsize= "20SP"/></linearlayout>
Code files:

package Com.fragmentdemo9_listfragment2;import Android.app.activity;import Android.app.fragmentmanager;import Android.app.fragmenttransaction;import Android.os.bundle;import Android.view.view;import android.widget.Button;/*  * Example of *listfragment demo optimized version one. * */public class Mainactivity extends Activity {private Button button;private Fragmentmanager manager;private Fragmenttra Nsaction transaction; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate ); Setcontentview (r.layout.activity_main); manager = Getfragmentmanager (); button = (button) Findviewbyid (r.id.button1 ); Button.setonclicklistener (new View.onclicklistener () {@Overridepublic void OnClick (View v) {transaction = Manager.begintransaction (); Articlelistfragment articlelistfragment = new Articlelistfragment (); Transaction.replace (R.id.right, Articlelistfragment, "right"); Transaction.commit ();}});}} 

Articlelistfragment.java:

Package Com.fragmentdemo9_listfragment2;import Java.util.arraylist;import Android.app.listfragment;import Android.os.bundle;import Android.view.layoutinflater;import Android.view.view;import Android.view.ViewGroup; Import Android.widget.listview;import android.widget.toast;/** * The only one in this example fragment * @author Administrator * */public Class Articlelistfragment extends Listfragment {private Myadapter adapter;private arraylist<string> list = new Arra Ylist<string> (); @Overridepublic void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate) ; for (int i = 0; i < i++) {List.add ("item" + i);} adapter = new Myadapter (getactivity (), list);} @Overridepublic View Oncreateview (Layoutinflater inflater, ViewGroup container,bundle savedinstancestate) {View view = Inflater.inflate (r.layout.article, null); Setlistadapter (adapter); return view;} @Overridepublic void Onlistitemclick (ListView l, View v, int position, long id) {Super.onlistitemclick (L, V, position, id) ; Toast.maketeXT (Getactivity (), list.get (position), Toast.length_short). Show ();}} 

Myadapter:

Package Com.fragmentdemo9_listfragment2;import Java.util.arraylist;import Android.content.context;import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;import Android.widget.baseadapter;import android.widget.textview;/** * Custom Adapter Myadapter * */public class MyAdapter extends Baseadapter {Private arraylist<string> list;private context Context;public myadapter (context context, ArrayList <String> list) {this.list = List;this.context = Context;} @Overridepublic int GetCount () {return list.size ();} @Overridepublic Object getItem (int position) {return list.get (position);} @Overridepublic long Getitemid (int position) {return position;} @Overridepublic view GetView (int position, view Convertview, ViewGroup parent) {Viewholder holder = new Viewholder (); if (Convertview = = null) {Convertview = Layoutinflater.from (context). Inflate (r.layout.item,null); Holder.textview = ( TextView) Convertview.findviewbyid (R.id.textview); Convertview.settag (holder);} else {HolDer = (Viewholder) Convertview.gettag ();} Holder.textView.setText (List.get (position). ToString ()); return Convertview;} Class Viewholder {TextView TextView;}}

Source code Download:

Click to download the source code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.