有哪些好的做兼职网站有哪些,信用体系建设网站维运工作制度,代做seo关键词排名,wordpress数据清除缓存项目需求
有一个文本数据比较长#xff0c;需要在文本右侧加一个SeekBar#xff0c;然后根据SeekBar的上下滚动来控制文本的滚动。
项目实现
我们使用TextView来显示文本#xff0c;但是文本比较长的话#xff0c;需要在TextView外面套一个ScrollView#xff0c;但是我…项目需求
有一个文本数据比较长需要在文本右侧加一个SeekBar然后根据SeekBar的上下滚动来控制文本的滚动。
项目实现
我们使用TextView来显示文本但是文本比较长的话需要在TextView外面套一个ScrollView但是我们现在这个文本是上下滚动的很巧不巧的是我们的文本在一个上下滚动的Recyclerview的item里面这下就很搞了因为两个都是上下滚动的我们需要做一个处理。
首先自定义一个ScrollView
public class NonScrollableScrollView extends ScrollView {public NonScrollableScrollView(Context context) {super(context);}public NonScrollableScrollView(Context context, AttributeSet attrs) {super(context, attrs);}public NonScrollableScrollView(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);}Overridepublic boolean onTouchEvent(MotionEvent ev) {return false;}Overridepublic boolean onInterceptTouchEvent(MotionEvent ev) {return false;}
}
重写其触摸事件方法使其不响应触摸事件。所有这样的话我们的TextView就不能通过自己滚动了然后我们通过这个SeekBar来控制TextView的滚动
接下来是对SeekBar的修改以下部分内容来自知乎 https://zhuanlan.zhihu.com/p/622534050
SuppressLint(AppCompatCustomView)
public class VerticalSeekBar extends SeekBar {private boolean isTopToBottom false;//显示进度方向是否从上到下默认false从下到上public VerticalSeekBar(Context context) {super(context);}public VerticalSeekBar(Context context, AttributeSet attrs) {super(context, attrs);TypedArray ta context.getTheme().obtainStyledAttributes(attrs, R.styleable.VerticalSeekBar, 0, 0);try {isTopToBottom ta.getBoolean(R.styleable.VerticalSeekBar_isTopToBottom, false);} finally {ta.recycle();}}public VerticalSeekBar(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);}public void setOnSeekBarChangeListener(OnSeekBarChangeListener l) {mOnSeekBarChangeListener l;}void onStartTrackingTouch() {if (mOnSeekBarChangeListener ! null) {mOnSeekBarChangeListener.onStartTrackingTouch(this);}}void onStopTrackingTouch() {if (mOnSeekBarChangeListener ! null) {mOnSeekBarChangeListener.onStopTrackingTouch(this);}}protected void onSizeChanged(int w, int h, int oldw, int oldh) {super.onSizeChanged(h, w, oldh, oldw);}Overridepublic synchronized void setProgress(int progress) {super.setProgress(progress);onSizeChanged(getWidth(), getHeight(), 0, 0);}Overrideprotected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {super.onMeasure(heightMeasureSpec, widthMeasureSpec);setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());}protected void onDraw(Canvas c) {if (isTopToBottom) {//显示进度方向 从上到下c.rotate(90);c.translate(0, -getWidth());} else {//显示进度方向 从下到上c.rotate(-90);c.translate(-getHeight(), 0);}super.onDraw(c);}Overridepublic boolean onTouchEvent(MotionEvent event) {if (!isEnabled()) {return false;}switch (event.getAction()) {case MotionEvent.ACTION_DOWN:onStartTrackingTouch();trackTouchEvent(event);break;case MotionEvent.ACTION_MOVE:trackTouchEvent(event);attemptClaimDrag();break;case MotionEvent.ACTION_UP:trackTouchEvent(event);onStopTrackingTouch();break;case MotionEvent.ACTION_CANCEL:onStopTrackingTouch();break;}return true;
// getParent().requestDisallowInterceptTouchEvent(true);
// return super.onTouchEvent(event);}private void trackTouchEvent(MotionEvent event) {//关键更改2int progress getMax() - (int) (getMax() * event.getY() / getHeight());//触摸进度方向 从下到上if (isTopToBottom) {progress (int) (getMax() * event.getY() / getHeight());//触摸进度方向 从上到下}setProgress(progress);if (mOnSeekBarChangeListener ! null) {mOnSeekBarChangeListener.onProgressChanged(this, progress);}}private void attemptClaimDrag() {if (getParent() ! null) {getParent().requestDisallowInterceptTouchEvent(true);}}public boolean dispatchKeyEvent(KeyEvent event) {if (event.getAction() KeyEvent.ACTION_DOWN) {KeyEvent newEvent null;switch (event.getKeyCode()) {case KeyEvent.KEYCODE_DPAD_UP:newEvent new KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_DPAD_RIGHT);break;case KeyEvent.KEYCODE_DPAD_DOWN:newEvent new KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_DPAD_LEFT);break;case KeyEvent.KEYCODE_DPAD_LEFT:newEvent new KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_DPAD_DOWN);break;case KeyEvent.KEYCODE_DPAD_RIGHT:newEvent new KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_DPAD_UP);break;default:newEvent new KeyEvent(KeyEvent.ACTION_DOWN,event.getKeyCode());break;}KeyEvent.DispatcherState dispatcherState new KeyEvent.DispatcherState();dispatcherState.isTracking(event);return newEvent.dispatch(this, dispatcherState, event);}return false;}/*** 设置显示进度方向** param isTopToBottom true 方向从上到下*/public void setTopToBottom(boolean isTopToBottom) {this.isTopToBottom isTopToBottom;}private OnSeekBarChangeListener mOnSeekBarChangeListener;public interface OnSeekBarChangeListener {void onProgressChanged(VerticalSeekBar VerticalSeekBar, int progress);void onStartTrackingTouch(VerticalSeekBar VerticalSeekBar);void onStopTrackingTouch(VerticalSeekBar VerticalSeekBar);}
}
需要在attrs文件里面添加 (需要注意因为在知乎上面没有这个东西) declare-styleable nameVerticalSeekBarattr nameisTopToBottom formatboolean //declare-styleable设置 progress_vertical_drawable2
?xml version1.0 encodingutf-8?
layer-list xmlns:androidhttp://schemas.android.com/apk/res/androiditem android:idandroid:id/backgroundshapecorners android:radius5dp /solid android:color#D9EFFF //shape/itemitem android:idandroid:id/progressscale android:scaleWidth100%shapecorners android:radius5dp /solid android:color#5EB2FF //shape/scale/itemitem android:idandroid:id/secondaryProgressscale android:scaleWidth100%shapecorners android:radius5dp /solid android:color#5EB2FF //shape/scale/item
/layer-list设置ic_thumb
?xml version1.0 encodingutf-8?
layer-list xmlns:androidhttp://schemas.android.com/apk/res/androiditemandroid:widthdimen/dp_20android:heightdimen/dp_20shape android:shapeovalgradientandroid:angle180android:endColor#1C000000android:startColor#1Cffffff //shape/itemitemandroid:bottom1dpandroid:left1dpandroid:right1dpandroid:top1dpshape android:shapeovalsolid android:color#5EB2FF /strokeandroid:width6dpandroid:color#FFFFFF //shape/item
/layer-list设置Style !--自定义SeekBar样式--style nameSeekbarStyleitem nameandroid:indeterminateDrawable!--未知资源时显示--android:drawable/progress_indeterminate_horizontal/itemitem nameandroid:progressDrawabledrawable/progress_vertical_drawable2/itemitem nameandroid:max100/itemitem nameandroid:progress0/itemitem nameandroid:maxHeightdimen/dp_60/item
!-- item nameandroid:minHeightdimen/dp_10/item--item nameandroid:thumbdrawable/ic_thumb/item
!-- item nameandroid:thumbOffsetdimen/dp_20/item--/style然后就可以在xml布局中使用了 com.complex.app.view.VerticalSeekBarandroid:idid/seekBar_Textstylestyle/SeekbarStyleandroid:layout_widthwrap_contentandroid:layout_heightdimen/dp_60android:backgroundandroid:color/transparentandroid:splitTrackfalseapp:isTopToBottomtrue /android:splitTrackfalse是为了让这个滑块周围的背景变的透明不然就只能是一个正方形的滑块图案了
然后我们在RecyclerView的Adapter里面进行设置
protected void convert(BaseViewHolder helper, ElectronicFencePoint item) {NonScrollableScrollView scrollView helper.getView(R.id.ns_scroll);VerticalSeekBar seekBar helper.getView(R.id.seekBar_Text);scrollView.post(() - {int maxScroll scrollView.getChildAt(0).getHeight() - scrollView.getHeight();if (maxScroll 0) {//这里我的逻辑是当TextView的文本显示内容不多不需要滑动的时候设置滑块滑动到底部显示seekBar.setProgress(seekBar.getMax());} else {//当TextView的文本很多需要滑动的时候将滑块放在顶部seekBar.setProgress(0);seekBar.setMax(maxScroll);}});seekBar.setOnSeekBarChangeListener(new VerticalSeekBar.OnSeekBarChangeListener() {Overridepublic void onProgressChanged(VerticalSeekBar VerticalSeekBar, int progress) {scrollView.scrollTo(0, progress);}Overridepublic void onStartTrackingTouch(VerticalSeekBar VerticalSeekBar) {}Overridepublic void onStopTrackingTouch(VerticalSeekBar VerticalSeekBar) {}});scrollView.setOnScrollChangeListener(new View.OnScrollChangeListener() {Overridepublic void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {seekBar.setProgress(scrollY);}});
}补充 开始滑动前 开始滑动后 这个只需要修改一下样式就好了
ic_thumb
?xml version1.0 encodingutf-8?
layer-list xmlns:androidhttp://schemas.android.com/apk/res/androiditemandroid:widthdimen/dp_15android:heightdimen/dp_15shape android:shapeovalgradientandroid:endColor#1C000000android:startColor#1Cffffff //shape/itemitemandroid:bottom1dpandroid:left1dpandroid:right1dpandroid:top1dpshape android:shapeovalsolid android:color#5EB2FF /strokeandroid:width2dpandroid:color#FFFFFF //shape/item
/layer-listprogress_vertical_drawable2
?xml version1.0 encodingutf-8?
layer-list xmlns:androidhttp://schemas.android.com/apk/res/android!--设置滑轨颜色滑过部分和未滑过部分--!--未滑过部分滑轨颜色--itemandroid:idandroid:id/backgroundandroid:height4dpandroid:gravitycentershapecorners android:radius67dp/solid android:color#4d000000//shape/item!--滑过部分滑轨颜色--itemandroid:idandroid:id/progressandroid:height6dpandroid:gravitycenterclipshapecorners android:radius67dp/solid android:color#2196F3//shape/clip/item
/layer-listSeekbarStyle !--自定义SeekBar样式--style nameSeekbarStyle parentWidget.AppCompat.SeekBaritem nameandroid:progressDrawabledrawable/progress_vertical_drawable2/itemitem nameandroid:thumbdrawable/ic_thumb/item/stylexml应用 com.southgnss.digitalconstruction.view.VerticalSeekBarandroid:idid/seekBar_Textstylestyle/SeekbarStyleandroid:layout_widthdimen/dp_20android:layout_heightdimen/dp_60android:backgroundnullandroid:splitTrackfalseapp:isTopToBottomtrue //LinearLayout