在片段之间滑动时如何更改ActionBar颜色(Material Design)?

Fis*_*fon 5 android android-actionbar fragmentpageradapter material-design

我有一个我正在做的应用程序,它有一个FragmentPagerAdapter设置,其中有3个片段可以在主页面之间滑动.

我一直试图让它设置好,所以当您在片段之间滑动时,操作栏会改变颜色(淡入和淡出).

但是我不确定我应该怎么做.在片段之间滑动时调用什么方法?IE应该在哪里放置代码来改变动作栏的颜色?

而且我如何获得淡入和淡出效果(因此它将一种颜色淡化为另一种颜色)?

我真的很感激任何人的帮助.

提前致谢

干杯科里:)

adn*_*eal 14

在片段之间滑动时调用什么方法?

你在找ViewPager.OnPageChangeListener.onPageScrolled.这会给你:

  • position 当前显示的第一页的位置索引.
  • positionOffset [0,1]中的值,表示距离页面位置的偏移量.
  • positionOffsetPixels指示偏离位置的像素值(以像素为单位).

虽然,您只需要前两个参数.您要做的是将特定颜色绑定到每个片段,检索当前页面和下一页颜色,然后使用positionOffset比率将它们混合在一起以创建新ActionBar背景.

可以在Google的新SlidingTabStrip示例中找到基于比率混合两种颜色的有用算法.0.0将返回第二种颜色,0.5将返回均匀混合,1.0并将返回第一种颜色

static int blendColors(int from, int to, float ratio) {
    final float inverseRation = 1f - ratio;
    final float r = Color.red(from) * ratio + Color.red(to) * inverseRation;
    final float g = Color.green(from) * ratio + Color.green(to) * inverseRation;
    final float b = Color.blue(from) * ratio + Color.blue(to) * inverseRation;
    return Color.rgb((int) r, (int) g, (int) b);
}
Run Code Online (Sandbox Code Playgroud)

这是一个简单的例子:

ColorFragment

public class ColorFragment extends Fragment {

    private static final String KEY_COLOR = "colorfragment:color";

    /** Empty constructor as per the {@link Fragment} docs */
    public ColorFragment() {
    }

    public static ColorFragment newInstance(int color) {
        final Bundle args = new Bundle();
        args.putInt(KEY_COLOR, color);
        final ColorFragment fragment = new ColorFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        final FrameLayout rootView = new FrameLayout(getActivity());
        rootView.setBackgroundColor(getArguments().getInt(KEY_COLOR));
        return rootView;
    }

    public int getColor() {
        return getArguments().getInt(KEY_COLOR);
    }

}
Run Code Online (Sandbox Code Playgroud)

把它们拉在一起

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Set the ActionBar background
    final ColorDrawable actionBarBackground = new ColorDrawable();
    getSupportActionBar().setBackgroundDrawable(actionBarBackground);
    ...
    final PagerAdapter pagerAdapter = ...;
    ...
    // Bind your data to your PagerAdapter
    ...
    final ViewPager pager = ...;
    pager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            super.onPageScrolled(position, positionOffset, positionOffsetPixels);
            if (position >= pagerAdapter.getCount() - 1) {
                // Guard against ArrayIndexOutOfBoundsException
                return;
            }
            // Retrieve the current and next ColorFragment
            final ColorFragment from = (ColorFragment) pagerAdapter.getItem(position);
            final ColorFragment to = (ColorFragment) pagerAdapter.getItem(position + 1);
            // Blend the colors and adjust the ActionBar
            final int blended = blendColors(to.getColor(), from.getColor(), positionOffset);
            actionBarBackground.setColor(blended);
        }

    });
    pager.setAdapter(pagerAdapter);
}
Run Code Online (Sandbox Code Playgroud)

结果

http://gfycat.com/CautiousBewitchedJabiru

我希望能帮到你一些!