Android获取Fixed Tab,PageViewer加载不同的片段

时间:2013-08-21 02:33:48

标签: java android android-fragments android-viewpager android-tabs

我是软件开发和Java的新手,我已经尝试了几天,以获得用于使用pageviewer的固定标签导航的默认模板。无论我尝试什么,我似乎无法加载任何东西,除了第一个片段。所有其他onClick标签事件在模拟器中加载模板中定义的相同的第一个片段,滑动寻呼机具有相同的效果。

我已尝试多次修改我的代码,直到运行时错误很少,但我仍然无法通过选项卡加载其他代码。

我很感激任何帮助。

这是我的代码。

我的项目是原生平板电脑EHR应用程序

    package com.example.medictouch;

    import java.util.Locale;

    import com.example.medictouch.MedicTouchActivity.encountersFragment;
    import com.example.medictouch.MedicTouchActivity.patient_chartFragment;
    import com.example.medictouch.MedicTouchActivity.billingFragment;
    import com.example.medictouch.MedicTouchActivity.medicationsFragment;
    import com.example.medictouch.MedicTouchActivity.treatmentsFragment;
    import com.example.medictouch.MedicTouchActivity.laboratoryFragment;
    import com.example.medictouch.MedicTouchActivity.imagingFragment;
    import com.example.medictouch.MedicTouchActivity.doctors_notesFragment;
    import com.example.medictouch.MedicTouchActivity.departmentsFragment;


    import android.app.ActionBar;
    import android.app.FragmentTransaction;
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentActivity;
    import android.support.v4.app.FragmentManager;
    import android.support.v4.app.FragmentPagerAdapter;
    import android.support.v4.app.NavUtils;
    import android.support.v4.view.ViewPager;
    import android.view.Gravity;
    import android.view.LayoutInflater;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.TextView;

    public class MedicTouchActivity extends FragmentActivity implements
            ActionBar.TabListener {

        /**
         * The {@link android.support.v4.view.PagerAdapter} that will provide
         * fragments for each of the sections. We use a
         * {@link android.support.v4.app.FragmentPagerAdapter} derivative, which
         * will keep every loaded fragment in memory. If this becomes too memory
         * intensive, it may be best to switch to a
         * {@link android.support.v4.app.FragmentStatePagerAdapter}.
         */
        SectionsPagerAdapter mSectionsPagerAdapter;

        /**
         * The {@link ViewPager} that will host the section contents.
         */
        ViewPager mViewPager;

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

            // Set up the action bar.
            final ActionBar actionBar = getActionBar();
            actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
            actionBar.setDisplayShowHomeEnabled(false);
            actionBar.setDisplayShowTitleEnabled(false);

            // Create the adapter that will return a fragment for each of the three
            // primary sections of the app.
            mSectionsPagerAdapter = new SectionsPagerAdapter(
                    getSupportFragmentManager());

            // Set up the ViewPager with the sections adapter.
            mViewPager = (ViewPager) findViewById(R.id.pager);
            mViewPager.setAdapter(mSectionsPagerAdapter);

            // When swiping between different sections, select the corresponding
            // tab. We can also use ActionBar.Tab#select() to do this if we have
            // a reference to the Tab.
            mViewPager
                    .setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
                        @Override
                        public void onPageSelected(int position) {
                            actionBar.setSelectedNavigationItem(position);
                        }
                    });

            // For each of the sections in the app, add a tab to the action bar.
            for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
                // Create a tab with text corresponding to the page title defined by
                // the adapter. Also specify this Activity object, which implements
                // the TabListener interface, as the callback (listener) for when
                // this tab is selected.
                actionBar.addTab(actionBar.newTab()
                        .setText(mSectionsPagerAdapter.getPageTitle(i))
                        .setTabListener(this));
            }
        }


        @Override
        public void onTabSelected(ActionBar.Tab tab,
                FragmentTransaction fragmentTransaction) {
            // When the given tab is selected, switch to the corresponding page in
            // the ViewPager.
            mViewPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(ActionBar.Tab tab,
                FragmentTransaction fragmentTransaction) {
        }

        @Override
        public void onTabReselected(ActionBar.Tab tab,
                FragmentTransaction fragmentTransaction) {
        }

        /**
         * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
         * one of the sections/tabs/pages.
         */
        public class SectionsPagerAdapter extends FragmentPagerAdapter {

            public SectionsPagerAdapter(FragmentManager fm) {
                super(fm);
            }

            @Override
            public Fragment getItem(int position) {
                // getItem is called to instantiate the fragment for the given page.
                // Return a DummySectionFragment (defined as a static inner class
                // below) with the page number as its lone argument.
                Fragment fragment = new encountersFragment();
                Fragment fragment1 = new patient_chartFragment();
                Fragment fragment2 = new billingFragment();
                Fragment fragment3 = new medicationsFragment();
                Fragment fragment4 = new treatmentsFragment();
                Fragment fragment5 = new laboratoryFragment();
                Fragment fragment6 = new imagingFragment();
                Fragment fragment7 = new doctors_notesFragment();
                Fragment fragment8 = new departmentsFragment();

                return fragment;
            }





            @Override
            public int getCount() {
                // Show 6 total pages.
                return 10;
            }

            @Override
            public CharSequence getPageTitle(int position) {
                Locale l = Locale.getDefault();
                switch (position) {
                case 0:
                    return getString(R.string.title_section1).toLowerCase();
                case 1:
                    return getString(R.string.title_section2).toLowerCase();
                case 2:
                    return getString(R.string.title_section3).toLowerCase();
                case 3:
                    return getString(R.string.title_section4).toLowerCase();
                case 4:
                    return getString(R.string.title_section5).toLowerCase();
                case 5:
                    return getString(R.string.title_section6).toLowerCase();
                case 6:
                    return getString(R.string.title_section7).toLowerCase();
                case 7:
                    return getString(R.string.title_section8).toLowerCase();
                case 8:
                    return getString(R.string.title_section9).toLowerCase();
                }
                return null;
            }
        }

        /**
         * A dummy fragment representing a section of the app, but that simply
         * displays dummy text.
         */
        public class encountersFragment extends Fragment {

            private final int title_section1 = 0;
            public final int ARG_SECTION_NUMBER = title_section1;


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

                return rootView;
            }
        }


    public class patient_chartFragment extends Fragment {

        private final int title_section2 = 1;
        public final int ARG_SECTION_NUMBER = title_section2;

        public patient_chartFragment() {
        }

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

            return rootView;
        }
    }


    public class billingFragment extends Fragment {

        private final int title_section3 = 2;
        public final int ARG_SECTION_NUMBER = title_section3;

        public billingFragment() {
        }

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

            return rootView;
        }
    }


    public class medicationsFragment extends Fragment {

        public final int ARG_SECTION_NUMBER = 3;

        public medicationsFragment() {
        }

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

            return rootView;
        }
    }


    public class treatmentsFragment extends Fragment {

        public final int ARG_SECTION_NUMBER = 4;

        public treatmentsFragment() {
        }

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

            return rootView;
        }
    }


    public class laboratoryFragment extends Fragment {

        public final int ARG_SECTION_NUMBER = 5;

        public laboratoryFragment() {
        }

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

            return rootView;
        }
    }


    public class imagingFragment extends Fragment {

        public final int ARG_SECTION_NUMBER = 6;

        public imagingFragment() {
        }

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

            return rootView;
        }
    }


    public class doctors_notesFragment extends Fragment {

        public final int ARG_SECTION_NUMBER = 7;

        public doctors_notesFragment() {
        }

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

            return rootView;
        }
    }


    public class departmentsFragment extends Fragment {

        public final int ARG_SECTION_NUMBER = 8;

        public departmentsFragment() {
        }

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

            return rootView;
        }
    }


    }

