如何让片段仅在我进入片段时才能开始?

时间:2018-12-29 16:19:50

标签: java android android-viewpager fragment bottomnavigationview

我使用bottomnavigationview + viewpager,当我在第一页上并且转到第二页时,它已经与应用程序启动时的第一页一起负责了

public class MainActivity extends AppCompatActivity{

    private ViewPager viewPager;
    private MenuItem prevMenuItem;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        viewPager = findViewById(R.id.viewpager);
        final BottomNavigationView navigation =  findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
        //aqui se da el metodo addOnPageChangeListener del viewpager
        onPageListener(viewPager,navigation);
        /*ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(viewPagerAdapter);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                navigation.getMenu().getItem(i).setChecked(true);
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });

        if(savedInstanceState == null){
            onNavigationItemSelected(navigation.getMenu().findItem(R.id.navigation_inicio));
        }
        */


        //aqui se agrega los fragments al arraylist de ViewPagerAdapter
        setearViewPager(viewPager);
    }

    /*@Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
        switch (menuItem.getItemId()) {
            case R.id.navigation_inicio:
                viewPager.setCurrentItem(0);
                return true;
            case R.id.navigation_reporte:
                viewPager.setCurrentItem(1);
                return true;
            case R.id.navigation_produccion:
                viewPager.setCurrentItem(2);
                return true;
            case R.id.navigation_configuracion:
                viewPager.setCurrentItem(3);
                return true;
            default:
                return false;
        }
    }
    */


    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
            switch (item.getItemId()) {
                case R.id.navigation_inicio:
                    viewPager.setCurrentItem(0);
                    return true;
                case R.id.navigation_reporte:
                    viewPager.setCurrentItem(1);
                    return true;
                case R.id.navigation_produccion:
                    viewPager.setCurrentItem(2);
                    return true;
                case R.id.navigation_configuracion:
                    viewPager.setCurrentItem(3);
                    return true;
            }
            return false;
        }
    };

    private void onPageListener(ViewPager viewPager, final BottomNavigationView bottomNavigationView){
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                if(prevMenuItem != null)
                    prevMenuItem.setChecked(false);
                else
                    bottomNavigationView.getMenu().getItem(0).setChecked(false);

                Log.d("pagina", "onPageSelected: " + i);
                bottomNavigationView.getMenu().getItem(i).setChecked(true);
                prevMenuItem = bottomNavigationView.getMenu().getItem(i);
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
    }

     private void setearViewPager(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        adapter.addFragment(HomeFragment.newInstance());
        adapter.addFragment(ReportFragment.newInstance());
        adapter.addFragment(ProductionFragment.newInstance());
        adapter.addFragment(ConfigurationFragment.newInstance());
        viewPager.setAdapter(adapter);
        //adapter.notifyDataSetChanged();
        //viewPager.setOffscreenPageLimit(0);
    }`

    `public class ViewPagerAdapter extends FragmentPagerAdapter {

    List<Fragment> fragmentList = new ArrayList<>();

    /*public ViewPagerAdapter(FragmentManager fm){
        super(fm);
    }

    @Override
    public Fragment getItem(int i) {
        switch (i){
            case 0:
                return HomeFragment.newInstance();
            case 1:
                return ReportFragment.newInstance();
            case 2:
                return ProductionFragment.newInstance();
            case 3:
                return ConfigurationFragment.newInstance();
            default:
                return null;
        }
    }

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

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        super.destroyItem(container, position, object);
    }
    */

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

    @Override
    public Fragment getItem(int i) {
        return fragmentList.get(i);
    }

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

    public void addFragment(Fragment fragment){
        fragmentList.add(fragment);
    }
}

当我从Inicio转到Reporte时,Report中的数据不会更新,但是我必须转到Configuracion并返回到Report,并且它最近已更新,as shown in this screenshot.

1 个答案:

答案 0 :(得分:0)

这是不可能的。因为ViewPageroffsetScreenPageLimit by default is 1

Look here,您的问题有一个答案。