视图分页器addOnPageChangeListener不适用于片段分页器适配器

时间:2018-08-20 18:02:44

标签: android android-fragments android-viewpager

我在为viewPager实现“ addOnPageChangeListener”时遇到麻烦。我实质上是在实现THIS;其带有片段的滑块视图,我对子片段也是如此。

将viewListener添加到ViewPager时,我的应用程序崩溃。没有addOnPageListener,它可以正常工作。我想这样做,以便可以在操作栏中放置一些标题动画,也许还可以放一些背景效果。

它是这样的: View Pager App Screenshot

任何帮助或建议将不胜感激,谢谢!

LogCat: https://pastebin.com/xqsBQ46h

JavaCode:

package com.example.varungovind.tm_alpha000;

import android.os.Bundle;
...

public class HomeFragment extends Fragment implements NavigationView.OnNavigationItemSelectedListener{

    private DrawerLayout mDrawer;
    private ViewPager mViewPager;
    private SamplePagerAdapter mPagerAdapter;
    private Toolbar mToolBar;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);

        //sets the system bar transparent
        Objects.requireNonNull(getActivity()).getWindow().getDecorView().setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_VISIBLE);



        //ToolBar and Navigation Drawer
        mToolBar = view.findViewById(R.id.fragment_toolbar);
        ((AppCompatActivity)getActivity()).setSupportActionBar(mToolBar);
        mDrawer = view.findViewById(R.id.nav_drawer_layout);
        NavigationView navigationView = view.findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(getActivity(), mDrawer, mToolBar, R.string.nav_drawer_open, R.string.nav_drawer_close);
        mDrawer.addDrawerListener(toggle);
        toggle.syncState();

        //Setting toolbar title


        ((AppCompatActivity) getActivity()).getSupportActionBar().setTitle("");
        final Animation fade_in = AnimationUtils.loadAnimation(getContext(), R.anim.fade_in);
        final Animation fade_out = AnimationUtils.loadAnimation(getContext(), R.anim.fade_out);
        final TextView toolbar_title = view.findViewById(R.id.toolbar_title);

        //This line of code causes the error
        mViewPager.addOnPageChangeListener(viewListener);

        //SliderViewPager
        mViewPager = view.findViewById(R.id.fragment_home_view_pager);
        mPagerAdapter = new SamplePagerAdapter(getChildFragmentManager());
        mViewPager.setAdapter(mPagerAdapter);
        mViewPager.setPageTransformer(false, new ZoomOutPageTransformer());

        return view;
    }

    ViewPager.OnPageChangeListener viewListener = new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}

        @Override
        public void onPageSelected(int position) {}

        @Override
        public void onPageScrollStateChanged(int state) {}
    };

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
        switch (menuItem.getItemId()) {
            case R.id.nav_about:
                break;
            case R.id.nav_donate:
                break;
            case R.id.nav_done:
                mViewPager.setCurrentItem(1, true);
                break;
            case R.id.nav_upcoming:
                mViewPager.setCurrentItem(0, true);
                break;
            case R.id.nav_feedback:
                break;
            case R.id.nav_drawer_new_list:
                break;
            case R.id.nav_settings:
                break;
        }
        mDrawer.closeDrawer(GravityCompat.START);

        return true;
    }

    private class SamplePagerAdapter extends FragmentPagerAdapter {
        public SamplePagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public CFragment getItem(int position) {
            /** Show a Fragment based on the position of the current screen */
            if (position == 0) {
                return new Upcoming_ChildFragment();
            } else
                return new Done_ChildFragment();
        }

        @Override
        public int getCount() {
            // Show 2 total pages.
            return 2;
        }

    }
}

1 个答案:

答案 0 :(得分:0)

尝试添加侦听器mViewPager.addOnPageChangeListener(viewListener); 设置adpater之后。