【问题标题】:Implementing action bar tabs with fragments使用片段实现操作栏选项卡
【发布时间】:2025-12-21 03:25:16
【问题描述】:

所以最近我需要实现将当前片段换成新片段的操作栏选项卡。尽管经过数小时的密集搜索,我还是找不到明确的解决方案,所以我想我会在这里介绍我是如何解决问题的。其中两个片段包含列表视图,结果证明这是一个主要的复杂因素。

【问题讨论】:

    标签: android android-fragments android-actionbar


    【解决方案1】:

    首先创建一个要附加片段的 Activity。在该 Activity 的 XML 文件中添加一个线性布局,如下所示:

    <LinearLayout 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:orientation="vertical"
    tools:context=".WoodenSideProject" >
    <LinearLayout
        android:id="@+id/fragment_placeholder"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    </LinearLayout>   
    

    不要向 XML 文件添加任何其他内容。甚至没有预装 Eclipse 的选项卡主机。

    接下来创建您的片段。首先使用片段的 XML 文件为您的片段构建您想要的 UI。我将展示如何使用列表视图创建片段:

    public class Fragment1Name extends Fragment
    {
    public static String TAG="DirectionsFragment";
    private String[] list_items = {"Put the list of Strings you want here"};
    ListView lView1;
    /*@Override
    public void onActivityCreated(Bundle savedInstanceState)
    {
        super.onActivityCreated(savedInstanceState);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_list_item_1, list_items);
            setListAdapter(adapter);
    }*/
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_XML_title, container, false);
        createListView(view);
        // Inflate the layout for this fragment
        return view;
    }
    
    private void createListView(View view)
    {
        lView1 = (ListView) view.findViewById(R.id.ListViewID);
        //Set option as Multiple Choice. So that user can able to select more the one option from list
        lView1.setAdapter(new ArrayAdapter<String>(getActivity(),
        android.R.layout.simple_list_item_1, list_items));
    }
    }
    

    有些人建议为使用列表视图的片段扩展 ListFragment。我的经验是,麻烦多于其价值。

    如下设置Activity的java文件:

    public class ActivityName extends FragmentActivity {
    public static Context appContext;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_project);
    
    ActionBar actionBar = getActionBar();
    actionBar.setDisplayHomeAsUpEnabled(true);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    setTitle("WoodenSideProject");
    
    ActionBar.Tab tab1 = actionBar.newTab().setText("Tab1");
    ActionBar.Tab tab2 = actionBar.newTab().setText("Tab2");
    ActionBar.Tab tab3 = actionBar.newTab().setText("Tab3");
    
    Fragment Fragment1Name = new Fragment1Name();
    Fragment Fragment2Name = new Fragment2Name();
    Fragment Fragment3Name = new Fragment3Name();
    
    tab1.setTabListener(new MyTabsListener(Fragment1Name));
    tab2.setTabListener(new MyTabsListener(Fragment2Name));
    tab3.setTabListener(new MyTabsListener(Fragment3Name));
    
    actionBar.addTab(tab1);
    actionBar.addTab(tab2);
    actionBar.addTab(tab3);
    }
    

    在同一个 Activity 中创建以下类:

    class MyTabsListener implements ActionBar.TabListener {
    public Fragment fragment;
    
    public MyTabsListener(Fragment fragment) {
        this.fragment = fragment;
    }
    
    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
        //do what you want when tab is reselected, I do nothing
    }
    
    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        ft.replace(R.id.fragment_placeholder, fragment);
    }
    
    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        ft.remove(fragment);
    }
    }
    

    我不知道其他人是否像我用片段实现操作栏选项卡一样遇到麻烦,但如果是的话,我希望这会有所帮助。任何有关更好实施的建议将不胜感激。

    【讨论】:

    • 如果遇到这样的错误怎么办? AndroidRuntime(5485):java.lang.IllegalStateException:失败保存状态:活动已清除索引:-1
    • 然后你会得到一个错误,应用程序会关闭 =)
    • 这是一个比 Google 在指南中提供的更好的 TabListener 实现。