android – 在选项卡中停止调用函数

前端之家收集整理的这篇文章主要介绍了android – 在选项卡中停止调用函数前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我的 android中有3个滑动标签(A,B,C).当来到选项卡c时,我有一个函数RetrieveData(),用于从 MySQL检索数据并加载到listView中.

假设在MysqL中我只有一行数据,当来到c时,数据将被加载到listView(仅一个列表).当我滑动到选项卡A并再次滑动到C时,列表现在变为2.
有没有办法让RetrieveData()只调用一次?谢谢.

public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
        View edit_details = inflater.inflate(R.layout.edit_work_details,container,false);
       // EditDetails = new ArrayList<HashMap<String,String>>();
        listViewUpdate = (ListView) edit_details.findViewById(R.id.listViewEdit);
        totalHours=(TextView)edit_details.findViewById(R.id.hour);
        setHasOptionsMenu(true);
        Bundle bundle = this.getArguments();
        if (getArguments() != null) {
            ID = bundle.getString("ID");
           RetrieveData(ID); // retrieve data from MysqL
        }
        Toast.makeText(getActivity(),"Details" + ID,Toast.LENGTH_LONG).show();
      }

TabAdapter

public class TabsFragmentPagerAdapter extends FragmentPagerAdapter {

    public TabsFragmentPagerAdapter(FragmentManager fm) {
        super(fm);
        // TODO Auto-generated constructor stub
    }

    @Override
    public Fragment getItem(int index) {
        // TODO Auto-generated method stub

        if(index == 0) {

            Fragment fragment=new A();
            Bundle bundle = new Bundle();
            bundle.putString("ID",Edit.ID);
            fragment.setArguments(bundle);
            return fragment;

        }
        if(index == 1) {
            Fragment fragment = new B();
            Bundle bundle = new Bundle();
            bundle.putString("ID",Edit.ID);
            fragment.setArguments(bundle);
            return fragment;
        }

        if(index == 2) {
            Fragment fragment = new C();
            Bundle bundle = new Bundle();
            bundle.putString("ID",Edit.ID);
            fragment.setArguments(bundle);
            return fragment;
        }

        return null;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return 3;
    }

}

ViewPager代码

public class ActivityB extends ActionBarActivity implements ActionBar.TabListener {

    private ViewPager viewPager;
    private ActionBar actionBar;
    private TabsFragmentPagerAdapter tabsAdapter;
    private String[] item = new String[]{"Information","Work Force","Work Details"};
    private String id;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_b);
//        id=getIntent().getExtras().getString("ID");
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        tabsAdapter = new TabsFragmentPagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(tabsAdapter);
        actionBar = getSupportActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        for(int i=0; i<4; i++){
            actionBar.addTab(actionBar.newTab().setText(item[i]).setTabListener(this));
        }
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int arg) {
                // TODO Auto-generated method stub
                actionBar.setSelectedNavigationItem(arg);
            }

            @Override
            public void onPageScrolled(int arg0,float arg1,int arg2) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
                // TODO Auto-generated method stub

            }
        });

    }



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

    }

    @Override
    public void onTabSelected(ActionBar.Tab tab,FragmentTransaction arg1) {
        // TODO Auto-generated method stub
        viewPager.setCurrentItem(tab.getPosition());
    }

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

    }

}

这是我的标签c

再次滑动到A和C之后

好像它再次调用RetrieveData.

解决方法

您应该使用ViewPager的setOffscreenPageLimit.它将首先设置您要加载的寻呼机页数.

setOffScreeenPageLimit的默认值为1,因此它将加载当前页面下一页.在你的情况下,它将加载A和B,当你从B移动到C时它会破坏A的实例.

尝试使用

`pager.setOffscreenPageLimit(2)`

它将加载当前页面和另外2页,因此A,B和C将被加载一次.

编辑

Set the number of pages that should be retained to either side of the
current page in the view hierarchy in an idle state. Pages beyond this
limit will be recreated from the adapter when needed.

This is offered as an optimization. If you know in advance the number
of pages you will need to support or have lazy-loading mechanisms in
place on your pages,tweaking this setting can have benefits in
perceived smoothness of paging animations and interaction. If you have
a small number of pages (3-4) that you can keep active all at once,
less time will be spent in layout for newly created view subtrees as
the user pages back and forth.

Source

它显然意味着您可以设置要在初始级别加载的页面数,如果要在首次运行时加载5个页面,请将其值设置为4.

现在,如果您将从5个页面中的任何页面移动,则不会调用Fragment的onCreateView(),因为它已在初始阶段加载.

猜你在找的Android相关文章