我的问题是关于片段和 ActionBar (android.support.v7.app.ActionBar)
我遵循了这个例子,特别是“添加导航选项卡”部分
该示例工作正常,但是当我想用另一个片段更改选项卡(因此片段)的内容时遇到问题。
在此示例中,我在第一个片段“Tab_Fragment_1”(在第一个选项卡下)添加了一个按钮。按下按钮时,我想更改内容(从而加载另一个片段),显然在同一个选项卡下。
在 MainActivity 我添加了方法“clickbutton”,它在 tab_fragment_1'layout 中定义
我不知道要遵循的模式。我该怎么做?
下面是我的代码:
主要活动
public class MainActivity extends ActionBarActivity {
private String TAG="MainActivity";
Tab tab =null;
Tab_Fragment_2A tab_Fragment_2A=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "msg-1");
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayShowTitleEnabled(false);
tab = actionBar.newTab()
.setText(R.string.tab_1)
.setTabListener(new TabListener<Tab_Fragment_1>(
this, "tab_1", Tab_Fragment_1.class));
actionBar.addTab(tab);
tab = actionBar.newTab()
.setText(R.string.tab_2)
.setTabListener(new TabListener<Tab_Fragment_2>(
this, "tab_2", Tab_Fragment_2.class));
actionBar.addTab(tab);
tab = actionBar.newTab()
.setText(R.string.tab_3)
.setTabListener(new TabListener<Tab_Fragment_3>(
this, "tab_3", Tab_Fragment_3.class));
actionBar.addTab(tab);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
Log.d(TAG, "msg-2");
getMenuInflater().inflate(R.menu.main, menu);
Log.d(TAG, "msg-3");
return true;
}
public static class TabListener<T extends Fragment> implements ActionBar.TabListener {
private Fragment mFragment;
private final Activity mActivity;
private final String mTag;
private final Class<T> mClass;
/** Constructor used each time a new tab is created.
* @param activity The host Activity, used to instantiate the fragment
* @param tag The identifier tag for the fragment
* @param clz The fragment's Class, used to instantiate the fragment
*/
public TabListener(Activity activity, String tag, Class<T> clz) {
mActivity = activity;
mTag = tag;
mClass = clz;
}
/* The following are each of the ActionBar.TabListener callbacks */
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// Check if the fragment is already initialized
if (mFragment == null) {
// If not, instantiate and add it to the activity
mFragment = Fragment.instantiate(mActivity, mClass.getName());
ft.add(android.R.id.content, mFragment, mTag);
} else {
// If it exists, simply attach it in order to show it
ft.attach(mFragment);
}
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
if (mFragment != null) {
// Detach the fragment, because another one is being attached
ft.detach(mFragment);
}
}
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// User selected the already selected tab. Usually do nothing.
}
}
public void clickbutton(View v) {
Log.d(TAG, "button clicked");
//to do?
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
</RelativeLayout>
Tab_Fragment_1 类:
public class Tab_Fragment_1 extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.tab_fragment_1,container, false);
return view;
}
}
tab_fragment_1:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/Azure">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:onClick="clickbutton" />
</LinearLayout>