SolveEditTextScrollClash.java 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package com.cooleshow.musicmerge.widget;
  2. import android.view.MotionEvent;
  3. import android.view.View;
  4. import android.widget.EditText;
  5. /**
  6. * Author by pq, Date on 2023/11/10.
  7. */
  8. public class SolveEditTextScrollClash implements View.OnTouchListener{
  9. private EditText editText;
  10. public SolveEditTextScrollClash(EditText editText) {
  11. this.editText = editText;
  12. }
  13. @Override
  14. public boolean onTouch(View view, MotionEvent event) {
  15. //触摸的是EditText而且当前EditText能够滚动则将事件交给EditText处理。否则将事件交由其父类处理
  16. if ((view.getId() == editText.getId() && canVerticalScroll(editText))) {
  17. view.getParent().requestDisallowInterceptTouchEvent(true);
  18. if (event.getAction() == MotionEvent.ACTION_UP) {
  19. view.getParent().requestDisallowInterceptTouchEvent(false);
  20. }
  21. }
  22. return false;
  23. }
  24. /**
  25. * EditText竖直方向能否够滚动
  26. * @param editText 须要推断的EditText
  27. * @return true:能够滚动 false:不能够滚动
  28. */
  29. private boolean canVerticalScroll(EditText editText) {
  30. //滚动的距离
  31. int scrollY = editText.getScrollY();
  32. //控件内容的总高度
  33. int scrollRange = editText.getLayout().getHeight();
  34. //控件实际显示的高度
  35. int scrollExtent = editText.getHeight() - editText.getCompoundPaddingTop() -editText.getCompoundPaddingBottom();
  36. //控件内容总高度与实际显示高度的差值
  37. int scrollDifference = scrollRange - scrollExtent;
  38. if(scrollDifference == 0) {
  39. return false;
  40. }
  41. return (scrollY > 0) || (scrollY < scrollDifference - 1);
  42. }
  43. }