Android ListFragment - onListItemClick启动另一个片段

时间:2015-03-12 01:41:16

标签: android android-fragments

虽然这似乎很容易出问题,但我已经被困了好几个小时。如果有人能帮助我提供一些代码,那就太好了。这就是我的问题。

有一个MainActivity,它有一个带有两个按钮的ActionBar。每个按钮在单击时启动ListFragment。然后每个ListFragment的每个项目都会启动另一个片段(这是我无法实现的)。以下是MainActivity的代码:

public class MainActivity extends Activity implements
    DataListFragment.DataListSelectionListener {

    private final String TAG = "MainActivity";

    // Reference the buttons in the ActionBar
    private Button actionBarData;
    private Button actionBarElu;
    private Button actionBarOptions;

    // Reference the ListFragments
    DataListFragment dataListFragment;
    EluListFragment eluListFragment;

    // Reference the fragments related to the listItems
    SectionTypeFragment sectionTypeFragment;
    SectionDimensionsFragment sectionDimensionsFragment;
    MaterialsFragment materialsFragment;
    ReinforcementFragment reinforcementFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Inflate the custom layout for the ActionBar
        final ViewGroup actionBarLayout = (ViewGroup) getLayoutInflater().inflate(
                R.layout.action_bar,
                null);
        // Set the custom ActionBar
        final ActionBar actionBar = getActionBar();
        actionBar.setDisplayShowHomeEnabled(true);
        actionBar.setDisplayShowTitleEnabled(true);
        actionBar.setDisplayShowCustomEnabled(true);
        actionBar.setCustomView(actionBarLayout);
        // ActionBar Customization with buttons
        final int actionBarColor = getResources().getColor(R.color.blue_2);
        actionBar.setBackgroundDrawable(new ColorDrawable(actionBarColor));

        actionBarData = (Button) findViewById(R.id.action_bar_data);
        actionBarData.setOnClickListener(new Button.OnClickListener(){
            @Override
            public void onClick(View v) {

                Log.i(TAG, "DATA button clicked.");
                clickActionBarButton(v);

            }
        });
        actionBarElu = (Button) findViewById(R.id.action_bar_elu);
        actionBarElu.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View v) {

                Log.i(TAG, "ELU button clicked.");
                clickActionBarButton(v);

            }
        });


    }


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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_bar_data) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


    public void clickActionBarButton(View v){

        // Get a reference to the FragmentManager
        FragmentManager fragmentManager = getFragmentManager();
        // Begin a new FragmentTransaction
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        // Create the fragment depending on the button pressed
        if(v == actionBarData){

            // If there is no FeedFragment instance, then create one
            if(sectionTypeFragment == null)
                dataListFragment = new DataListFragment();

            // Replace the main_frame_layout with the sectionTypeFragment
            fragmentTransaction.replace(R.id.main_frame_layoout, dataListFragment);

        }else if(v == actionBarElu){

            // If there is no FeedFragment instance, then create one
            if(sectionTypeFragment == null)
                eluListFragment = new EluListFragment();

            // Replace the main_frame_layout with the sectionTypeFragment
            fragmentTransaction.replace(R.id.main_frame_layout, eluListFragment);

        }

        // Commit the FragmentTransaction
        fragmentTransaction.commit();
    }

    // Called when the user selects an item in the DataListFragment
    public void onDataListSelection(int index){

        // Get a reference to the FragmentManager
        FragmentManager fragmentManager = getFragmentManager();
        // Begin a new FragmentTransaction
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        // Add the fragment related to the selected item in the List
        switch (index){
            case 0:

                // If there is no SectionTypeFragment instance, then create one
                if(sectionTypeFragment == null)
                    sectionTypeFragment = new SectionTypeFragment();

                // Replace the main_frame_layout with the sectionTypeFragment
                fragmentTransaction.replace(R.id.main_frame_layout, sectionTypeFragment);
                // Add this FragmentTransaction to the backstack
                fragmentTransaction.addToBackStack(null);
                break;

            case 1:

            ...

        }

        // Commit the FragmentTransaction
        fragmentTransaction.commit();

    }

}

以下是其中一个ListFragments(DataListFragment)的代码:

public class DataListFragment extends ListFragment {

    private static final String TAG = "DataFragment";

    // List items
    private String[] listItems;

    private DataListSelectionListener mListener = null;

    // Callback interface that allows this Fragment to notify the MainActivity when
    // user clicks on a DataList Item
    public interface DataListSelectionListener {
        public void onDataListSelection(int index);
    }

    // Called when the user selects an item from the DataList
    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {

        // Indicates the selected item has been checked
        getListView().setItemChecked(position, true);

        // Inform the MainActivity that an item has been selected
        mListener.onDataListSelection(position);

    }

    public void onActivityCreated(Bundle savedInstanceState){
        super.onActivityCreated(savedInstanceState);
        // Get the the items to show in the List from the strings.xml
        listItems = getResources().getStringArray(R.array.data_list_array);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, listItems);
        setListAdapter(adapter);

    }

}

以下是例外:

03-12 14:39:38.681  29515-29515/com.tari.rcsec E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
        at android.view.ViewGroup.addViewInner(ViewGroup.java:3459)
        at android.view.ViewGroup.addView(ViewGroup.java:3330)
        at android.view.ViewGroup.addView(ViewGroup.java:3275)
        at android.view.ViewGroup.addView(ViewGroup.java:3251)
        at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:840)
        at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
        at android.app.BackStackRecord.run(BackStackRecord.java:635)
        at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1397)
        at android.app.FragmentManagerImpl$1.run(FragmentManager.java:426)
        at android.os.Handler.handleCallback(Handler.java:615)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:153)
        at android.app.ActivityThread.main(ActivityThread.java:5022)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1032)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:790)
        at dalvik.system.NativeStart.main(Native Method)

0 个答案:

没有答案