选定的标签不会保留在Adndroid的标签布局中

时间:2017-11-19 15:44:51

标签: android navigation-drawer android-tablayout

当我点击导航抽屉项目时,第一个选项卡被选中。 从抽屉中选择第​​二个项目后,第二个选项卡刚刚被选中几秒钟,它将在第一个选项卡上移动。和其他人一样

the first tab is selected from navigation drawer, it works fine

now as per selected second menu from drawer it automatically move first tab after few seconds

This code block run for view pager 

  if (Share.getSetCheckVariable().equals("Products")) {
            boolean b = true;
            Share.setSetCheckVariable("Products");
            for (int i = 0; i < productSize; i++) {
                fragment = new ImageListFragmentImageListFragment();
                bundle = new Bundle();
                bundle.putInt("type", i);
                fragment.setArguments(bundle);
                if (b) {
                    for (int g = 0; g < subCategories_Size; g++) {
                        b = false;
                        adapter.addFragment(fragment, sub_categories.get(g));
                    }
                }
            }
        }


This code block run when click on navigation drawer item    

    for (int i = 0; i < subCategories_Size; i++) {
                    if (id == i) {
                        Share.setSetCheckVariable("Products");
                        get_productDetails(sub_categoriesID.get(i));
                        b = true;
                        myHandler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                productSize = Share.getProduct_name().size();
                                setupViewPager(viewPager);
                            }
                        }, 1000);
                        break;
                    }
                }
                viewPager.setCurrentItem(id);
            }

1 个答案:

答案 0 :(得分:0)

您没有更改id的值或i的值,因此当您运行时:

for (int i = 0; i < subCategories_Size; i++) {
                    if (id == i) {
                        Share.setSetCheckVariable("Products");
                        get_productDetails(sub_categoriesID.get(i));
                        b = true;
                        myHandler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                productSize = Share.getProduct_name().size();
                                setupViewPager(viewPager);
                            }
                        }, 1000);
                        break;
                    }
                }
                viewPager.setCurrentItem(id);
            }

它会显示第一个,因为viewPager.setCurrentItem(id);id = 1。

此外,您应该使用onClickListener代替