Fragment (fragmentation) can be flexible to add or remove Fragment from an active activity and have a good user experience;
Here is the specific design of the fragment:
1. Create a new_item_fragment resource file:
Location: Res->new_item_fragment.xml
<?xml version= "1.0" encoding= "Utf-8"?> <edittext xmlns:android=
"http://schemas.android.com/apk/res/" Android "
android:id=" @+id/myedittext "
android:layout_width=" match_parent "
android:layout_height=" Wrap_content "
android:hint=" @string/additemhint "
android:contentdescription=" @string Additemcontentdescription "
/>
Contains a EditText control;
2. Create a logical implementation of newitemfragment:
Location: Java->package->newitemfragment.java
Package Mzx.spike.todolist.app;
Import android.app.Activity;
Import android.app.Fragment;
Import Android.os.Bundle;
Import android.view.KeyEvent;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.widget.EditText;
/** * Created by C.l.wang on 14-3-14.
* * public class Newitemfragment extends fragment{private Onnewitemaddedlistener onnewitemaddedlistener;
Listener Event public interface Onnewitemaddedlistener {public void onnewitemadded (String newitem);
@Override public View Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {View view = Inflater.inflate (R.layout.new_item_fragment, Conta
Iner, false);
Final EditText Myedittext = (edittext) View.findviewbyid (R.id.myedittext); Listener Event Myedittext.setonkeylistener (nEW View.onkeylistener () {@Override public boolean onkey (view view, int i, keyevent keyevent) { if (keyevent.getaction () = = Keyevent.action_down) if ((i = = Keyevent.keycode_dpad_
CENTER) | | (i = = Keyevent.keycode_enter))
{String newitem = Myedittext.gettext (). toString ();
Onnewitemaddedlistener.onnewitemadded (NewItem);
Myedittext.settext ("");
return true;
return false;
}
});
return view;
}//bind to activity public void Onattach (activity activity) {Super.onattach (activity);
try {Onnewitemaddedlistener = (onnewitemaddedlistener) activity; catch (ClassCastException e) {throw new ClassCastException (activity.tostring () + "Must implement Onnewitemaddedlistener "); }
}
}
Detailed
1. Inheritance of the fragment category;
2. Listener interface (interface Onnewitemlistener), including a listening method (onnewitemadded), and then implement an instance;
3. Create a view (Oncreateview), you need to return a view;
4. Populate a view, call the Inflater.inflate () function, bind the fragment resource file;
5. Obtains the reference of the control, View.findviewbyid (), obtains the Myedittext the reference;
6. Monitor the event, listen to the input content of Myedittext, pass to the content of the interface (new Item);
7. Use the Onattach () function, bind the acitivity, and obtain a reference to the activity;
3. Create a logical implementation of todolistfragment:
Location: Java->package->todolistfragment.java
Package Mzx.spike.todolist.app;
Import android.app.ListFragment;
/**
* Created by C.l.wang on 14-3-14.
*/Public
class Todolistfragment extends listfragment{
}
Inheriting listfragment;
4. Create the Activity_to_do_list resource file, the activity resource file:
Location: Res->layout->activity_to_do_list.xml
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http:// Schemas.android.com/tools "android:orientation=" vertical "android:layout_width=" match_parent "Android:layout_h" eight= "Match_parent" android:paddingleft= "@dimen/activity_horizontal_margin" android:paddingright= "@dimen Activity_horizontal_margin "android:paddingtop=" @dimen/activity_vertical_margin "android:paddingbottom=" @dimen Activity_vertical_margin "tools:context=" mzx.spike.todolist.app.ToDoListActivity "> <fragment androi D:name= "Mzx.spike.todolist.app.NewItemFragment" android:id= "@+id/newitemfragment" Android:layout_width= "Ma Tch_parent "android:layout_height=" wrap_content "android:hint=" @string/additemhint "Android:conten tdescription= "@string/additemcontentdescription"/> <fragment android:name= "Mzx.spike.todolist.app . Todolistfragment "Android:id=" @+id/todolistfragment "android:layout_width=" match_parent "android:layout_height=" wrap_content "/> </Li Nearlayout>
LinearLayout contains two fragment nodes, which need to provide android:name parameters to achieve;
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/OS/extra/