1 个答案:

答案 0 :(得分:2)

您需要在getItem(int position)方法中使用switch..case ..块。使用以下代码。

@Override
public Fragment getItem(int position) {
    // getItem is called to instantiate the fragment for the given page.
    // Return a DummySectionFragment (defined as a static inner class
    // below) with the page number as its lone argument.
    Fragment fragment;
    switch (position) {

    case 0:
        fragment = new encountersFragment();
        break;
    case 1:
        fragment = new patient_chartFragment();
        break;
    case 2:
        fragment = new billingFragment();
        break;
    case 3:
        fragment = new medicationsFragment();
        break;
    case 4:
        fragment = new treatmentsFragment();
        break;
    case 5:
        fragment = new laboratoryFragment();
        break;
    case 6:
        fragment = new imagingFragment();
        break;
    case 7:
        fragment = new doctors_notesFragment();
        break;
    case 8:
        fragment = new departmentsFragment();
        break;
    default:
        break;
    }

    return fragment;
}

修改

我在你的代码中发现了这个错误(可能有更多错误)。您在xml中使用AutoCompletTextView将其更改为 AutoCompleteTextView

08-16 21:53:46.247: E/AndroidRuntime(4768): Caused by:
java.lang.ClassNotFoundException: Didn't find class "android.view.AutoCompletTextView" on 
path: DexPathList[[zip file "/data/app/com.example.medictouch-
2.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.medictouch-2, /system/lib]]