把条目添加到动作栏
你的fragment们可以向activity的菜单(按Manu键时出现的东西)添加项,同时也可向动作栏(界面中顶部的那个区域)添加条目,这都需通过实现方法onCreateOptionManu()来完成。
你从fragment添加到菜单的任何条目,都会出现在现有菜单项之后。Fragment之后可以通过方法onOptionsItemSelected()来响应自己的菜单项被选择的事件。
你也可以在fragemnt中注册一个view来提供快捷菜单(上下文菜单)。当用户要打开快捷菜单时,fragment的onCreateContextMenu()方法会被调用。当用户选择其中一项时,fragemnt的onContextItemSelected()方法会被调用。
注:尽管你的fragment可以分别收到它所添加的菜单项的选中事件,但是activity才是第一个接收这些事件的家伙,只有当activity对某个事件置之不理时,fragment才能接收到这个事件,对于菜单和快捷菜单都是这样。
下例中实验了之前所讲的所有内容。此例有一个activity,其含有两个fragment。一个显示莎士比亚剧的播放曲目,另一个显示选中曲目的摘要。此例还演示了如何跟据屏幕大小配置fragment。
MainActivity:
@Override
protectedvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_layout);
}
Layout.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent" android:layout_height="match_parent">
<fragment class="com.example.android.apis.app.FragmentLayout$TitlesFragment"
android:id="@+id/titles" android:layout_weight="1"
android:layout_width="0px" android:layout_height="match_parent" />
<FrameLayout android:id="@+id/details" android:layout_weight="1"
android:layout_width="0px" android:layout_height="match_parent"
android:background="?android:attr/detailsElementBackground" />
</LinearLayout>
系统在activity加载此layout时初始化TitlesFragment(用于显示标题列表),TitlesFragment的右边是一个FrameLayout,用于存放显示摘要的fragment,但是现在它还是空的,fragment只有当用户选择了一项标题后,摘要fragment才会被放到FrameLayout中。
然而,并不是所有的屏幕都有足够的宽度来容纳标题列表和摘要。所以,上述layout只用于横屏,现把它存放于ret/layout-land/fragment_layout.xml。
之外,当用于竖屏时,系统使用下面的layout,它存放于ret/layout/fragment_layout.xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<fragment class="com.example.android.apis.app.FragmentLayout$TitlesFragment"
android:id="@+id/titles"
android:layout_width="match_parent" android:layout_height="match_parent" />
</FrameLayout>
这个
layout只包含
TitlesFragment。这表示当使用竖屏时,只显示标题列表。当用户选中一项时,程序会启动一个新的
activity去显示摘要,而不是加载第二个
fragment。
下一步,你会看到Fragment类的实现。第一个是TitlesFragment,它从ListFragment派生,大部分列表的功能由ListFragment提供。
当用户选择一个Title时,代码需要做出两种行为,一种是在同一个activity中显示创建并显示摘要fragment,另一种是启动一个新的activity。
public static class TitlesFragment extends ListFragment {
boolean mDualPane;
int mCurCheckPosition = 0;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Populate list with our static array of titles.
setListAdapter(new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_activated_1, Shakespeare.TITLES));
// Check to see if we have a frame in which to embed the details
// fragment directly in the containing UI.
View detailsFrame = getActivity().findViewById(R.id.details);
mDualPane = detailsFrame != null && detailsFrame.getVisibility() == View.VISIBLE;
if (savedInstanceState != null) {
// Restore last state for checked position.
mCurCheckPosition = savedInstanceState.getInt("curChoice", 0);
}
if (mDualPane) {
// In dual-pane mode, the list view highlights the selected item.
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
// Make sure our UI is in the correct state.
showDetails(mCurCheckPosition);
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("curChoice", mCurCheckPosition);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
showDetails(position);
}
/**
* Helper function to show the details of a selected item, either by
* displaying a fragment in-place in the current UI, or starting a
* whole new activity in which it is displayed.
*/
void showDetails(int index) {
mCurCheckPosition = index;
if (mDualPane) {
// We can display everything in-place with fragments, so update
// the list to highlight the selected item and show the data.
getListView().setItemChecked(index, true);
// Check what fragment is currently shown, replace if needed.
DetailsFragment details = (DetailsFragment)
getFragmentManager().findFragmentById(R.id.details);
if (details == null || details.getShownIndex() != index) {
// Make new fragment to show this selection.
details = DetailsFragment.newInstance(index);
// Execute a transaction, replacing any existing fragment
// with this one inside the frame.
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.details, details);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
}
} else {
// Otherwise we need to launch a new activity to display
// the dialog fragment with selected text.
Intent intent = new Intent();
intent.setClass(getActivity(), DetailsActivity.class);
intent.putExtra("index", index);
startActivity(intent);
}
}
第二个
fragment,
DetailsFragment显示被选择的
Title的摘要:
public static class DetailsFragment extends Fragment {
/**
* Create a new instance of DetailsFragment, initialized to
* show the text at 'index'.
*/
public static DetailsFragment newInstance(int index) {
DetailsFragment f = new DetailsFragment();
// Supply index input as an argument.
Bundle args = new Bundle();
args.putInt("index", index);
f.setArguments(args);
return f;
}
public int getShownIndex() {
return getArguments().getInt("index", 0);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null) {
// We have different layouts, and in one of them this
// fragment's containing frame doesn't exist. The fragment
// may still be created from its saved state, but there is
// no reason to try to create its view hierarchy because it
// won't be displayed. Note this is not needed -- we could
// just run the code below, where we would create and return
// the view hierarchy; it would just never be used.
return null;
}
ScrollView scroller = new ScrollView(getActivity());
TextView text = new TextView(getActivity());
int padding = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
4, getActivity().getResources().getDisplayMetrics());
text.setPadding(padding, padding, padding, padding);
scroller.addView(text);
text.setText(Shakespeare.DIALOGUE[getShownIndex()]);
return scroller;
}
}
如果当前的
layout没有
R.id.detailsView(它被用于
DetailsFragment的容器),那么程序就启动
DetailsActivity来显示摘要。
下面是DetailsActivity,它只是简单地嵌入DetailsFragment来显示摘要。
public static class DetailsActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getResources().getConfiguration().orientation
== Configuration.ORIENTATION_LANDSCAPE) {
// If the screen is now in landscape mode, we can show the
// dialog in-line with the list so we don't need this activity.
finish();
return;
}
if (savedInstanceState == null) {
// During initial setup, plug in the details fragment.
DetailsFragment details = new DetailsFragment();
details.setArguments(getIntent().getExtras());
getFragmentManager().beginTransaction().add(android.R.id.content, details).commit();
}
}
}
注意这个
activity在检测到是竖屏时会结束自己,于是主
activity会接管它并显示出
TitlesFragment和
DetailsFragment。这可以在用户在竖屏时显示在
TitleFragment,但用户旋转了屏幕,使显示变成了横屏。
分享到:
相关推荐
androidx-fragment-1.1.0.aar
android Fragment回退栈实践
仿映客主界面fragment的嵌套、fragement、viewpage
Android Fragment使用示例
Android Fragment切换动画
Android fragment切换动画
二、Android Fragment 示例讲解一 2.1 创建 Fragment 2.2 Fragment 管理 2.3 Fragment 与 Activity 通讯 2.4 Fragment 示例 三、Android Fragment 示例讲解二 3.1 项目的效果图 3.2 项目结构图与内容分析...
二、Android Fragment示例讲解一 2.1 创建Fragment 2.2 Fragment 管理 2.3 Fragment 与Activity 通讯 2.4 Fragment 示例 三、Android Fragment示例讲解二 3.1 项目的效果图 3.2 项目结构图与内容分析 3.4 ...
Android Fragment实例,对应的教程也在我的资源里
Fragment是Android honeycomb 3.0新增的概念,在如何使用Android Fragment中做了关于Fragment的详细介绍。本文则主要是通过实例的方式让大家更直观的了解...LinearLayout xmlns:android=“http://schemas.android.co
简单实用的Fragment框架,适用于商城,影音,即时通讯等项目!经测试可用!下载直接运行,如有问题请联系扣扣2691608900!!!
通过fragment 简单的实现页面的切换实现 代码简单 易懂
android fragment demo 源码,切换 android fragment demo 源码,切换
Android Fragment的使用。 功能:用一个Activity来管理两个Fragment,并且在其中一个Fragment中,点击按钮,影响到第二个Fragment。Activity中用list来管理Fragment。fragment布局采用垂直排列。
基于ViewModel 实现 Android Fragment 间信息共享。 例子简单,也是 抠的大神的代码,修改好了之后能跑起来的demo。 希望能给入门者参考。想当初我也是找了 n久 就是找不到合适的demo。 278397935@qq.com
android fragment使用示例 ,很好的学习示例,希望帮到学习中的人。
android-fragment-demo 演示片段在Android中的简单应用
本文主要供学习使用,主要讲解点击不同按钮之后,相应...所以这篇文章是写的比较简单的一个实例,目的就是使用最新的Fragment来实现这种切换效果,希望初学者能够完全理解其中的思路。为以后更复杂的开发打好基础。
Android下Fragment的动画切换效果 ,基于Eclipse,可运行
主要介绍了Android Fragment的回退栈示例详细介绍的相关资料,需要的朋友可以参考下