0

我正在尝试使 ActionBarSherlock 中的 TabNavigation 示例适用于转到不同的片段,而不仅仅是更改 textView 的文本(正如我们在此示例中所做的那样)。这是我的代码:

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);
        }   
     }

和 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>

而 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);
    }   
}

你可以帮帮我吗?

如果您认为我的方法不对,请给我一些我可以遵循的示例或教程。

4

1 回答 1

1

将您的活动更改为扩展 SherlockFragmentActivity 而不是 SherlockActivity,还有一件事是移动ft.replace(R.id.fragment_container, fragment);onTabSelected(Tab tab, FragmentTransaction ft)方法,我认为您不需要ft.remove(fragment);在里面, onTabReselected()因为如果在选择选项卡时按下选项卡,它将删除您的片段

于 2012-12-29T17:38:07.257 回答