ToDoList的例子是《android高级编程第四版》上的一个例子
1.第一个ToDoList很简单,就是main.xml上填充一个EditText和ListView
2.然后通过EditText的监听函数setOnKeyListener监听动作
3.然后在把相应的文本加入到ListView上
我主要是理解一下ToDoList的版本2,通过Fragment去实现,里面涉及到了几个知识点:
1.Fragment和Activity的通信--通过Activity实现Fragment中的接口
2.ListFragment的理解--不需要填充
3.Fragment在Activity中的调用--onCreate中调用
4.Fragment在Activity中的填充--xml中fragment标签
5.Fragment中填充--就一个EditText
代码如下:
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.godlovesoccer.todolistfragment.MainActivity" >
<fragment
android:id="@+id/NewItemFragment"
android:name="com.godlovesoccer.todolistfragment.NewItemFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<fragment
android:id="@+id/TodoListFragment"
android:name="com.godlovesoccer.todolistfragment.ToDoListFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
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:contentDescription="@string/addItemContentDescription"
android:hint="@string/addItemHint"/>
MainActivity.java
package com.godlovesoccer.todolistfragment;
import java.util.ArrayList;
import android.app.Activity;
import android.app.FragmentManager;
import android.os.Bundle;
import android.widget.ArrayAdapter;
public class MainActivity extends Activity implements NewItemFragment.OnNewItemAddedListener{
private ArrayAdapter<String> aa;
private ArrayList<String> todoItems;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fm=getFragmentManager();
ToDoListFragment todoListFragment=(ToDoListFragment)fm.findFragmentById(R.id.TodoListFragment);
todoItems=new ArrayList<String>();
aa=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,todoItems);
todoListFragment.setListAdapter(aa);
}
@Override
public void onNewItemAdded(String newItem) {
// TODO Auto-generated method stub
todoItems.add(newItem);
aa.notifyDataSetChanged();
}
}
NewItemFragment.java
package com.godlovesoccer.todolistfragment;
import android.app.Activity;
import android.app.Fragment;
import android.view.KeyEvent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
public class NewItemFragment extends Fragment{
private OnNewItemAddedListener onNewItemAddedListener;
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){
View view=inflater.inflate(R.layout.new_item_fragment, container,false);
final EditText myEditText=(EditText)view.findViewById(R.id.myEditText);
myEditText.setOnKeyListener(new View.OnKeyListener(){
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if(event.getAction()==KeyEvent.ACTION_DOWN)
if((keyCode==KeyEvent.KEYCODE_DPAD_CENTER)||
keyCode==KeyEvent.KEYCODE_ENTER){
String newItem=myEditText.getText().toString();
onNewItemAddedListener.onNewItemAdded(newItem);
myEditText.setText("");
return true;
}
return false;
}
});
return view;
}
public interface OnNewItemAddedListener{
public void onNewItemAdded(String newItem);
}
public void onAttach(Activity activity){
super.onAttach(activity);
try{
onNewItemAddedListener=(OnNewItemAddedListener)activity;
}catch(ClassCastException e){
throw new ClassCastException(activity.toString()+"must implement onNewItemAddedListener");
}
}
}
ToDoListFragment.java
package com.godlovesoccer.todolistfragment;
import android.app.ListFragment;
public class ToDoListFragment extends ListFragment{
}
对于我来说,对于这几个类的调用关系真是挺复杂的,总是不能很顺利的去理解。只能先通过死记硬背的方式去尝试着理顺。
1.Fragment和Activity的通信
MainActivity实现了Fragment的接口。
这个看似是前提,其实Fragment在onAttach函数中对Activity的引用才是前提。
onCreateView中将EditText填充到Fragment,然后监听操作,再调用Activity。
2.在Activity中实现了ListFragment和Arrayadpter的绑定
然后在实现Fragment的接口,将每次输入,插入到Arrayadpter中
3.这么一看,其实入口相当于Fragment的,每次去调用Activity的接口函数
4.我现在的疑惑就是,为什么非要实现Fragment的接口呢?
其实在Activity中实现一个函数,能处理Fragment给他的数据不就行了吗???
5.行是行,但是不太好,通过接口的话,能很清楚的知道Activity能和fragment做
什么样的交互。在Fragment中制定了交互的内容,Activity根据不同的接口,实
现不同交互内容。