如何在Andriod中停止运行片段onCreateView的标签更改

时间:2016-08-30 16:37:19

标签: java android tabs

我有三个选项卡使用下面的实现,它们表现非常好。更改选项卡时,正确的片段会加载,依此类推。问题是,当我到达最后一个选项卡并回到第一个片段时,它的oncreateview方法总是会再次触发运行其他代码而导致重复。任何帮助将不胜感激。

//选项卡上的活动基于

public class Dashboard extends AppCompatActivity {

private TabLayout tabLayout;
private ViewPager viewPager;
private MyViewPagerAdapter myViewPagerAdapter;
private int[] tabIcon = {R.drawable.ic_home, R.drawable.ic_message, R.drawable.ic_person};
android.support.v7.widget.Toolbar toolbar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.dashboard);
    //Toolbar
    toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    //ViewPager
    viewPager = (ViewPager) findViewById(R.id.viewpager);
    setupViewPager(viewPager);

    //Tablayout
    tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);
    tabLayout.getTabAt(0).setIcon(tabIcon[0]);
    tabLayout.getTabAt(1).setIcon(tabIcon[1]);
    tabLayout.getTabAt(2).setIcon(tabIcon[2]);

    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            switch(tab.getPosition()) {
                case 0:
                    viewPager.setCurrentItem(0);
                    toolbar.setTitle("Home");
                    break;
                case 1:
                    viewPager.setCurrentItem(1);
                    toolbar.setTitle("Messages");
                    break;
                case 2:
                    viewPager.setCurrentItem(2);
                    toolbar.setTitle("Profile");
                    break;
            }
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }


    });
}

private void setupViewPager(ViewPager viewPager){
    myViewPagerAdapter = new MyViewPagerAdapter(getSupportFragmentManager());
    myViewPagerAdapter.addFragments(new CategoryFragment(), "Categories");
    myViewPagerAdapter.addFragments(new MessagesFragment(), "Messages");
    myViewPagerAdapter.addFragments(new ProfileFragment(), "Profile");
    viewPager.setAdapter(myViewPagerAdapter);
}

//View Pager Adapter
public class MyViewPagerAdapter extends FragmentPagerAdapter {

    ArrayList<Fragment> fragments = new ArrayList<>();
    ArrayList<String> tabTitles = new ArrayList<>();

    public void addFragments(Fragment fragments, String titles){

        this.fragments.add(fragments);
        this.tabTitles.add(titles);
    }

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

    @Override
    public int getCount() {
        return fragments.size();
    }

    @Override
    public Fragment getItem(int position) {
        return fragments.get(position);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        //return tabTitles.get(position);
        return null;
    }
}



@Override
public void onBackPressed() {
    super.onBackPressed();
}

}

//主要的第一个片段代码 公共类CategoryFragment扩展了Fragment {

private DBHandler dbHandler;
private ListView listView;
private ListAdapter adapter;
ArrayList<Categories> categoriesList = new ArrayList<Categories>();

public CategoryFragment() {
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view =  inflater.inflate(R.layout.fragment_home, container, false);

    //Setting up the basic categories
    dbHandler = new DBHandler(view.getContext());

    //Get Categories from database
    final Cursor cursor = dbHandler.getCategories(0);
    if (cursor != null) {
        if(cursor.moveToFirst()){
            do{
                Categories categories = new Categories();
                categories.set_id(cursor.getInt(0));
                categories.set_categoryname(cursor.getString(2));
                categories.set_categoriescaption(cursor.getString(3));
                categoriesList.add(categories);

            }while (cursor.moveToNext());
        }
        cursor.close();
    }

    listView = (ListView) view.findViewById(R.id.categories);

    adapter = new CategoryAdapter(view.getContext(), R.layout.cursor_row, categoriesList);
    listView.setAdapter(adapter);

    listView.setOnItemClickListener(
            new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                    Integer cid = (int) (long) adapter.getItemId(position);

                    TextView categoryname = (TextView) view.findViewById(R.id.cursor);
                    String cname = categoryname.getText().toString();

                    Intent i = new Intent(view.getContext(), CategoryList.class);
                    i.putExtra("categoryname", cname);
                    i.putExtra("categoryid", cid);
                    startActivity(i);
                }
            }
    );

    return view;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

}

所以当我从最后一个标签向后滑动时。 OncreateView再次运行。我该如何处理并防止重复。谢谢

1 个答案:

答案 0 :(得分:2)

默认情况下,ViewPager在空闲状态下仅保留视图层次结构中的1页。因此,当您滑动到第三个选项卡时,viepager会破坏第一个选项卡以保留第二个选项卡。

要解决此问题,请添加此行

viewPager.setOffscreenPageLimit(3);

设置适配器后。 查看here文档以获取更多信息