80,359
社区成员
发帖
与我相关
我的任务
分享
// 创建一个Bundle对象,用于向Fragment传入参数
Bundle args = new Bundle();
args.putInt(DummyFragment.ARG_SECTION_NUMBER,
tab.getPosition() + 1);
// 创建一个Bundle对象,用于向Fragment传入参数
Bundle args = new Bundle();
args.putInt("section_number",tab.getPosition() + 1);
// 获取创建该Fragment时传入的参数Bundle
Bundle args = getArguments();
// 设置TextView显示的文本
textView.setText(args.getInt("section_number") + "");
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
package org.crayzit.ui;
import android.app.Fragment;
import android.os.Bundle;
//import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
/**
* Description:
* <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
* <br/>Copyright (C), 2001-2014, Yeeku.H.Lee
* <br/>This program is protected by copyright laws.
* <br/>Program Name:
* <br/>Date:
* @author Yeeku.H.Lee kongyeeku@163.com
* @version 1.0
*/
public class DummyFragment extends Fragment
{
public static final String ARG_SECTION_NUMBER = "section_number";
// 该方法的返回值就是该Fragment显示的View组件
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
TextView textView = new TextView(getActivity());
textView.setGravity(Gravity.CENTER_HORIZONTAL);
// 获取创建该Fragment时传入的参数Bundle
Bundle args = getArguments();
// 设置TextView显示的文本
textView.setText(args.getInt(ARG_SECTION_NUMBER) + "");
textView.setTextSize(30);
// 返回该TextView
return textView;
}
}
package org.crayzit.ui;
import android.app.ActionBar;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
/**
* Description:
* <br/>website: <a href="http://www.crazyit.org">crazyit.org</a>
* <br/>Copyright (C), 2001-2014, Yeeku.H.Lee
* <br/>This program is protected by copyright laws.
* <br/>Program Name:
* <br/>Date:
* @author Yeeku.H.Lee kongyeeku@163.com
* @version 1.0
*/
public class ActionBar_TabNav extends Activity implements
ActionBar.TabListener
{
private static final String SELECTED_ITEM = "selected_item";
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ActionBar actionBar = getActionBar();
// 设置ActionBar的导航方式:Tab导航
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// 依次添加3个Tab页,并为3个Tab标签添加事件监听器
actionBar.addTab(actionBar.newTab().setText("第一页")
.setTabListener(this));
actionBar.addTab(actionBar.newTab().setText("第二页")
.setTabListener(this));
actionBar.addTab(actionBar.newTab().setText("第三页")
.setTabListener(this));
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState)
{
if (savedInstanceState.containsKey(SELECTED_ITEM))
{
// 选中前面保存的索引对应的Fragment页
getActionBar().setSelectedNavigationItem(
savedInstanceState.getInt(SELECTED_ITEM));
}
}
@Override
public void onSaveInstanceState(Bundle outState)
{
// 将当前选中的Fragment页的索引保存到Bundle中
outState.putInt(SELECTED_ITEM,
getActionBar().getSelectedNavigationIndex());
}
@Override
public void onTabUnselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction)
{
}
// 当指定Tab被选中时激发该方法
@Override
public void onTabSelected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction)
{
// 创建一个新的Fragment对象
Fragment fragment = new DummyFragment();
// 创建一个Bundle对象,用于向Fragment传入参数
Bundle args = new Bundle();
args.putInt(DummyFragment.ARG_SECTION_NUMBER,
tab.getPosition() + 1);
// 向fragment传入参数
fragment.setArguments(args);
// 获取FragmentTransaction对象
FragmentTransaction ft = getFragmentManager()
.beginTransaction();
// 使用fragment代替该Activity中的container组件
ft.replace(R.id.container, fragment);
// 提交事务
ft.commit();
}
@Override
public void onTabReselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction)
{
}
}
// 创建一个Bundle对象,用于向Fragment传入参数
Bundle args = new Bundle();
args.putInt("section_number",tab.getPosition() + 1);
接收数据:// 获取创建该Fragment时传入的参数Bundle
Bundle args = getArguments();
// 设置TextView显示的文本
textView.setText(args.getInt("section_number") + "");
我对我上面的代码的理解是,在A中向Bundle bundle中存入数据,数据名为“name”,数据为“data”。在B中接收数据,使用Bundle bundle.name获得的数据就是“data”。
如果我上面的方法可以的话,那按照问题中程序的写法,在开头写“public static final String ARG_SECTION_NUMBER = "section_number";”有什么好处嘛?可以帮我说下嘛?谢谢。[/quote]好处就是避免名称写错。你直接将它定义成常量,然后直接通过常量来操作就可以避免这些细节错误。// 创建一个Bundle对象,用于向Fragment传入参数
Bundle args = new Bundle();
args.putInt("section_number",tab.getPosition() + 1);
接收数据:// 获取创建该Fragment时传入的参数Bundle
Bundle args = getArguments();
// 设置TextView显示的文本
textView.setText(args.getInt("section_number") + "");
我对我上面的代码的理解是,在A中向Bundle bundle中存入数据,数据名为“name”,数据为“data”。在B中接收数据,使用Bundle bundle.name获得的数据就是“data”。
如果我上面的方法可以的话,那按照问题中程序的写法,在开头写“public static final String ARG_SECTION_NUMBER = "section_number";”有什么好处嘛?可以帮我说下嘛?谢谢。// 创建一个Bundle对象,用于向Fragment传入参数
Bundle args = new Bundle();
args.putInt("section_number",tab.getPosition() + 1);
接收数据:// 获取创建该Fragment时传入的参数Bundle
Bundle args = getArguments();
// 设置TextView显示的文本
textView.setText(args.getInt("section_number") + "");
我对我上面的代码的理解是,在A中向Bundle bundle中存入数据,数据名为“name”,数据为“data”。在B中接收数据,使用Bundle bundle.name获得的数据就是“data”。
如果我上面的方法可以的话,那按照问题中程序的写法,在开头写“public static final String ARG_SECTION_NUMBER = "section_number";”有什么好处嘛?可以帮我说下嘛?谢谢。[/quote]你这么写完全可以,好处就是万一你手误把字符串写错了,导致你取数据取不到,声明字符串变量可以避免此问题!