本文主要是介绍API Guides - App Components,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Activities //TODO
Fragments
每个activity有一个back stack,里面存放的是 fragment transaction。
fragment必须放入activity (实际放入ViewGroup),状态直接受activity影响。
可用代码创建Fragment,也可用xml <fragment>元素
要求 API Level 11
fragment transaction 即 add, remove, replace a fragment
Fragments.Design Philosophy (done)
Creating a Fragment
1. 创建Fragment 子类,修改回调函数( onCreate(), onStart(), onPause(), onStop)
2. 通常要覆盖onCreate(), onCreateView() -> 第一次绘制UI时,返回root of your UI 也可返回null(无UI),onPause()
3. 可以继承的Fragment子类:DialogFragment, ListFragment, PreferenceFragment
public static class ExampleFragment extends Fragment {@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {// Inflate the layout for this fragmentreturn inflater.inflate(R.layout.example_fragment, container, false);}
}
注:LayoutInflater由参数提供。注意第三个参数为false。
4. 添加到layout: 1) 用xml (静态方式) 2)用代码(动态方式, FragmentTransaction类,
<?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="match_parent"><fragment android:name="com.example.news.ArticleListFragment"android:id="@+id/list"android:layout_weight="1"android:layout_width="0dp"android:layout_height="match_parent" /><fragment android:name="com.example.news.ArticleReaderFragment"android:id="@+id/viewer"android:layout_weight="2"android:layout_width="0dp"android:layout_height="match_parent" /> </LinearLayout>
代码方式:
FragmentManager fragmentManager = getFragmentManager()
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();ExampleFragment fragment = new ExampleFragment();
fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.commit();
fragment还可以没有UI,提供一种后台服务. 用FragmentTransaction.add(Fragment, String)版本的函数,而且无需实现 onCreateView(),而且没有id,只能用string tag访问。
findFragmentByTag()
后台fragment的例子:FragmentRetainInstance.java
Managing Fragments
用 FragmentManager 管理 fragment, getFragmentManager()
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
(builder模式)
调用commin()之前,可用 addToBackStack(),将transanction放入 back stack.
// Create new fragment and transaction
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);// Commit the transaction
transaction.commit();
commin()之前使用 setTransition() 可设置动画
Communicating with the Activity
public static class FragmentA extends ListFragment {...// Container Activity must implement this interfacepublic interface OnArticleSelectedListener {public void onArticleSelected(Uri articleUri);}...
}
public static class FragmentA extends ListFragment {OnArticleSelectedListener mListener;...@Overridepublic void onAttach(Activity activity) {super.onAttach(activity);try {mListener = (OnArticleSelectedListener) activity;} catch (ClassCastException e) {throw new ClassCastException(activity.toString() + " must implement OnArticleSelectedListener");}}...
}
public static class FragmentA extends ListFragment {OnArticleSelectedListener mListener;...@Overridepublic void onListItemClick(ListView l, View v, int position, long id) {// Append the clicked item's row ID with the content provider UriUri noteUri = ContentUris.withAppendedId(ArticleColumns.CONTENT_URI, id);// Send the event and Uri to the host activitymListener.onArticleSelected(noteUri);}...
}
fragment 还可以给Action Bar 增加按钮, 具体
Adding items to the Action Bar //TODO
这篇关于API Guides - App Components的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!