我创建了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;
}
@覆盖 public int getItemPosition(Object object) {
return POSITION_NONE;
}
}
答案 0 :(得分:0)
检查以下代码
mPager = (ViewPager) findViewById(R.id.mPager);
tabLayout = (TabLayout) findViewById(R.id.tabLayout);
setupViewPager(mPager);
tabLayout.setupWithViewPager(mPager);
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new RequestFragment(), getString(R.string.requests));
// adapter.addFragment(new AddedMeFragment(), getString(R.string.added_me));
adapter.addFragment(new FriendsFragment(), getString(R.string.friends));
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}