Fragment 简单实例详解

此例就是一个包含一个EditText 和ListView两个组件的简单例子  在文本框中输入文字保存到listView列表中,就是学习Fragment布局屏幕适配性便利和增加删除布局的便利性。


把UI分割为一系列的Fragment 来表示其各个组件 :文本输入框和 to-do 事项的列表  这样可以很容易的为不同大小的屏幕创建最佳的布局。

1 首先为EditeText组件创建一个布局文件:

new_item_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
  <EditText
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/myEditText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:hint="@string/addItemHint"
        android:contentDescription="@string/addItemContentDescription"
    />

2.为每个UI组件创建一个新的Fragment  创建一个继承自Fragment的NewItemFragment重写onCreateView处理程序来填充第一步创建的布局,每个Fragment应该封装它所提供的功能,

  

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) {
// TODO Auto-generated method stub
//return inflater.inflate(R.layout.new_item_fragment, container, false);
View view = inflater.inflate(R.layout.new_item_fragment, container, false);
final EditText myEditText =(EditText) view.findViewById(R.id.myEditText1);
          myEditText.setOnKeyListener(new View.OnKeyListener() {

@Override
public boolean onKey(View arg0, int arg1, KeyEvent arg2) {
// TODO Auto-generated method stub
if(arg2.getAction()==KeyEvent.ACTION_DOWN){
if((arg1==KeyEvent.KEYCODE_DPAD_CENTER)||(arg1==KeyEvent.KEYCODE_ENTER)||(arg1==KeyEvent.KEYCODE_BREAK)){
                      String newItem = myEditText.getText().toString();
                      onNewItemAddedListener.onNewItemAdded(newItem);
 myEditText.setText("");
 return true;
}
}
return false;
}
});
          return view;
}


@Override
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
super.onAttach(activity);

try {
onNewItemAddedListener =(OnNewItemAddedListener)activity;
} catch (Exception e) {
// TODO: handle exception
throw new ClassCastException(activity.toString()+"must import OnNewItemAddedListener");
}
}

     
}

3.创建包含列表的Fragment  ,Android提供ListFragment类,它可恨容易地创建基于Fragment的简单ListView   创建一个新的继承自ListFragment 类,ListFragment类包含一个单独的ListView 组成的默认UI  对于这个例子来说这已经足够

  public class ToDoListFragment extends ListFragment {


}


4.完成了Fragment  该返回Activity 了,将两个Fragment 添加到main.xml 布局里,将ListView 与 EditText分别替换为ToDoListFragment 和 NewItemFragment 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".TodoListActivity"
    >
   <fragment android:name="com.spreadtone.activitytest.NewItemFragment"
       android:id="@+id/NewItemFragment"
       android:layout_width="match_parent"
       android:layout_height="wrap_content" 
   />
    <fragment android:name="com.spreadtone.activitytest.ToDoListFragment"
       android:id="@+id/ToDoListFragment"
       android:layout_width="match_parent"
       android:layout_height="wrap_content" 
   />

</LinearLayout>

5  返回到 ToDoListActivity  在OnCreate 方法中  在给ToDoList  Fragment 创建和分配适配器之前  先通过Fragment Manager 获取ToDoListFragment 的一个引用  因为ListView 和EditText Views 此时封装在Fragment中 所以不需要在Activity 中获取它们的引用


public class TodoListActivity extends Activity implements NewItemFragment.OnNewItemAddedListener {
private ArrayList<String> todoItems;
private ArrayAdapter<String> aa;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.todolist);

todoItems = new ArrayList<String>();
aa=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, todoItems);

       FragmentManager fm =getFragmentManager();
       ToDoListFragment todoListFragment = (ToDoListFragment) fm.findFragmentById(R.id.ToDoListFragment);
       todoListFragment.setListAdapter(aa);
       
}


@Override
public void onNewItemAdded(String newItem) {
// TODO Auto-generated method stub
todoItems.add(newItem);
aa.notifyDataSetChanged();
}


}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值