简体   繁体   English

在ActionBarSherlock的NavigationTab示例中使用片段

[英]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.

相关问题 ActionBarSherlock片段显示与传递的数据不同的数据 - ActionBarSherlock fragment showing different data than passed 使用Robolectric + ActionBarSherlock时发生异常 - Exception when using Robolectric + ActionBarSherlock 无法使用actionbarsherlock创建actionbar菜单项 - Cannot create actionbar menu item using actionbarsherlock 无法使用ActionBarsherlock 4.4.0版读取布局 - Layout cannot be read using actionbarsherlock version 4.4.0 使用ActionBarSherlock和FacebookSdk时Jar不匹配 - Jar Mismatch while using ActionBarSherlock and FacebookSdk 使用ActionBarSherlock 4.1.0时资源文件中的错误 - Errors in resource files when using ActionBarSherlock 4.1.0 使用ActionBarSherlock片段的按钮中的NullPointerException setOnClickListener - NullPointerException setOnClickListener in a button using ActionBarSherlock fragments 如何在 Android 应用程序中使用 Gradle 导入 ActionBarSherlock? - How to import ActionBarSherlock using Gradle in Android app? 我想使用ActionBarSherlock实现滑动菜单吗? - I want to implement Sliding Menu using ActionBarSherlock? 在带有actionBarSherlock的SherlockFragmentActivity中使用Google Map apiV2全屏的问题 - Issue using google map apiV2 full screen in SherlockFragmentActivity with actionBarSherlock
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM