1

我目前有三个选项卡,它们是 ListFragments,但现在我想要一个选项卡,左侧是 ListFragment,右侧是 DetailFragment,就像 Google Dev 中的示例一样。文档在这里。这是我想要的图片:链接

这是一个问题:如何在另一个片段中添加两个片段?关于这个主题的一些问题说我需要使用 API17 中的“嵌套片段”。其他人说我必须使用 FragmentActivities?实现这一点的最简单方法是什么?

这是我的代码:

MainActivity.java

package tud.fb13.iib.iibw_v2;


import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

import android.app.ActionBar;
import android.app.AlertDialog;
import android.app.FragmentTransaction;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        actionBar.setDisplayShowTitleEnabled(false);

        /** Creating first Tab */
        Tab tab = actionBar.newTab()
                .setText(R.string.tab_1)
                .setTabListener(new CustomTabListener<tab1_fragment>(this, "tag_tab1", tab1_fragment.class));
                //.setIcon(R.drawable.android);

        actionBar.addTab(tab);


        /** Creating second Tab */
        tab = actionBar.newTab()
                .setText(R.string.tab_2)
                .setTabListener(new CustomTabListener<tab2_fragment>(this, "tag_tab2", tab2_fragment.class));
                //.setIcon(R.drawable.apple);


        actionBar.addTab(tab);            

        /** Creating third Tab */
        tab = actionBar.newTab()
                .setText(R.string.tab_3)
                .setTabListener(new CustomTabListener<tab3_fragment>(this, "tag_tab3", tab3_fragment.class));
                //.setIcon(R.drawable.apple);


        actionBar.addTab(tab); 

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
        case R.id.action_user:
            Toast.makeText(this, "Menue eintrag ausgeführt!", Toast.LENGTH_SHORT).show();

            /*
            login tFragment = new login();
            android.app.FragmentManager fragmentManager = getFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            tFragment.show(fragmentTransaction, "time_dialog");
            */
            return true;

        case R.id.action_settings:
            Toast.makeText(this, "Einstellungen werden aufgerufen.", Toast.LENGTH_SHORT).show();
            return true;

        case R.id.action_end:
            //Toast.makeText(this, "App wird Beendet!", Toast.LENGTH_SHORT).show();


            // Hässlich aber es funktioniert !      

            new AlertDialog.Builder(this, AlertDialog.THEME_HOLO_DARK)

            .setTitle("WARNUNG")
                .setIcon(android.R.drawable.ic_dialog_alert)
               .setMessage("Wollen Sie die App wirklich beenden? Sämtliche nicht gespeicherten Einträge gehen verloren!")
               .setCancelable(false)
               .setPositiveButton("Ja", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                        //CustomTabActivity.this.finish();
                       android.os.Process.killProcess(android.os.Process.myPid());
                   }
               })
               .setNegativeButton("Nein", null)
               .show();

            return true;

        default:
            return super.onOptionsItemSelected(item);
        }
    }

}

tab3_fragment.java

package tud.fb13.iib.iibw_v2;

import android.app.ListFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

/** This is a listfragment class */
public class tab3_fragment extends ListFragment {

    /** An array of items to display in ArrayList */
    String string_tab3_temp[] = new String[]{
        "Rauchmelder 1",
        "Rauchmelder 2",
        "Rauchmelder 3",
        "Rauchmelder 4",
        "Rauchmelder 5"
    };


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        /** Creating array adapter to set data in listview */
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity().getBaseContext(), android.R.layout.simple_list_item_single_choice, string_tab3_temp);

        /** Setting the array adapter to the listview */
        setListAdapter(adapter);

        return super.onCreateView(inflater, container, savedInstanceState);
    }



    @Override
    public void onStart() {
        super.onStart();

        /** Setting the multiselect choice mode for the listview */
        getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);       
    }



}

CustomTabListener.java

package tud.fb13.iib.iibw_v2;

import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;

public class CustomTabListener<T extends Fragment> implements TabListener {
    private Fragment mFragment;
    private final Activity mActivity;
    private final String mTag;
    private final Class<T> mClass;

    public CustomTabListener(Activity activity, String tag, Class<T> clz){
        mActivity = activity;
        mTag = tag;
        mClass = clz;

    }


    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
        // Nothing special to do here for this application
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        if(mFragment==null){
            mFragment = Fragment.instantiate(mActivity, mClass.getName());


            ft.add(android.R.id.content,mFragment, mTag);

        }else{
            ft.attach(mFragment);
        }

    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        if(mFragment!=null)
            ft.detach(mFragment);       
    }

}

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

0 回答 0