在Android Studio中将布局参数动画从Kotlin转换为Java

lsmepo6l  于 2023-02-19  发布在  Kotlin
关注(0)|答案(1)|浏览(161)

我的Kotlin项目中有一个onScrollListener动画,我也想在Java中使用该代码,我尝试将Kotlin代码转换为Java,但我看到一些错误,当检测到onScrollListener时,我的应用程序崩溃,动画隐藏了TextView
这是Kotlin密码

class MainActivity : AppCompatActivity() 
{
    private lateinit var category_recycler: RecyclerView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        
        var btnExtended = true
        //animated scroll
        var animator : ValueAnimator? = null

        //the note onscroll listener
        note_recycler.addOnScrollListener(object : RecyclerView.OnScrollListener(){
            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)
                if (animator==null){
                    animator=createanimator()
                }
                if (dy>0 && btnExtended)
                {
                    animator!!.start()
                    btnExtended = !btnExtended
                }else if (dy<0 && !btnExtended){
                    animator!!.reverse()
                    btnExtended = !btnExtended

                }
            }
        })

  }

   private fun createanimator(): ValueAnimator{
        var textview = findViewById<TextView>(R.id.textview)  //textview i want to hide
        val initSize=textview.measuredWidth
        val animator=ValueAnimator.ofInt(initSize,0)
        animator.duration=250
        animator.addUpdateListener { animation ->
            val value = animation.animatedValue as Int
            val layoutParams = textview.layoutParams   //line 50
            layoutParams.width = value
            textview.requestLayout()

        }
        return animator
    }

}



  //So far this is the code i has converted to java
 public class MainActivity extends AppCompatActivity implements NoteListener {
  
    private RecyclerView note_recycler;

    private ValueAnimator animator = null;

    private Boolean btnExtended;

    private TextView textview;

    private LinearLayout.LayoutParams layoutParams;

      @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


          btnExtended = true;
        note_recycler.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if(animator==null){
                    animator=createanimator();

                }
                if (dy>0 && btnExtended)
                {
                    if(animator!=null){
                        animator.start();
                    }
                  
                    btnExtended = !btnExtended;
                }
                else if (dy<0 && !btnExtended){
                    if(animator!=null){
                        animator.reverse();
                    }
              
                    btnExtended = !btnExtended;

                }

            }
        });


      }

     
    private ValueAnimator createanimator() {
        textview = findViewById(R.id.textview);
        int initSize = textview.getMeasuredWidth();
        animator=ValueAnimator.ofInt(initSize,0);
        animator.setDuration(250);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(@NonNull ValueAnimator animation) {
                int value = (int) animation.getAnimatedValue();
                          //there is a missing line here named line 50 from the kotlin code,          Please can you help me convert it and also cross check the other code for the java convertion
                layoutParams.width =value;
                textview.requestLayout();

            }
        });

      return animator;
    }

}

//Please我是Kotlin的新手,我不确定这个java转换是否正确,请你检查一下,并转换我无法转换的第50行

798qvoo8

798qvoo81#

经过几次环顾四周,我不会说我自己找到了答案,但它确实有效:)

ViewGroup.LayoutParams layoutParams = textview.getLayoutParams();

这是缺少的第50行的工作,令人震惊的是没有错误的其余转换.+4小时哈哈无论哪种方式这是一个OnScrollAnimation像在gmail与他们的组成按钮,我设置按钮 Package 内容,当动画隐藏按钮中的文本,它只显示图标.

相关问题