[英]Using fragment in NavigationTab sample of ActionBarSherlock
I am trying to make the TabNavigation Sample in ActionBarSherlock works with going to different fragment, not just change the text of a textView (as we have in this sample). 我试图使ActionBarSherlock中的TabNavigation示例适用于不同的片段,而不仅仅是更改textView的文本(如本示例中那样)。 This is my code: 这是我的代码:
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.MenuItem;
import android.widget.Toast;
public class TestNavigation extends SherlockActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(R.style.Theme_Sherlock);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
for (int i = 1; i <= 3; i++) {
ActionBar.Tab tab = getSupportActionBar().newTab();
tab.setText("Tab " + i);
SherlockFragment PlayerFragment = new PhotosActivity();
tab.setTabListener(new MyTabsListener(PlayerFragment));
getSupportActionBar().addTab(tab);
}
}
class MyTabsListener implements ActionBar.TabListener {
public SherlockFragment fragment;
public MyTabsListener(SherlockFragment fragment) {
this.fragment = fragment;
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
Toast.makeText(getApplicationContext(), "Reselected!", Toast.LENGTH_LONG).show();
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
ft.replace(R.id.fragment_container, fragment);
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
ft.remove(fragment);
}
}
And main.xml: 和main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_gravity="center">
<LinearLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</LinearLayout>
</LinearLayout>
And photoActivity is very simple: 而且photoActivity非常简单:
public class PhotosActivity extends SherlockFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.photos_layout, container, false);
}
}
could you help me? 你可以帮帮我吗?
If you think I am in the wrong way, please give me some sample or toturial that I can follow. 如果您认为我的方法有误,请给我一些示例或提示,供我参考。
Change your activity to extends SherlockFragmentActivity not SherlockActivity and one more thing is to move ft.replace(R.id.fragment_container, fragment);
更改您的活动以扩展SherlockFragmentActivity而不是SherlockActivity,另一件事是移动ft.replace(R.id.fragment_container, fragment);
to onTabSelected(Tab tab, FragmentTransaction ft)
method and i don't think you need ft.remove(fragment);
到onTabSelected(Tab tab, FragmentTransaction ft)
方法,我认为您不需要ft.remove(fragment);
that is inside onTabReselected()
because it will remove your fragment if the tab is pressed while it is selected 它位于onTabReselected()
因为如果在选择该选项卡时按下该选项卡,它将删除您的片段
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.