摘要:通过注册一个侦听返回栈变化的侦听器。执行片段事务中使用片段的一大优点是,可以根据用户行为通过它们执行添加移除替换以及其他操作。当通过该接口收到回调时,可以根据需要与布局中的其他片段共享这些信息。
目录
通过创建Fragment子类
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.ViewGroup;
public class ArticleFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.article_view, container, false);
}
}
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class MainActivity extends FragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.news_articles);
}
}
2.1. fragment的生命周期
生命周期 | 含义 | 主要内容 |
---|---|---|
onCreate() | 系统会在创建片段时调用此方法 | 初始化组件 |
onCreateView() | 系统会在片段首次绘制其用户界面时调用此方法。 要想为您的片段绘制 UI 您从此方法中返回的 View 必须是片段布局的根视图 |
|
onPause() | 系统将此方法作为用户离开片段的第一个信号(但并不总是意味着此片段会被销毁)进行调用 | 确认在当前用户会话结束后仍然有效的任何更改 |
example_fragment.xml
fragment
类中加载布局public static class ExampleFragment extends Fragment {
// container:您的片段布局将插入到的父 ViewGroup
@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);
}
}
activity
中添加片段
Activity
布局时,会实例化在布局中指定的每个片段,并为每个片段调用 onCreateView()
方法,以检索每个片段的布局。
// 必须使用 FragmentTransaction 中的 API
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
// 使用 add() 方法添加一个片段,指定要添加的片段以及将其插入哪个视图
ExampleFragment fragment = new ExampleFragment();
fragmentTransaction.add(R.id.fragment_container, fragment);
// 调用 commit() 以使更改生效
fragmentTransaction.commit();
FragmentManager的执行操作包括:
findFragmentById()
(对于在Activity
布局中提供 UI
的片段)或 findFragmentByTag()
(对于提供或不提供 UI 的片段)获取 Activity
中存在的片段。popBackStack()
(模拟用户发出的返回命令)将片段从返回栈中弹出。addOnBackStackChangedListener()
注册一个侦听返回栈变化的侦听器。// 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();
3.2. 与Activity通信
getActivity()
访问Activity
实例,并轻松地执行在Activity
布局中查找视图等任务。Activity
也可以使用findFragmentById()
或 findFragmentByTag()
,通过从FragmentManager
获取对 Fragment
的引用来调用片段中的方法// fragment - > activity
View listView = getActivity().findViewById(R.id.list);
// activity - > fragment
ExampleFragment fragment = (ExampleFragment) getFragmentManager().findFragmentById(R.id.example_fragment);
(1) 创建对Activity的事件回调
//fragment定义接口
public static class FragmentA extends ListFragment {
...
// Container Activity must implement this interface
public interface OnArticleSelectedListener {
public void onArticleSelected(Uri articleUri);
}
...
OnArticleSelectedListener mListener;
...
// 确保宿主 Activity 实现此接口
// 通过转换传递到 onAttach() 中的 Activity 来实例化 OnArticleSelectedListener 的实例
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (OnArticleSelectedListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement OnArticleSelectedListener");
}
}
// 列表点击事件
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);
}
...
}
4. fragment与activity的生命周期关系
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class MainActivity extends FragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.news_articles);
// Check that the activity is using the layout version with
// the fragment_container FrameLayout
if (findViewById(R.id.fragment_container) != null) {
// However, if we"re being restored from a previous state,
// then we don"t need to do anything and should return or else
// we could end up with overlapping fragments.
if (savedInstanceState != null) {
return;
}
// Create a new Fragment to be placed in the activity layout
HeadlinesFragment firstFragment = new HeadlinesFragment();
// In case this activity was started with special instructions from an
// Intent, pass the Intent"s extras to the fragment as arguments
firstFragment.setArguments(getIntent().getExtras());
// Add the fragment to the "fragment_container" FrameLayout
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, firstFragment).commit();
}
}
}
由于该片段已在运行时被添加到 FrameLayout
容器,可以从该Activity
中移除该片段,并将其替换为其他片段。
// Create fragment and give it an argument specifying the article it should show
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();
6. 实例,新闻页面
https://blog.csdn.net/zhaoyanga14/article/details/52166491
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/2531.html
摘要:原文链接正式版发布啦今天是个大日子,终于发布了正式版,这对于开发者来说简直是喜大普奔的大消息啊,那么就果断来下载使用。官方下载地址如果你之前已经使用其他版本的,那么直接覆盖就好了,如果是第一次使用,那么参照系列教程一进行安装配置。 原文链接:http://stormzhang.com/devtools/2014/12/09/android-studio-tutorial3/ ...
摘要:我是如何自学,资料分享最近知乎上有网友问我怎么自学,其实说实在的,我学的也一塌糊涂,当然在学习过程也积累了一些知识,对于以前没接触过的朋友,或者刚入门的朋友,这篇文章作为入门,那是再合适不过了,希望对刚入门的朋友有帮助,接下来,就不罗嗦啦。 我是如何自学Android,资料分享 最近知乎上有网友问我怎么自学Android,其实说实在的,我学的也一塌糊涂,当然在学习过程也积累了一些知识,...
摘要:让你收获满满码个蛋从年月日推送第篇文章一年过去了已累积推文近篇文章,本文为年度精选,共计篇,按照类别整理便于读者主题阅读。本篇文章是今年的最后一篇技术文章,为了让大家在家也能好好学习,特此花了几个小时整理了这些文章。 showImg(https://segmentfault.com/img/remote/1460000013241596); 让你收获满满! 码个蛋从2017年02月20...
摘要:云端和上显示的地址一定要一致这时在云手机的页面,你会看到请求远程连接的提示,点击确定。现在你可以用调试了当你的电脑通过前面的操作连接到了云手机,你就获得了这台手机的最大控制权。 用了很久的模拟器,今天给大家分享一个不用模拟器,在没有手机的情况下,如何实现真机debug的教程,第一次发文章,非喜勿喷,望支持下! 废话少说,先睹为快。在不用数据线连接手机的情况下,先来个打断点截图。。。 s...
摘要:目录将添加到布局主中如何调用对象重写定义实例将添加到布局此文件命名为线性布局网格布局瀑布布局主中如何调用对象的三部曲获取对象添加水平分割线如果传入则该布局支持纵向滑动,那么前面的则指的是列。目录 1. RecyclerView 1.1. Add support library 1.2. 将RecyclerView添加...
阅读 877·2021-11-24 09:39
阅读 331·2021-11-23 09:51
阅读 4105·2021-11-22 09:34
阅读 575·2021-09-07 09:59
阅读 3412·2021-09-02 15:21
阅读 2031·2021-08-24 10:01
阅读 561·2021-08-19 10:55
阅读 2220·2019-08-30 15:55