查看具有listview更新多次的片段的寻呼机

时间:2013-06-14 17:13:52

标签: android

我正在尝试在View寻呼机中实现3个片段。所有片段都有自己的列表视图。每当我刷回第一个片段时,列表视图就会自动更新,从而获得重复值。这是实现视图寻呼机的主要活动。请建议。

public class MainActivity extends FragmentActivity {
    /** Called when the activity is first created. */
    static AppSectionsPagerAdapter mAppSectionsPagerAdapter;

    ViewPager mViewPager;
    SearchView mSearchView;
    MenuItem menuSearch;
    Boolean flag = false;
    String pid;

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

        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setupActionBar();
        getActionBar().setTitle("All Patients");

        mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager());


        // Set up the ViewPager, attaching the adapter and setting up a listener for when the
        // user swipes between sections.
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mAppSectionsPagerAdapter);
        mViewPager.setOnPageChangeListener(new OnPageChangeListener() 
        {
            public void onPageSelected(int position) 
            {
                //mAppSectionsPagerAdapter.getItemPosition(getFragmentManager().findFragmentById(position));
                //getActionBar().setTitle(mAppSectionsPagerAdapter.getPageTitle(position));

                if(position==0){
                    getActionBar().setTitle("All Patients");
                    //flag = true;
                    //menuSearch.setVisible(true);
                    invalidateOptionsMenu();

                }
                if(position==1){
                    getActionBar().setTitle("All Doctors");
                    //flag=false;
                    //menuSearch.setVisible(false);
                    invalidateOptionsMenu();
                }
                else
                    getActionBar().setTitle("All Nurses");
                invalidateOptionsMenu();

            }

            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) 
            {

            }

            public void onPageScrollStateChanged(int state) 
            {
                if (state == ViewPager.SCROLL_STATE_DRAGGING)
                {

                }
                if (state == ViewPager.SCROLL_STATE_IDLE)
                {

                }
            }
        });
    }
    /**
     * A {@link FragmentPagerAdapter} that returns a fragment corresponding to one of the primary
     * sections of the app.
     */
    public static class AppSectionsPagerAdapter extends FragmentPagerAdapter {

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

        @Override
        public Fragment getItem(int i) {
            switch (i) {
            case 0:
                // The first section of the app is the most interesting -- it offers
                // a launchpad into the other demonstrations in this example application.

                return new AllPatient();


            case 1:

                //Intent intent = new Intent(MainActivity.this,abc.class);
                return new AllDoctors();



            case 2:
                return new AllNurses();
            }
            return null;
        }

        @Override
        public int getCount() {
            return 3;
        }
    }

1 个答案:

答案 0 :(得分:1)

试试这个:

mViewPager.setOffscreenPageLimit(3);