我有一个我正在做的应用程序,它有一个 FragmentPagerAdapter 设置,其中有 3 个 fragment 可以在主页面上滑动。
我一直在尝试设置它,以便当您在 fragment 之间滑动时,操作栏会改变颜色(淡入和淡出)。
但是我不确定我应该怎么做。 在 fragment 之间滑动时调用什么方法? I.E 我应该把代码放在哪里来改变操作栏的颜色?
还有我如何获得淡入和淡出效果(所以它会从一种颜色淡出到另一种颜色)?
非常感谢任何人的帮助。
提前致谢
干杯 科里:)
最佳答案
What method is called when you swipe between fragments?
您正在寻找 ViewPager.OnPageChangeListener.onPageScrolled
.这会给你:
- position 当前显示的第一个页面的位置索引。
- positionOffset 来自 [0, 1) 的值,指示与页面位置的偏移量。
- positionOffsetPixels 以像素为单位的值,指示与位置的偏移量。
尽管如此,您只需要前两个参数。您要做的是将特定颜色绑定(bind)到每个 fragment ,检索当前页面和下一页颜色,然后使用 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);
}
这是一个简单的例子:
颜色 fragment
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);
}
}
将其整合在一起
@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);
}
结果
http://gfycat.com/CautiousBewitchedJabiru
希望对您有所帮助!
关于android - 在 fragment 之间滑动时如何改变 ActionBar 颜色( Material 设计)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26812554/