3

我正在尝试在操作栏中设置选项卡式导航。当您点击一个选项卡时,frameLayout 应更改为该活动中 4 个可能的片段之一。

但是,它卡在第一个片段中,当我点击选项卡时,什么也没有发生。关于什么可能是错的任何想法?我怀疑我在处理选项卡的侦听器方面做错了,但我无法弄清楚(非常令人沮丧)。有什么帮助吗?提前致谢。

Form.java(主应用程序)

package com.example.ehistory;

import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
import android.view.MenuItem;

@SuppressLint("NewApi")
public class Form extends FragmentActivity {

    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_form1);

        ActionBar actionBar = getActionBar();

        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        actionBar.setDisplayShowHomeEnabled(true);
        actionBar.setDisplayShowTitleEnabled(false);

        Tab tab = actionBar.newTab()
                //.setText(R.string.anamnesis)
                .setIcon(R.drawable.id)
                .setTabListener(new TabListener<Frag1>(
                        this, "anamnesis", Frag1.class));
        actionBar.addTab(tab);

        tab = actionBar.newTab()
            //.setText(R.string.symptom)
            .setIcon(R.drawable.current)
            .setTabListener(new TabListener<Frag2>(
                    this, "symptom", Frag2.class));
        actionBar.addTab(tab);

        tab = actionBar.newTab()
                //.setText(R.string.otherhistory)
                .setIcon(R.drawable.past)
                .setTabListener(new TabListener<Frag3>(
                        this, "otherhistory", Frag3.class));
            actionBar.addTab(tab);

        tab = actionBar.newTab()
                 //.setText(R.string.ros)
                 .setIcon(R.drawable.ros)
                 .setTabListener(new TabListener<Frag4>(
                        this, "ros", Frag4.class));
            actionBar.addTab(tab);

            if (findViewById(R.id.fragment_container) != null) {

            // However, if we're being restored from a previous state,
            // then we don't need to do anything and should return or else
            // we could end up with overlapping fragments.
            if (savedInstanceState != null) {
                return;
            }

            // Create an instance of ExampleFragment
            Frag1 firstFragment = new Frag1();

            // In case this activity was started with special instructions from an Intent,
            // pass the Intent's extras to the fragment as arguments
            firstFragment.setArguments(getIntent().getExtras());

            // Add the fragment to the 'fragment_container' FrameLayout
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.fragment_container, firstFragment).commit();
        }

    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            //don't forget are you sure you don't want to save bullshit
            case R.id.newpatient:
                Intent intent = new Intent(Form.this, Form.class);
                Form.this.startActivity(intent);
            default: break;
        }
        return true;
    }

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

        /** Constructor used each time a new tab is created.
          * @param activity  The host Activity, used to instantiate the fragment
          * @param tag  The identifier tag for the fragment
          * @param clz  The fragment's Class, used to instantiate the fragment
          */
        public TabListener(Activity activity, String tag, Class<T> clz) {
            mActivity = activity;
            mTag = tag;
            mClass = clz;
        }

        /* The following are each of the ActionBar.TabListener callbacks */

        public void onTabSelected(Tab tab, FragmentTransaction ft) {
            // Check if the fragment is already initialized
            if (mFragment == null) {
                // If not, instantiate and add it to the activity
                mFragment = Fragment.instantiate(mActivity, mClass.getName());
                ft.replace(R.id.fragment_container, mFragment, mTag);
                ft.commit();
            } else {
                // If it exists, simply attach it in order to show it
                ft.attach(mFragment);
            }

        }

        public void onTabUnselected(Tab tab, FragmentTransaction ft) {
            if (mFragment != null) {
                // Detach the fragment, because another one is being attached
                ft.detach(mFragment);
            }
        }

        public void onTabReselected(Tab tab, FragmentTransaction ft) {
            // User selected the already selected tab. Usually do nothing.
        }

        @Override
        public void onTabReselected(Tab arg0,
                android.app.FragmentTransaction arg1) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onTabSelected(Tab arg0, android.app.FragmentTransaction arg1) {
            // TODO Auto-generated method stub


        }

        @Override
        public void onTabUnselected(Tab arg0,
                android.app.FragmentTransaction arg1) {
            // TODO Auto-generated method stub

        }
    }

}

Frag1.java(片段之一)

package com.example.ehistory;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Spinner;

public class Frag1 extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
            Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.activity_form, container, false);

        Spinner spinner = (Spinner) v.findViewById(R.id.gender_spin);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this.getActivity(),
                R.array.genders, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);

        AutoCompleteTextView textView = (AutoCompleteTextView) v.findViewById(R.id.ethnicity_edit);
        String[] ethnicities = getResources().getStringArray(R.array.ethnicity_array);
        ArrayAdapter<String> adapter_auto = 
                new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_list_item_1, ethnicities);
        textView.setAdapter(adapter_auto);

        AutoCompleteTextView textView1 = (AutoCompleteTextView) v.findViewById(R.id.nationality_edit);
        String[] nationalities = getResources().getStringArray(R.array.nationality_array);
        ArrayAdapter<String> adapter_auto1 = 
                new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_list_item_1, nationalities);
        textView1.setAdapter(adapter_auto1);

        Spinner spinner1 = (Spinner) v.findViewById(R.id.leg_spin);
        ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(this.getActivity(),
                R.array.legal_status, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner1.setAdapter(adapter1);

        AutoCompleteTextView textView2 = (AutoCompleteTextView) v.findViewById(R.id.religion_edit);
        String[] religions = getResources().getStringArray(R.array.religions);
        ArrayAdapter<String> adapter_auto2 = 
                new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_list_item_1, religions);
        textView2.setAdapter(adapter_auto2);

        return v;
    }

}
4

1 回答 1

1

为什么不使用ViewPager带有标签的导航?Android 开发者网站上有一个关于使用标签创建滑动视图的优秀教程。

于 2013-07-03T19:26:00.943 回答