ViewPager加载不同的片段

时间:2018-05-08 18:20:45

标签: android android-viewpager

我创建了我的PagerAdapter:

public class PagerAdapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>();

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

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

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

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

得到3个不同的片段:

public class chargerOperationsList extends Fragment {
    ListView listview;
    List<ChargingOperationGet> list2;
    ProgressBar progressBar;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View myView = inflater.inflate(R.layout.activity_charger_operations_list, null);

        listview = (ListView) myView.findViewById(R.id.listView);
        progressBar = (ProgressBar) myView.findViewById(R.id.progressBar2);
        progressBar.setVisibility(View.VISIBLE);

        asyncGet asyncget = new asyncGet(this);
        asyncget.execute();


        return myView;
    }

    public void setListOnMainClass(List<ChargingOperationGet> list2)
    {
        this.list2=list2;
        CustomListAdapter customListAdapter = new CustomListAdapter(getActivity(), R.layout.customlistlayout, list2);
        listview.setAdapter(customListAdapter);
    }


    public void setProgressBarInvisible()
    {
        progressBar.setVisibility(View.INVISIBLE);
    }
}

我的主类,有BottomNavigationView(id:navigation)以及ViewPager(id:viewpager):

public class MainBottom extends AppCompatActivity {

SecondFragment secondFragment;
Connect connectFragment;
chargerOperationsList chargerOperationsList;

ViewPager viewPager;
MenuItem prevMenuItem;
BottomNavigationView navigation;

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

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_home:
                   viewPager.setCurrentItem(0);
                   break;
                case R.id.navigation_dashboard:
                    viewPager.setCurrentItem(1);
                    break;
                case R.id.navigation_notifications:
                    if(!BluetoothAdapter.getDefaultAdapter().isEnabled()) {
                        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                        startActivityForResult(enableBtIntent , 0);
                    }

                    if(BluetoothAdapter.getDefaultAdapter().isEnabled()) {
                        viewPager.setCurrentItem(2);
                    }

                    break;
            }
            return false;
        }
    };


    private void setupViewPager(ViewPager viewPager)
    {
        PagerAdapter adapter = new PagerAdapter(getSupportFragmentManager());
        secondFragment = new SecondFragment();
        chargerOperationsList = new chargerOperationsList();
        connectFragment = new Connect();
        adapter.addFragment(secondFragment);
        adapter.addFragment(chargerOperationsList);
        adapter.addFragment(connectFragment);
        viewPager.setAdapter(adapter);
    }




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

       navigation = (BottomNavigationView) findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

        viewPager = (ViewPager) findViewById(R.id.viewpager);

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (prevMenuItem != null) {
                    prevMenuItem.setChecked(false);
                }
                else
                {
                    navigation.getMenu().getItem(0).setChecked(false);
                }
                Log.d("page", "onPageSelected: "+position);
                navigation.getMenu().getItem(position).setChecked(true);
                prevMenuItem = navigation.getMenu().getItem(position);

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        setupViewPager(viewPager);
     //   loadFragment(new SecondFragment());
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
           // viewPager.setCurrentItem(2);
        }
        if (resultCode == RESULT_CANCELED) {
            Toast.makeText(getApplicationContext(), "Cannot be done", Toast.LENGTH_SHORT).show();
        }
    }
}

当我尝试选择这3个片段中的一个时,它们显示不正确。看起来他们正在互相混合。也许是“viewPager.setCurrentItem(0);”工作不正常?

0 个答案:

没有答案