问题是:有一个ViewPager具有相同类型的片段。有一个Spinner,其中包含一个值列表,选中后,替换所有片段的内容并移动到1个片段。是否可以在从列表中选择值时添加ViewPager的alpha动画,然后选择第一个片段?试图这样做,但动画总是笨拙地调用,并且它会多次出现。
ActivityViewPager
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityCompat.postponeEnterTransition(this);
setContentView(R.layout.activity_treining_info);
if (getIntent().hasExtra(ClassTraining.KEY_TR_TYPE)) {
type = getIntent().getIntExtra(ClassTr.KEY_TR_TYPE, 1);
lvl = getIntent().getIntExtra(ClassTr.KEY_TR_LVL, 1);
}
animation = new AnimationClass(this);
dataRes=DataResCompound.getInstance();
clInfo = (ConstraintLayout)findViewById(R.id.cl_info);
initPagerAdapter();
initSpinner();
}
private void initPagerAdapter() {
pager = (ViewPager) findViewById(R.id.pager);
pagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager());
pager.setAdapter(pagerAdapter);
pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
lvl = position+1;
}
@Override
public void onPageScrolled(int position, float positionOffset,
int positionOffsetPixels) {
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
pager.setCurrentItem(lvl-1);
}
private void initSpinner() {
final int[] size1 = {0};
//ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.trainings, R.layout.spiner_layout);
spnTrain = (Spinner)findViewById(R.id.spn_train);
ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.trainings, android.R.layout.simple_spinner_dropdown_item);
spnTrain.setAdapter(adapter);
spnTrain.setSelection(type-1);
spnTrain.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if(isStart>0) {
lvl = 1;
type=position+1;
getSupportFragmentManager().getFragments().clear();
pagerAdapter.notifyDataSetChanged();
alphaViewTraining();
}
isStart++;
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
private void alphaViewTraining(){
clInfo.startAnimation(animation.getAlphaAnimation(pager, clInfo, 2, lvl-1)); } private class MyFragmentPagerAdapter extends FragmentPagerAdapter {
public MyFragmentPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return FragmentPageTrainInfo.newInstance(typeTrening, position+1);
}
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
@Override
public int getCount() {
return PAGE_COUNT;
}
@Override
public CharSequence getPageTitle(int position) {
int lvl=position+1;
return "Уровень " + lvl;
}
}
AnimationClass
public Animation getAlphaAnimation(View _view, final View layout, final int typeView, Object _value){
view=_view;
value=_value;
in = AnimationUtils.loadAnimation(context, R.anim.alpha_in);
out = AnimationUtils.loadAnimation(context, R.anim.alpha_out);
out.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
setValueToView(view, layout, value, typeView);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
return out;}private void setValueToView(View view, View layout, Object value, int typeView){
((ViewPager)view).setCurrentItem((int)value);
layout.startAnimation(in);
break;
view.startAnimation(in);}
在我看来,这是一个重叠标准动画ViewPager和我的问题。是否提示可以制作一些东西?