Android – ToDoList(fragment) 詳解

ToDoList(fragment) 詳解

 

版權所有, 禁止轉載, 如有需要, 請站內聯系.

 

 

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

下面是Fragment的具體設計:

 

1. 創建new_item_fragment的資源文件:

位置: res->new_item_fragment.xml

 



包含一個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包含兩個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 aa;
    private ArrayList 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();

        //三個參數
        aa = new ArrayAdapter(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. 執行程序:

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

 

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

 

發佈留言