为什么在viewPager方法中动画不起作用?

问题描述

我有2个视图的viewPager。我需要通过单击和翻转来获取第二个视图。有用。但是当我在第二个视图中时,我必须具有按钮动画。这个按钮是我的customView。此动画无效。而且我不知道为什么。 binding.libraryToolbar.setonMenuItemClickListener { artistAdapter.submitList( when (it.itemId) { R.id.sort_none -> musicModel.artists.value R.id.sort_alpha_up -> artistAdapter.currentList.sortedBy { it.name } R.id.sort_alpha_down -> artistAdapter.currentList.sortedByDescending { it.name } else -> artistAdapter.currentList } ) true } 方法有效,因为我得到了日志。

我的customView类:

onPageSelected(int position)

和我的活动课程:

public class CustomView extends LinearLayout {
    private LayoutInflater mInflater;
    private Button button;
    private ViewPager viewPager;
    private float buttonWidth;

    public void setPager(ViewPager viewPager) {
        this.viewPager = viewPager;

        viewPager.addOnPagechangelistener(new ViewPager.OnPagechangelistener() {
            @Override
            public void onPageScrolled(int position,float positionOffset,int positionOffsetPixels) {
              setClick();
            }

            @Override
            public void onPageSelected(int position) {

                ViewTreeObserver vto = button.getViewTreeObserver();
                vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {
                        buttonWidth = button.getWidth();
                    }
                });

                displayMetrics metrics = new displayMetrics();
                ((Activity) getContext()).getwindowManager().getDefaultdisplay().getRealMetrics(metrics);

                boolean isdisplayPortrait = getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
                float middleScreen = isdisplayPortrait ? metrics.widthPixels / 2 : metrics.heightPixels / 2;
                float movingdistanceForButton = middleScreen - buttonWidth / 2;

                final TranslateAnimation animation = new TranslateAnimation(0,movingdistanceForButton,0);
                animation.setDuration(3000);
                animation.setFillAfter(true);
                if (position == 1) {
                    button.setAnimation(animation);
                    Log.e("position1","pos1");
                } else {
                    button.getAnimation().cancel();
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

   public CustomView(@NonNull Context context,@Nullable AttributeSet attrs) {
        super(context,attrs);
        mInflater = LayoutInflater.from(context);

        View v = mInflater.inflate(R.layout.custom_view,this,true);
        button = v.findViewById(R.id.nextBotton);
 }

        public void setClick() {

        button.setonClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
               viewPager.setCurrentItem(viewPager.getCurrentItem() + 1,true);

            }
        });
    }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)