ToDoList(fragment) 详解


版权所有, 禁止转载, 如有需要, 请站内联系.


本文地址:http://blog.csdn.net/caroline_wendy/article/details/21246963


ToDoList做为Android的经典练习, 参考:http://blog.csdn.net/caroline_wendy/article/details/21223995


Fragment(碎片)可以灵活地从一个活动的Activity上添加或删除Fragment, 有良好的用户体验;

下面是Fragment的具体设计:


1. 创建new_item_fragment的资源文件:

位置: 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" />

包含一个EditText控件;


2. 创建NewItemFragment的逻辑实现:

位置: 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;      //监听事件     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, container, false);          final EditText myEditText = (EditText)view.findViewById(R.id.myEditText);          //监听事件         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;     }      //绑定到Activity     public void onAttach(Activity activity) {          super.onAttach(activity);          try {             onNewItemAddedListener = (OnNewItemAddedListener)activity;         } catch (ClassCastException e) {             throw new ClassCastException(activity.toString() + " must implement OnNewItemAddedListener");         }     }  } 

详解:

1. 继承Fragment类;

2. 监听接口(interface OnNewItemListener), 包含一个监听方法(onNewItemAdded), 然后实现一个实例;

3. 创建视图(onCreateView), 需要返回一个视图(View);

4. 填充一个视图, 调用inflater.inflate()函数, 绑定fragment的资源文件;

5. 获得控件的引用,view.findViewById(), 得到myEditText的引用;

6. 监听事件, 监听myEditText的输入内容, 传递给接口的内容(new Item);

7. 使用onAttach()函数, 绑定Acitivity之后, 获得activity的引用;


3.创建ToDoListFragment的逻辑实现:

位置: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{ }

继承ListFragment;


4. 创建activity_to_do_list资源文件, 即Activity资源文件:

位置: 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_height="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 android:name="mzx.spike.todolist.app.NewItemFragment"         android:id="@+id/NewItemFragment"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:hint="@string/addItemHint"         android:contentDescription="@string/addItemContentDescription"     />      <fragment android:name="mzx.spike.todolist.app.ToDoListFragment"         android:id="@+id/ToDoListFragment"         android:layout_width="match_parent"         android:layout_height="wrap_content"     />  </LinearLayout>

LinearLayout包含两个fragment结点, 需要提供android:name的参数, 实现;


5.创建ToDoListActivityt的逻辑实现, 即Activity的逻辑实现:

位置:java->package->ToDoListActivityt.java


package mzx.spike.todolist.app;  import android.app.FragmentManager; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.view.Menu; import android.view.MenuItem; import android.widget.ArrayAdapter;  import java.util.ArrayList;   public class ToDoListActivity extends ActionBarActivity implements NewItemFragment.OnNewItemAddedListener {      private ArrayAdapter<String> aa;     private ArrayList<String> toDoItems;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_to_do_list);          //获得fragment的引用         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);     }      //重写了接口的方法     public void onNewItemAdded(String newItem) {         toDoItems.add(newItem);         aa.notifyDataSetChanged();     }      @Override     public boolean onCreateOptionsMenu(Menu menu) {                  // Inflate the menu; this adds items to the action bar if it is present.         getMenuInflater().inflate(R.menu.to_do_list, menu);         return true;     }      @Override     public boolean onOptionsItemSelected(MenuItem item) {         // Handle action bar item clicks here. The action bar will         // automatically handle clicks on the Home/Up button, so long         // as you specify a parent activity in AndroidManifest.xml.         int id = item.getItemId();         if (id == R.id.action_settings) {             return true;         }         return super.onOptionsItemSelected(item);     }  } 


详解:

1. 继承监听接口(OnNewItemAddedListener);

2. ArrayList类型数据, 和适配器(ArrayAdapter);

3. 通过findFragmentById(),获得ToDoListFragment的引用;

4. 把toDoItem绑定适配器,ToDoListFragment设置适配器(setListAdapter);

5. 实现接口的方法, 传递至toDoItems, 通知适配器更改(notifyDataSetChanged);


6. 执行程序:

其他文件参考:http://blog.csdn.net/caroline_wendy/article/details/21223995


下载地址:http://download.csdn.net/detail/u012515223/7042137



更多相关文章

  1. (android 实战总结)android对html支持接口总结
  2. 第三部分:Android 应用程序接口指南---第一节:应用程序组件---第五
  3. Android 无线接口层RIL
  4. android对html支持接口总结
  5. Android 匿名共享内存Java接口分析
  6. Android rest接口
  7. android listview继承BaseAdapter,自定义的适配器,getView方法执
  8. android 常用api 接口签名验证
  9. Android中接口的使用及类使用

随机推荐

  1. Linux 之 Yum 仓库的配置及使用
  2. Android(安卓)实现文字按照路径曲线显示
  3. 关于Service的使用
  4. 芝麻信用分SDK接入,显示芝麻信用授权界面(A
  5. Android——Canvas类的使用
  6. 开发技术前线 第六期
  7. android 修改AlertDialog的黑色背景的两
  8. Android中VideoView播放视频不能充满屏幕
  9. Archlinux下配置Android开发环境[转]
  10. Android(安卓)- 文件操作 小结