Android中的标签页消失了。代码以前可以工作,但是现在停止工作

时间:2018-12-21 00:37:09

标签: android android-layout android-fragments android-intent

我在Android中一起使用了Tab键控件和RecyclerView。它的运行方式是我在SearchBar中有toolbar。当用户在搜索栏中输入搜索查询时。按下搜索按钮后,tab's片段应该被加载数据。问题如下。

1)标签图标消失

2)在通过找到的代码进行调试时(由于我使用的是带有Tab布局的ViewPager来实现android中的Tabs),因此每次触发getItem时,都会两次调用PagerAdapter类的getItem方法。

我被困住了,不知道该怎么办。网上也没有太多帮助。

1)制表符初始化代码:-

 private TabLayout tabLayout;

 @Override
 protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        tabLayout = (TabLayout)findViewById(R.id.tablayout);

 tabLayout.addTab(tabLayout.newTab().setIcon(R.mipmap.craigslist_new1));
    tabLayout.addTab(tabLayout.newTab().setIcon(R.mipmap.kijiji_new1));
    tabLayout.addTab(tabLayout.newTab().setIcon(R.mipmap.letgo_new1));        
 tabLayout.addTab(tabLayout.newTab().setIcon(R.mipmap.varagesale_new1));        
 tabLayout.addTab(tabLayout.newTab().setIcon(R.mipmap.facebook_new1));
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
    drawerLayout = findViewById(R.id.drawer_layout);

}


 @Override
 public boolean onQueryTextSubmit(String query) {

    //loadRecyclerViewData(query);

    Bundle bundle = new Bundle();
    bundle.putString("searchText", query);

        final ViewPager viewPager = (ViewPager)findViewById(R.id.pager);
        final PageAdapter adapter = new PageAdapter(getSupportFragmentManager(), tabLayout.getTabCount(), bundle);
        viewPager.setAdapter(adapter);

    viewPager.setOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.setupWithViewPager(viewPager);
    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            viewPager.setCurrentItem(tab.getPosition());

        }

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

        }

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

        }
    });

    return true;
}

2)PagerAdapter:-

 public class PageAdapter extends FragmentStatePagerAdapter {

      int mNoofTabs;
      Bundle bundle;

      public PageAdapter(FragmentManager fragmentManager, int NumberOfTabs, Bundle bundle)
        {
        super(fragmentManager);
        this.mNoofTabs = NumberOfTabs;
        this.bundle = bundle;

    }
     @Override
     public Fragment getItem(int i) {
        switch(i)
         {
            case 0 :
                CraigslistFragment tab1 = new CraigslistFragment();
                tab1.setArguments(bundle);
                return tab1;
            case 1:
                KijijiFragment tab2 = new KijijiFragment();
                tab2.setArguments(bundle);
                return tab2;
            case 2:
                letgoFragment tab3 = new letgoFragment();
                tab3.setArguments(bundle);
                return tab3;
            case 3:
                VaragesaleFragment tab4 = new VaragesaleFragment();
                tab4.setArguments(bundle);
                return tab4;
            case 4:
                FacebookFragment tab5 = new FacebookFragment();
                tab5.setArguments(bundle);
                return tab5;

            default:
                return null;
        }

     }

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

0 个答案:

没有答案