API Guides - App Components
来源:互联网 发布:甘肃省精准扶贫大数据 编辑:程序博客网 时间:2024/05/19 19:35
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 { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return 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 transactionFragment 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 stacktransaction.replace(R.id.fragment_container, newFragment);transaction.addToBackStack(null);// Commit the transactiontransaction.commit();commin()之前使用 setTransition() 可设置动画
Communicating with the Activity
public static class FragmentA extends ListFragment { ... // Container Activity must implement this interface public interface OnArticleSelectedListener { public void onArticleSelected(Uri articleUri); } ...}
public static class FragmentA extends ListFragment { OnArticleSelectedListener mListener; ... @Override public 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; ... @Override public void onListItemClick(ListView l, View v, int position, long id) { // Append the clicked item's row ID with the content provider Uri Uri noteUri = ContentUris.withAppendedId(ArticleColumns.CONTENT_URI, id); // Send the event and Uri to the host activity mListener.onArticleSelected(noteUri); } ...}
fragment 还可以给Action Bar 增加按钮,具体
Adding items to the Action Bar //TODO
- API Guides - App Components
- Android API Guides - App Components
- Android API Guides 之 App Components(16) - App Widgets
- API Guides/App Components/Process and Thread翻译
- Android API Guides 之 App Components(3) - Activities
- Android API Guides 之 App Components(4) - Activities - Fragments
- Android API Guides 之 App Components(5) - Activities - Loaders
- Android API Guides 之 App Components(7) - Services
- Android API Guides 之 App Components(9) - Services - AIDL
- Android API Guides 之 App Components(10) - Content Providers
- Android API Guides---Custom Components
- Android API Guides 之 App Components(17) - App Widgets - App Widget Host
- Android API Guides 之 App Components(1) - Intents and Intent Filters
- Android API Guides 之 App Components(2) - Intents and Intent Filters - Common Intents
- Android API Guides 之 App Components(6) - Activities - Task and Back Stack
- Android API Guides 之 App Components(8) - Services - Bound Services
- Android API Guides 之 App Components(11) - Content Providers - Basics
- Android API Guides 之 App Components(12) - Content Providers - Creating
- java快速排序
- 面向对象:Close的问题
- [Android JNI] JNI Types and Data Structures
- android listview 用simplecursoradapter适配器数据刷新
- Linux下core文件调试方法
- API Guides - App Components
- 给超链接自定义一个右键菜单
- 根据包名取得应用全部信息ResolveInfo
- 文本框禁止手动输入 只能选择数据到 文本框
- 可执行的android C 程序
- 使用maven 构建hibernate应用
- c++智能指针的设计和使用
- Java 反射机制
- c++ 里面由浅拷贝引起的悬挂指针