查看页与片段有listview更新多次

  • 本文关键字:listview 更新 片段 android
  • 更新时间 :
  • 英文 :


我试图在视图页内实现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;
        }
    }

试试这个:

mViewPager.setOffscreenPageLimit(3);

最新更新