1. 程式人生 > >解決TextView多行滑動與NestedScrollView等,滑動沖突,我的解決方案

解決TextView多行滑動與NestedScrollView等,滑動沖突,我的解決方案

rip max 解決 底部 e-mail ast sts end 謝謝

1.首先要明白,什麽時候回TextView處理滑動,什麽時候不處理滑動

1.1往上滑動,到達文本底部就不要再處理了,如果往上滑動不在底部則繼續TextView滑動

1.2往下滑動,到達文本頂部就不要再處理了,如果往下滑動不在頂部則繼續TextView滑動

關鍵計算地方:

      1.當前是上滑動還是下滑動(相對於屏幕) ,使用ev.getRawY()獲得當前滑動位置在屏幕哪個地方

      2. 計算文本客滑動到哪裏即可停止, (行高*總文本行數)- (行高 * 最多顯示行數) int sum = getLineHeight() * getLineCount() - getLineHeight() * getMaxLines();

廢話不說了,上代碼,歡迎參考使用,轉載請說明出處,原創不易且行且珍惜,謝謝支持

import android.content.Context;
import android.text.method.ScrollingMovementMethod;
import android.util.AttributeSet;
import android.view.MotionEvent;

import com.suxuantech.erpsys.utils.L;

/**
 * ......................我佛慈悲....................
 * ......................_oo0oo_.....................
 * .....................o8888888o....................
 * .....................88" . "88....................
 * .....................(| -_- |)....................
 * .....................0\  =  /0....................
 * ...................___/`---‘\___..................
 * ..................‘ \\|     |// ‘.................
 * ................./ \\|||  :  |||// \..............
 * .............../ _||||| -卍-|||||- \..............
 * ..............|   | \\\  -  /// |   |.............
 * ..............| \_|  ‘‘\---/‘‘  |_/ |.............
 * ..............\  .-\__  ‘-‘  ___/-. /.............
 * ............___‘. .‘  /--.--\  `. .‘___...........
 * .........."" ‘<  `.___\_<|>_/___.‘ >‘ ""..........
 * ........| | :  `- \`.;`\ _ /`;.`/ - ` : | |.......
 * ........\  \ `_.   \_ __\ /__ _/   .-` /  /.......
 * ....=====`-.____`.___ \_____/___.-`___.-‘=====....
 * ......................`=---=‘.....................
 * ..................佛祖開光 ,永無BUG................
 *
 * @author Created by 李站旗 on 2018/3/3 0003 15:25 .
 *         QQ:1032992210
 *         E-mail:[email protected]
 * @Description: 可滑動的TextView, 並且解決了與 ScrollView等的滑動沖突
 
*/ public class ScrollTextView extends android.support.v7.widget.AppCompatTextView { public ScrollTextView(Context context) { super(context); setMovementMethod(ScrollingMovementMethod.getInstance()); } public ScrollTextView(Context context, AttributeSet attrs) { super(context, attrs); setMovementMethod(ScrollingMovementMethod.getInstance()); }
public ScrollTextView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); setMovementMethod(ScrollingMovementMethod.getInstance()); } float lastScrollY = 0; @Override public boolean onTouchEvent(MotionEvent ev) { if (getLineCount() > getMaxLines()) { if (ev.getAction() == MotionEvent.ACTION_DOWN) { lastScrollY = ev.getRawY(); L.d("lldd","down:"+lastScrollY); } else if (ev.getAction() == MotionEvent.ACTION_MOVE) { //滑動到頭並且還在繼續上滑動,或者滑動到底部就不要再攔截了(有誤差) int sum = getLineHeight() * getLineCount() - getLineHeight() * getMaxLines(); //計算上次與本次差 float diff = lastScrollY - ev.getRawY(); if (diff>0){//下滑動並且到達了底部也不要處理了 //底部這裏用abs的原因是,因為計算sum的時候有些誤差 if (Math.abs(sum - getScrollY())<5) { getParent().requestDisallowInterceptTouchEvent(false); } else { getParent().requestDisallowInterceptTouchEvent(true); } }else if (diff<0){//上滑動 if (getScrollY() == 0) {//上滑動並且已經到達了頂部就不要在處理了 getParent().requestDisallowInterceptTouchEvent(false); } else { getParent().requestDisallowInterceptTouchEvent(true); } } lastScrollY = ev.getRawY(); } else { getParent().requestDisallowInterceptTouchEvent(false); } } return super.onTouchEvent(ev); } }

解決TextView多行滑動與NestedScrollView等,滑動沖突,我的解決方案