Directly post code, too lazy to say these old knowledge points
First, the main activity: include a title, an entry to add a list to think, in fact, below is our listfragment used to display the list of items, and can be deleted
Todolistactivity:
package com.example.learn;import android.app.activity;import android.os.bundle;import android.view.view;import android.widget.edittext;/** * created by gongxufan on 2014/10/30. */public class ToDoListActivity extends Activity implements todolistfragment.todolistinterface{ private edittext todo; @Override public void oncreate (bundle Savedinstancestate) { super.oncreate ( Savedinstancestate); setcontentview (R.layout.todolist); todo = (EditText) Findviewbyid (R.id.additem); } public void additem (VIEW V) { addtodolistitem (Todo.gettExt (). toString ()); } @Override public void addtodolistitem (String itemname) { TodoListFragment ft = (todolistfragment) Getfragmentmanager (). Findfragmentbyid (r.id.todolist); ft.addtodolist (ItemName); }}
Corresponding layout file: R.layout.todolist
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http// Schemas.android.com/apk/res/android " android:orientation= "Vertical" android:layout_width= "Match_parent" android:layout_height= "Match_parent" android:gravity= "Center_horizontal" > < Textview android:layout_width= "Match_ Parent " android:layout_height=" Wrap_ Content " android:text=" To-Do-List " android:id= "@+id/textview" android:gravity= "Center"/> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:orientation=" Horizontal " android:layout_width=" match _parent " android:layout_height=" Wrap_ Content " android:gravity=" center > <EditText Android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:layout_weight= "4" android:layout_gravity= "Fill_horizontal" android:id= "@+id/addItem" /> <button android:layout_width= "Wrap_content" android:onclick= "AddItem" android:layout_height= "Wrap_content" android:text= "Add" android:layout_weight= "1"/> </ Linearlayout> <fragment android:layout_width= "Match_parent" android:name= " Com.example.learn.TodoListFragment "&NBSP;&NBSP;&NBSP;&NBSP;&Nbsp; android:layout_height= "Wrap_content" android:id= "@+id/todoList" ></ Fragment></linearlayout>
As mentioned on the three components, the mid-term nested linear layout, fagment is also a linear layout nested results
Next is the Todolistfragment code, which is mainly a listfragment, with three parts: EditText and two buttons, because we use the custom item layout, we must specify an ID of "@+id/android: List "layout,
The adapter then implements the specific layout of the item item. Put down 2 layout files first:
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http// Schemas.android.com/apk/res/android " android:orientation= "Vertical" android:layout_width= "Match_parent" android:layout_height= "Match_parent" > <listview android:id= "@+id/android:list" android:layout_width= "Fill_parent" android:layout_height= "Wrap_content" /></LinearLayout>
This is the main layout file for Listfragment, which is called in Oncreateview, and the custom view is added to the ListView as an item in the adapter.
Here is a little explanation, why listfragment main layout file must have an ID list of the ListView, in fact, this is the listfragment to help us realize the function of the ListView.
In the adapter View getView (final int position, view Convertview, ViewGroup parent) The parent in this method is actually an instance of the ListView in the above layout file, looking at the debug result:
<?xml version= "1.0" encoding= "Utf-8"?><linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android" android:orientation= "Horizontal" android:layout_ Width= "Match_parent" android:layout_height= "Wrap_content" > <EditText Android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" Android:id= "@+id/todolistcontent" android:layout_weight= "8"/> <button style= "Android:attr/buttonstylesmall" &Nbsp; android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:text= "Edit" android:id= "@+id/edititem" android:layout_weight= "1"/> < Button style= "? android:attr/ Buttonstylesmall " android:layout_width= "Wrap_content" android:layout_height= " Wrap_content " android:text=" Delete " android:id= "@+id/deleteItem" android: layout_weight= "1"/></linearlayoUt>
The above is the layout of each item, very simple
Next, paste the Listfragment code:
package com.example.learn;import android.app.activity;import android.app.fragment;import android.app.listfragment;import android.os.bundle;import android.view.layoutinflater;import android.view.view;import android.view.viewgroup;import android.widget.*;import java.util.arraylist;import java.util.hashmap;import java.util.list;import java.util.map;/** * Created by gongxufan on 2014/10/30. */public class todolistfragment extends listfragment{ private todolistinterface callback; private list<string> data = new arraylist< String> (); private baseadapter baseadapter; public interface ToDoListInterface{ public void Addtodolistitem (String itemname); } private class ViewHolder{ EditText todoItem; Button edit; Button delete; } Public todolistinterface getcallback () { return callback; } @Override public void onattach (activity activity) { super.onattach (activity); callback = (todolistinterface) activity; } @Override public void ondetach () { super.ondetach (); callback = null; } public void addtodolist (String itemname) { data.add (ItemName); Baseadapter.notifydatasetchanged (); } @Override public void oncreate (bundle savedinstancestate) { super.oncreate (savedinstancestate); baseadapter = new baseadapter () { @Override public int getcount () { return data.size (); } @Override public object getitem (int position) { return data.get (position); } @Override public long getitemid (int position) { return position; } @Override public view getview (final int Position, view convertview, viewgroup parent) { LayoutInflater mInflater = (Layoutinflater) TodoListFragment.this.getActivity () .getsystemservice (TodoListFragment.this.getActivity (). Layout_inflater_service); viewholder viewholder = null; &nBsp; if (convertView == null) { viewholder = new viewholder (); convertView = Minflater.inflate (R.layout.ltodolist_item,parent,false); viewHolder.todoItem = (EditText) Convertview.findviewbyid (r.id.todolistcontent); viewHolder.edit = (Button) Convertview.findviewbyid (R.id.edititem); viewHolder.delete = (Button) Convertview.findviewbyid (R.id.deleteitem); Convertview.settag (Viewholder); }else{ viewHolder = (Viewholder) Convertview.gettag (); } viewholder.todoitem.settext (String) GetItem ( Position)); ViewHolder.delete.setOnClickListener (New view.onclicklistener () { @Override public void onclick (VIEW&NBSP;V) { data.remove (GetItem (position)); baseadapter.notifydatasetchanged (); } }); return convertView; } }; setlistadapter ( Baseadapter); } @Override public View oncreateview (layoutinflater inflater, viewgroup container, bundle Savedinstancestate) { return Inflater.inflate (R.layout.todolist_fragment, container, false); } @Override public void onlistitemclick (listview l, view v, int position, long id) { super.onlistitemclick (l, v, position, id); }}
The code is also simple, just a baseadapter for a custom layout, then a response to delete, add event handling, change the adapter data to re-ui layout
Ok a simple rough to-do-list finished, just to practice under fragment use, I novice, forget forgive me.
Emulator Run Result:
Android version to-do-list, practice the use of fragment