向左或向右滑动时更改背景颜色

时间:2016-10-05 19:22:00

标签: android swipe

RelativeLayout有三个TexView。我想要在向左或向右滑动时更改RelativeLayout的背景颜色(仅更改背景中Relativelayout内的所有其他视图相同)。

我尝试了ViewPager,但我没有得到我想要的东西。我用滑动动画附加了GIF轰鸣声。任何帮助,将不胜感激。谢谢!

以下是我到目前为止尝试的代码:

//some code
setContentView(R.layout.activity_main);
 mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);
 mViewPager.setOnPageChangeListener(new CustomOnPageChangeListener());
        setUpColors();
}
private void setUpColors(){

        Integer color1 = Color.BLACK;
        Integer color2 = Color.BLUE;
        Integer color3 = Color.GREEN;

        Integer[] colors_temp = {color1, color2, color3};
        colors = colors_temp;

    }
public class SectionsPagerAdapter extends FragmentPagerAdapter {

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

        @Override
        public Fragment getItem(int position) {
            return PlaceholderFragment.newInstance(position + 1);
        }

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

    public static class PlaceholderFragment extends Fragment {
        /**
         * The fragment argument representing the section number for this
         * fragment.
         */
        private static final String ARG_SECTION_NUMBER = "section_number";

        /**
         * Returns a new instance of this fragment for the given section
         * number.
         */
        public static PlaceholderFragment newInstance(int sectionNumber) {
            PlaceholderFragment fragment = new PlaceholderFragment();
            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_NUMBER, sectionNumber);
            fragment.setArguments(args);
            return fragment;
        }

        public PlaceholderFragment() {
        }
    }

    public class CustomOnPageChangeListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            if(position < (mSectionsPagerAdapter.getCount() -1) && position < (colors.length - 1)) {

                mViewPager.setBackgroundColor((Integer) argbEvaluator.evaluate(positionOffset, colors[position], colors[position + 1]));

            } else {

                // the last page color
                mViewPager.setBackgroundColor(colors[colors.length - 1]);

            }

        }

        @Override
        public void onPageSelected(int position) {
        }

        @Override
        public void onPageScrollStateChanged(int i) {
        }

    }

它会在滑动时改变背景颜色,但我想要一个像gif一样的动画。当你滑动时它会逐渐消失。

1 个答案:

答案 0 :(得分:0)

你必须使用viewPage和RelativeLayout的一些方法。

switch(mView.getCurrentItem()){
case 0: mRelativeLayout.setBackgroundColor(Color.Red);break;
case 1: mRelativeLayout.setBackgroundColor(Color.Blue);break;
case 2: mRelativeLayout.setBackgroundColor(Color.Black);break;
}

我希望这会是肝脏