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); } }
原文地址:https://www.cnblogs.com/lizhanqi/p/8520836.html
时间: 2024-11-06 18:40:24