首页 > 代码库 > Android版本TO-DO-LIST,练习Fragment的使用

Android版本TO-DO-LIST,练习Fragment的使用

直接帖代码,懒得说这些老知识点

首先是主Activity:包括一个标题,一个输入项用于添加列表想,其实下方就是我们的ListFragment用于展示列表项,同时可以删除之

ToDoListActivity:

 package com.example.learn;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
/**
 * Created by gongxufan on 2014/10/30.
 */
public class ToDoListActivity extends Activity implements TodoListFragment.ToDoListInterface
{
    private EditText todo;
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.todolist);
        todo = (EditText)findViewById(R.id.addItem);
    }
    public void addItem(View v){
        addToDoListItem(todo.getText().toString());
    }
    @Override
    public void addToDoListItem(String itemName)
    {
        TodoListFragment ft =  (TodoListFragment)getFragmentManager().findFragmentById(R.id.todoList);
        ft.addToDoList(itemName);
    }
}

对应的布局文件:R.layout.todolist

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:gravity="center_horizontal">
    <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="To-Do-List"
            android:id="@+id/textView"
            android:gravity="center"/>
    <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center">
    <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="4"
            android:layout_gravity="fill_horizontal"
            android:id="@+id/addItem"
            />
        <Button android:layout_width="wrap_content" android:onClick="addItem"
                android:layout_height="wrap_content" android:text="添加" android:layout_weight="1"/>
       </LinearLayout>
    <fragment android:layout_width="match_parent"
              android:name="com.example.learn.TodoListFragment"
              android:layout_height="wrap_content"
              android:id="@+id/todoList"></fragment>
</LinearLayout>

如上所说的三个组件,期中嵌套了线性布局,fagment也是线性布局嵌套结果

接下来是TodoListFragment代码,其主要是一个ListFragment,有三部分组成:EditText和两个Button,由于我们使用自定义ITEM布局所以必须指定一个ID为"@+id/android:list"的布局,

然后在适配器实现ITEM项的具体布局。先贴下2个布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    <ListView
            android:id="@+id/android:list"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            />
</LinearLayout>

上面是ListFragment的主布局文件,在onCreateView进行调用,然后在适配器中把自定义VIEW添加到这个ListView中作为项。

这里稍微说明下,为什么ListFragment主布局文件必须要有一个ID为list的ListView呢,其实这正是ListFragment里帮我们实现了LISTVIEW的功能。

在适配器View getView(final int position, View convertView, ViewGroup parent)这个方法中的parent其实就是上面布局文件里的ListView的实例,看调试结果:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/todoListContent" android:layout_weight="8"/>
    <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="编辑"
            android:id="@+id/editItem" android:layout_weight="1"/>
    <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="删除"
            android:id="@+id/deleteItem" android:layout_weight="1"/>
</LinearLayout>

上面是每个项的布局,很简单

接下来贴上ListFragment的代码:

package com.example.learn;
import android.app.Activity;
import android.app.Fragment;
import android.app.ListFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * Created by gongxufan on 2014/10/30.
 */
public class TodoListFragment extends ListFragment
{
    private ToDoListInterface callback;
    private List<String> data = new ArrayList<String>();
    private BaseAdapter baseAdapter;
    public interface ToDoListInterface{
        public void addToDoListItem(String itemName);
    }
    private class ViewHolder{
        EditText todoItem;
        Button edit;
        Button delete;
    }
    public ToDoListInterface getCallback()
    {
        return callback;
    }
    @Override
    public void onAttach(Activity activity)
    {
        super.onAttach(activity);
        callback = (ToDoListInterface)activity;
    }
    @Override
    public void onDetach()
    {
        super.onDetach();
        callback = null;
    }
    public void addToDoList(String itemName){
        data.add(itemName);
        baseAdapter.notifyDataSetChanged();
    }
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        baseAdapter = new BaseAdapter()
        {
            @Override
            public int getCount()
            {
                return data.size();
            }
            @Override
            public Object getItem(int position)
            {
                return data.get(position);
            }
            @Override
            public long getItemId(int position)
            {
                return position;
            }
            @Override
            public View getView(final int position, View convertView, ViewGroup parent)
            {
                LayoutInflater mInflater = (LayoutInflater) TodoListFragment.this.getActivity()
                    .getSystemService(TodoListFragment.this.getActivity().LAYOUT_INFLATER_SERVICE);
                ViewHolder viewHolder = null;
                if(convertView == null){
                    viewHolder = new ViewHolder();
                    convertView = mInflater.inflate(R.layout.ltodolist_item,parent,false);
                    viewHolder.todoItem = (EditText)convertView.findViewById(R.id.todoListContent);
                    viewHolder.edit = (Button)convertView.findViewById(R.id.editItem);
                    viewHolder.delete = (Button)convertView.findViewById(R.id.deleteItem);
                    convertView.setTag(viewHolder);
                }else{
                    viewHolder = (ViewHolder)convertView.getTag();
                }
                viewHolder.todoItem.setText((String)getItem(position));
                viewHolder.delete.setOnClickListener(new View.OnClickListener()
                {
                    @Override
                    public void onClick(View v)
                    {
                        data.remove(getItem(position));
                        baseAdapter.notifyDataSetChanged();
                    }
                });
                return convertView;
            }
        };
        setListAdapter(baseAdapter);
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        return inflater.inflate(R.layout.todolist_fragment, container, false);
    }
    @Override
    public void onListItemClick(ListView l, View v, int position, long id)
    {
        super.onListItemClick(l, v, position, id);
    }
}

代码也很简单,只是一个BaseAdapter进行自定义布局,然后就是响应删除,添加的事件处理,改变适配器数据重新进行UI布局

 

好了一个简单粗狂的TO-DO-LIST就完成了,只是为了练习下Fragment使用,本人新手,忘见谅。

模拟器运行结果:

Android版本TO-DO-LIST,练习Fragment的使用