0
我已創建ViewPager,其中一個選項卡具有QR碼閱讀器,一個選項卡顯示掃描的QR碼的結果。默認情況下,qr代碼屏幕的結果將彈出,所以當我在掃描qr代碼選項卡上的標籤我只看到該片段的佈局,但掃描儀屏幕不顯示。 當我將默認選項卡設置爲掃描儀時,只有它顯示掃描儀屏幕。下面 是我的視圖尋呼機代碼..當點擊標籤頁時,查看分頁器片段不會被調用?
tabLayout = (TabLayout) findViewById(R.id.bottomNavigation);
viewPager =(ViewPager)findViewById(R.id.viewPagerHome);
tabLayout.setupWithViewPager(viewPager);
viewPager.setOffscreenPageLimit(2);
view1= (View)findViewById(R.id.view1);
view2= (View)findViewById(R.id.view2);
view3= (View)findViewById(R.id.view3);
//Adding the tabs using addTab() method
tabLayout.addTab(tabLayout.newTab().setText("Scan"));
tabLayout.addTab(tabLayout.newTab().setText("Candidate"));
tabLayout.addTab(tabLayout.newTab().setText("Setting"));
viewPager = (ViewPager) findViewById(R.id.viewPagerHome);
//Creating our pager adapter
adapter = new Pager(getSupportFragmentManager(), tabLayout.getTabCount());
//Adding adapter to pager
viewPager.setAdapter(adapter);
//Adding onTabSelectedListener to swipe views
tabLayout.setOnTabSelectedListener(this);
//setUptabIcon();
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
switch(position){
case 0:
/* ViewPager viewPager = (ViewPager)findViewById(R.id.viewPagerHome);
viewPager.setCurrentItem(0);*/
view1.setBackgroundResource(R.color.red );
view2.setBackgroundResource(R.color.white );
view3.setBackgroundResource(R.color.white);
break;
case 1:
view1.setBackgroundResource(R.color.white);
view2.setBackgroundResource(R.color.red);
view3.setBackgroundResource(R.color.white);
break;
case 2:
view1.setBackgroundResource(R.color.white );
view2.setBackgroundResource(R.color.white);
view3.setBackgroundResource(R.color.red );
break;
default:
view1.setBackgroundResource(R.color.white );
view2.setBackgroundResource(R.color.red);
view3.setBackgroundResource(R.color.white );
break;
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
if(tab.getPosition() == 0){
Fragment childF = new CandidateScanChildFragment();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.child_scanfragment_container, childF).commit();
}
}
ViewPagerAdpater.class
public class Pager extends FragmentStatePagerAdapter {
int tabCount;
FragmentManager mFragmentManager;
private Fragment mFragmentAtPos0;
//FirstPageListener listener = new FirstPageListener();
public Pager(FragmentManager fm, int tabCount) {
super(fm);
this.tabCount = tabCount;
}
public Pager(FragmentManager fragmentManager) {
super(fragmentManager);
mFragmentManager = fragmentManager;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
ScanFragment scanTabFragment = new ScanFragment();
return scanTabFragment;
case 1:
CandidateFragment candidateTabFragment = new CandidateFragment();
return candidateTabFragment;
case 2:
SettingFragment settingtabFragment = new SettingFragment();
return settingtabFragment;
default:
return null;
}
}
@Override
public int getCount() {
return tabCount;
}
@覆蓋 公衆詮釋getItemPosition(Object對象) {
return POSITION_NONE;
}
}