android 文档阅读记录-添加fragment
来源:互联网 发布:营销活动数据分析报告 编辑:程序博客网 时间:2024/04/30 23:19
1.在xml文件里面直接添加Fragment
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent"> <fragment android:name="com.example.android.fragments.HeadlinesFragment" android:id="@+id/headlines_fragment" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent" /> <fragment android:name="com.example.android.fragments.ArticleFragment" android:id="@+id/article_fragment" android:layout_weight="2" android:layout_width="0dp" android:layout_height="match_parent" /></LinearLayout>当你在xml文件里面直接添加Fragment的时候,在程序运行时的时候是不能移除的,如果你打算在运行的时候根据用户的互动来切换,那么你只能通过activity 在onceat的时候添加和管理。
2.如何创建灵活的Fragment
首先需要在XML里面加上
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/fragment_container" android:layout_width="match_parent" android:layout_height="match_parent" />
HeadlinesFragment firstFragment = new HeadlinesFragment();//创建Fragment实例3.Fragment 通信 通过接口的方式实现 Fragment 和其他 Fragment以及 activity之间的通信getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, firstFragment).commit();//通过FragmentTransaction 将fragment加入activity
public class HeadlinesFragment extends ListFragment { OnHeadlineSelectedListener mCallback; // Container Activity must implement this interface public interface OnHeadlineSelectedListener { public void onArticleSelected(int position); } @Override public void onAttach(Activity activity) { super.onAttach(activity); //确保 要和此fragment通信的容器 实现了OnHeadlineSelectedListener 接口 否则抛出 没有实现接口的异常 try { mCallback = (OnHeadlineSelectedListener) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement OnHeadlineSelectedListener"); } } ...}这样 Fragment就可以通过调用onArticleSelected()方法将信息传到Activity里面去
简单实例:
@Override public void onListItemClick(ListView l, View v, int position, long id) { // Send the event to the host activity mCallback.onArticleSelected(position); }当Fragment 里面的listview 点击时,就会将这个position 传到activity里面去,当然也可以传其他的参数,执行相应的操作。
从Activity传递消息到Fragment里面
public void onArticleSelected(int position) { // The user selected the headline of an article from the HeadlinesFragment // Do something here to display that article ArticleFragment articleFrag = (ArticleFragment) getSupportFragmentManager().findFragmentById(R.id.article_fragment); if (articleFrag != null) { // If article frag is available, we're in two-pane layout... // Call a method in the ArticleFragment to update its content articleFrag.updateArticleView(position); } else { // Otherwise, we're in the one-pane layout and must swap frags... // Create fragment and give it an argument for the selected article ArticleFragment newFragment = new ArticleFragment(); Bundle args = new Bundle(); args.putInt(ArticleFragment.ARG_POSITION, position); newFragment.setArguments(args); FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); // Replace whatever is in the fragment_container view with this fragment, // and add the transaction to the back stack so the user can navigate back transaction.replace(R.id.fragment_container, newFragment); transaction.addToBackStack(null); // Commit the transaction transaction.commit(); } }
0 0
- android 文档阅读记录-添加fragment
- Android官方文档阅读记录
- 阅读android中文开发文档笔记---Fragment
- iptables阅读文档记录
- Android记录官方文档系列(2):Fragment(上)
- Android记录官方文档系列(3):Fragment(中)
- android 官方文档阅读记录-多屏幕适配
- android文档之Fragment
- Android:Fragment官方文档
- android Fragment(文档中文版)
- Android Fragment异常记录
- Android 阅读PDF文档
- Android 添加Fragment到回退栈
- Android 动态添加Fragment
- Android 动态添加Fragment
- Android 动态添加Fragment
- 【Android】Fragment官方中文文档
- Android文档Training之Fragment
- LD_PRELOAD
- None of the configured nodes are available #transport#-1
- 三种方案实现Android应用的环境分离
- 计算机视觉中的多视图几何——点与直线的关系
- 关于 spingMVC aop和ioc的理解与概念
- android 文档阅读记录-添加fragment
- 树状数组
- 常用的11个git命令
- 1123常识
- STM32F4 DMA
- 当Butterknife遇到Dagger
- 2016.11.25总结
- 让Activity更加简洁第一篇---MVP模式
- API 25 (Android 7.1.1 API) widget.FrameLayout