在实现一个Fragment时,通常需要override以下方法
onCreate(),完成初始化功能onCreateView(),Fragment首次绘制UI时会调用该方法,返回根View,如果Fragment不需要UI,则返回nullonPause(),commit changes几种Fragment
DialogFragmentListFragmentPreferenceFragment创建Fragment的几种方法
布局文件里声明 <fragment android:name="com.example.news.ArticleListFragment" android:id="@+id/list" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent" />
Programmatically (in Activity) FragmentManager fm = getFragmentManager(); FragmentTransaction ft = fm.beginTransaction(); SampleFragment f = new SampleFragment(); ft.add(R.id.fragment_container, f); fm.commit();
无UI的Fragment
使用 add(Fragment fragment, String tag) 进行添加无需重写 onCreateView 方法使用 findFragmentByTag 获取该FragmentFragmentManager 的作用
获取Fragment:findFragmentById(有UI),findFragmentByTag(无UI)使Fragment出栈,popBackStack(),模拟用户点击Back按钮。(用处何在?)在BackStack中增加Listener,addOnBackStackChangeListener()使用BackStack来存储Fragment的状态
// 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();
在commit()前使用setTransaction()来增加动画效果
彼此获取引用
Fragment通过getActivity()来获取宿主Activity的引用Activity通过getFragmentManager().findFragmentById()来获取嵌入的Fragment引用在Fragment中实现对Activity的Callback:声明一个接口,在onAttach()时将实现该接口的Activity引用保存,在需要通知Activity时调用接口中的方法
public static class FragmentA extends ListFragment { private OnArticleSelectedListener mListener; ... // Container Activity must implement this interface public interface OnArticleSelectedListener { public void onArticleSelected(Uri articleUri); } ... @Override public void onAttach(Activity activity) { super.onAttach(activity); try { mListener = (OnArticleSelectedListener) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement OnArticleSelectedListener"); } } ... @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可以注册Options Menu与Context Menu,从而接收到onOptionsItemSelected()等事件
Fragment的生命周期与Activity类似
create阶段,onAttach() -> onCreate() -> onCreateView() -> onActivityCreated()最大的不同在于Activity自动入栈,而Fragment需要显式声明几个方法:onCreate(),onCreateView(),onViewCreated(),onActivityCreated() public void onCreate(Bundle),完成Fragment初始化工作,在onAttach(Activity)之后、onCreateView(LayoutInflater, ViewGroup, Bundle)之前发生,与宿主Activity的onCreate()方法之间没有必然的先后关系,不可以依赖宿主Activity的变量public View onCreateView(LayoutInflater, ViewGroup, Bundle),创建Fragment的布局,对于非可见的Fragment,可以不重载此方法或者返回null。发生在onCreate(Bundle)之后、onActivityCreated(Bundle)之前public void onViewCreated(View, Bundle),发生在onCreateView(LayoutInflater, ViewGroup, Bundle)之后、该Fragment附加到Parent之前。This gives the subclasses a chance to initialize themselves once they know their view hierarchy has been completely created.public void onActivityCreated(Bundle),发生在onCreateView(LayoutInflater, ViewGroup, Bundle)后,可以进行retrive views、restore state等动作在宿主Activity的Resume阶段,可以自由进行Fragment的增添/删除
用如下方法将dp换算为padding单位
int padding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4, getActivity().getResources().getDisplayMetrics());
转载于:https://www.cnblogs.com/maozhige/p/4067329.html