我正在尝试使用viewpager进行此主题选择。我创建了一个静态arraylist对象,并在此活动中使用它与viewpager。但无法妥善管理它。它是从位置+ 1值设置工具栏和状态颜色,其余的正常工作。背景,文本颜色和名称是正确的,但工具栏和状态栏具有下一次迭代的值。
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String COLORSET_POS = "list_pos";
TextView nick,post,countdown;
FrameLayout frameLayout;
Toolbar toolbar;
Button select;
public PlaceholderFragment() {
}
/**
* 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(COLORSET_POS, sectionNumber);
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.colorset_preview_layout, container, false);
int pos = getArguments().getInt(COLORSET_POS);
nick = (TextView) rootView.findViewById(R.id.nick);
post = (TextView) rootView.findViewById(R.id.post);
countdown = (TextView) rootView.findViewById(R.id.countdown);
frameLayout = (FrameLayout) rootView.findViewById(R.id.frame);
toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
select = (Button) rootView.findViewById(R.id.select);
if(HelperClass.colorSetArrayList != null)
setColors(HelperClass.colorSetArrayList.get(pos).colorSet());
Toast.makeText(getActivity(), "Position " + pos + " name " + HelperClassHelperClass.colorSetArrayList.get(pos).getName(), Toast.LENGTH_SHORT).show();
return rootView;
}
synchronized void setColors(ColorSet colorSet){
toolbar.setSubtitle(String.format(getString(R.string.online_count), new Random().nextInt(20)));
if (colorSet != null) {
nick.setTextColor(colorSet.textcolor());
post.setTextColor(colorSet.textcolor());
countdown.setTextColor(colorSet.textcolor());
frameLayout.setBackgroundColor(colorSet.backgroundcolor());
toolbar.setBackgroundColor(colorSet.primarycolor());
nick.setText(.getUsername());
select.setTextColor(colorSet.textcolor());
if(colorSet.getName()!=null) post.setText(colorSet.getName());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
getActivity().getWindow().setStatusBarColor(colorSet.primarydarkcolor());
}
}
}
}
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
return PlaceholderFragment.newInstance(position);
}
@Override
public int getCount() {
return HelperClass.colorSetArrayList.size();
}
@Override
public CharSequence getPageTitle(int position) {
if(HelperClass.colorSetArrayList==null)
return "null";
else return HelperClass.colorSetArrayList.get(position).getName();
}
}