Android-ToDoList(fragment)詳解

ToDoList(fragment) 詳解


站在用戶的角度思考問題,與客戶深入溝通,找到全南網(wǎng)站設(shè)計與全南網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗,讓設(shè)計與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個性化、用戶體驗好的作品,建站類型包括:成都網(wǎng)站制作、成都網(wǎng)站建設(shè)、外貿(mào)營銷網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、域名注冊、網(wǎng)站空間、企業(yè)郵箱。業(yè)務(wù)覆蓋全南地區(qū)。

版權(quán)所有, 禁止轉(zhuǎn)載, 如有需要, 請站內(nèi)聯(lián)系.


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


ToDoList做為Android的經(jīng)典練習(xí), 參考: http://blog.csdn.net/caroline_wendy/article/details/21223995


Fragment(碎片) 可以靈活地從一個活動的Activity上添加或刪除Fragment, 有良好的用戶體驗;

下面是Fragment的具體設(shè)計:


1. 創(chuàng)建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. 創(chuàng)建NewItemFragment的邏輯實現(xiàn):

位置: 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;      //監(jiān)聽事件     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);          //監(jiān)聽事件         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. 監(jiān)聽接口(interface OnNewItemListener), 包含一個監(jiān)聽方法(onNewItemAdded), 然后實現(xiàn)一個實例;

3. 創(chuàng)建視圖(onCreateView), 需要返回一個視圖(View);

4. 填充一個視圖, 調(diào)用inflater.inflate()函數(shù), 綁定fragment的資源文件;

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

6. 監(jiān)聽事件, 監(jiān)聽myEditText的輸入內(nèi)容, 傳遞給接口的內(nèi)容(new Item);

7. 使用onAttach()函數(shù), 綁定Acitivity之后, 獲得activity的引用;


3. 創(chuàng)建ToDoListFragment的邏輯實現(xiàn):

位置: 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. 創(chuàng)建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結(jié)點, 需要提供android:name的參數(shù), 實現(xiàn);


5. 創(chuàng)建ToDoListActivityt的邏輯實現(xiàn), 即Activity的邏輯實現(xiàn):

位置: 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>();          //三個參數(shù)         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. 繼承監(jiān)聽接口(OnNewItemAddedListener);

2. ArrayList類型數(shù)據(jù), 和適配器(ArrayAdapter);

3. 通過findFragmentById(), 獲得ToDoListFragment的引用;

4. 把toDoItem綁定適配器, ToDoListFragment設(shè)置適配器(setListAdapter);

5. 實現(xiàn)接口的方法, 傳遞至toDoItems, 通知適配器更改(notifyDataSetChanged);


6. 執(zhí)行程序:

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


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


Android - ToDoList(fragment) 詳解

網(wǎng)站名稱:Android-ToDoList(fragment)詳解
當(dāng)前路徑:http://bm7419.com/article4/isgioe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、云服務(wù)器商城網(wǎng)站、網(wǎng)站內(nèi)鏈App設(shè)計、服務(wù)器托管

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作