andorid封裝自動滾動TextView,沒有焦點,有兩種滾動方式
阿新 • • 發佈:2019-01-01
為了解決使用原生自動滾動的TextView,在ListView上item不能點選,是因為獲取焦點所以不能點選,還有就是隻有一種方式的滾動。
關於ScreenUtils 點選開啟連結
主要程式碼:
package com.sun.framework.CustomizeVC; import android.content.Context; import android.os.Handler; import android.util.AttributeSet; import android.util.TypedValue; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.widget.FrameLayout; import android.widget.HorizontalScrollView; import android.widget.LinearLayout; import android.widget.RelativeLayout; import android.widget.TextView; import com.sun.framework.Utils.ScreenUtils; /** * Created by apple on 2017/4/21. */ public class AutoHorizontalScrollTextView extends RelativeLayout{ Context context; private final Handler handler = new Handler(); RelativeLayout contentRelativeLayout; HorizontalScrollView scrollView; boolean isRight; TextView textView,textViewTwo; CharSequence text; RelativeLayout.LayoutParams leftViewParams,rightViewParams; AutoHorizontalScrollDirectionEnum autoHorizontalScrollDirectionEnum; ScreenUtils screenUtils; public int mDelayMillis;//滾動延遲毫秒數 public int mScrollEdgeDelayMillis;//滾動到邊緣延遲毫秒數 public AutoHorizontalScrollTextView(Context context) { super(context); } public AutoHorizontalScrollTextView(Context context, AttributeSet attrs) { this(context, attrs, -1); } public AutoHorizontalScrollTextView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(context); } private void init(Context context) { this.context = context; text = "——"; mDelayMillis = 100; mScrollEdgeDelayMillis = 1000; screenUtils = new ScreenUtils(context); refreshAutoHorizontalScrollTextView(); } private void refreshAutoHorizontalScrollTextView(){ if (this.getChildCount() > 0){ this.removeAllViews(); } scrollView = new HorizontalScrollView(context); scrollView.setOverScrollMode(View.OVER_SCROLL_NEVER); scrollView.setHorizontalScrollBarEnabled(false); scrollView.setHorizontalFadingEdgeEnabled(false); scrollView.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent motionEvent) { return true;//禁止手動滑動 } }); this.addView(scrollView); RelativeLayout.LayoutParams scrollViewParams = (RelativeLayout.LayoutParams) scrollView.getLayoutParams(); scrollViewParams.width = ViewGroup.LayoutParams.WRAP_CONTENT; scrollViewParams.height = ViewGroup.LayoutParams.WRAP_CONTENT; scrollView.setHorizontalScrollBarEnabled(false); contentRelativeLayout = new RelativeLayout(context); scrollView.addView(contentRelativeLayout); FrameLayout.LayoutParams contentParams = (FrameLayout.LayoutParams) contentRelativeLayout.getLayoutParams(); contentParams.width = ViewGroup.LayoutParams.WRAP_CONTENT; contentParams.height = ViewGroup.LayoutParams.WRAP_CONTENT; cancelFocus(contentRelativeLayout); LinearLayout linearLayout = new LinearLayout(context); linearLayout.setOrientation(LinearLayout.HORIZONTAL); contentRelativeLayout.addView(linearLayout); RelativeLayout.LayoutParams linearLayoutParams = (RelativeLayout.LayoutParams) linearLayout.getLayoutParams(); linearLayoutParams.width = ViewGroup.LayoutParams.WRAP_CONTENT; linearLayoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT; textView = new TextView(context); textView.setText(text); linearLayout.addView(textView); LinearLayout.LayoutParams textViewParams = (LinearLayout.LayoutParams) textView.getLayoutParams(); textViewParams.width = ViewGroup.LayoutParams.WRAP_CONTENT; textViewParams.height = ViewGroup.LayoutParams.WRAP_CONTENT; textViewTwo = new TextView(context); textViewTwo.setText(text); textViewTwo.setMaxLines(1); linearLayout.addView(textViewTwo); ScreenUtils.ViewWidthHeight(scrollView, new ScreenUtils.ViewOnGlobalLayoutListener() { @Override public void onGlobalLayout(View view, int width, int Height) { LinearLayout.LayoutParams textView2Params = (LinearLayout.LayoutParams) textViewTwo.getLayoutParams(); textView2Params.width = width; textView2Params.height = ViewGroup.LayoutParams.WRAP_CONTENT; } }); int leftOrRight = screenUtils.dp2px(16); setTextViewPadding(leftOrRight,0,leftOrRight,0); // View leftShadowView = new View(context); // leftShadowView.setBackgroundColor(Color.argb(50,255,255,255)); // // this.addView(leftShadowView); // leftViewParams = (RelativeLayout.LayoutParams) leftShadowView.getLayoutParams(); // leftViewParams.width = 20; // // View rightShadowView = new View(context); // rightShadowView.setBackgroundColor(Color.argb(50,255,255,255)); // this.addView(rightShadowView); // rightViewParams = (RelativeLayout.LayoutParams) rightShadowView.getLayoutParams(); // rightViewParams.width = 20; // rightViewParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE); cancelFocus(this,scrollView,contentRelativeLayout,linearLayout,textView,textViewTwo); handler.post(ScrollRunnable); } private void cancelFocus(View... views){ for (View view : views){ view.setFocusable(false); view.setFocusableInTouchMode(false); } } private Runnable ScrollRunnable = new Runnable() { @Override public void run() { int i = mDelayMillis; int off = contentRelativeLayout.getMeasuredWidth() - scrollView.getWidth(); if (off > 0) { if (scrollView.getScrollX() == off) { isRight = false; if (autoHorizontalScrollDirectionEnum == AutoHorizontalScrollDirectionEnum.Left){ scrollView.scrollTo(0,0); } i = mScrollEdgeDelayMillis; } if (scrollView.getScrollX() == 0) { isRight = true; i = mScrollEdgeDelayMillis; } if (isRight){ scrollView.scrollBy(5, 0); }else { if (autoHorizontalScrollDirectionEnum == AutoHorizontalScrollDirectionEnum.Left_Right) { scrollView.scrollBy(-(5), 0); } } handler.postDelayed(this, i); } } }; public RelativeLayout getContentRelativeLayout() { return contentRelativeLayout; } public TextView getTextView() { return textView; } public TextView getTextViewTwo() { return textViewTwo; } public void setTextSize(float size){ textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP,size); if (autoHorizontalScrollDirectionEnum == AutoHorizontalScrollDirectionEnum.Left){ textViewTwo.setTextSize(TypedValue.COMPLEX_UNIT_DIP,size); } } public void setTextColor(int color){ textView.setTextColor(color); if (autoHorizontalScrollDirectionEnum == AutoHorizontalScrollDirectionEnum.Left){ textViewTwo.setTextColor(color); } } public void setTextViewPadding(int left, int top, int right, int bottom) { textView.setPadding(left,top,right,bottom); textViewTwo.setPadding(0,top,right,bottom); } public void setScrollDirectionLeftText(CharSequence text){ this.autoHorizontalScrollDirectionEnum = AutoHorizontalScrollDirectionEnum.Left; textViewTwo.setVisibility(View.VISIBLE); commonSetText(text); } public void setScrollDirectionRight_LeftText(CharSequence text){ this.autoHorizontalScrollDirectionEnum = AutoHorizontalScrollDirectionEnum.Left_Right; textViewTwo.setVisibility(View.GONE); commonSetText(text); } private void commonSetText(CharSequence text){ this.text = text; textView.setText(text); textViewTwo.setText(text); scrollView.scrollTo(0,0); handler.removeCallbacks(ScrollRunnable); handler.post(ScrollRunnable); } public enum AutoHorizontalScrollDirectionEnum { Left(0),//向左邊滾動 Left_Right(1);//右左滾動 AutoHorizontalScrollDirectionEnum(int value) { this.value=value; } public int Value() { return value; } private int value; } }
使用:
xml佈局
<?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android"> <data class="ForecastBinding"> </data> <LinearLayout android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <com.sun.framework.CustomizeVC.AutoHorizontalScrollTextView android:id="@+id/autoHorizontalScrollTextView" android:layout_width="200dp" android:layout_height="wrap_content"> </com.sun.framework.CustomizeVC.AutoHorizontalScrollTextView> <com.sun.framework.CustomizeVC.AutoHorizontalScrollTextView android:id="@+id/autoHorizontalScrollTextView2" android:layout_width="200dp" android:layout_height="wrap_content"> </com.sun.framework.CustomizeVC.AutoHorizontalScrollTextView> </LinearLayout> </layout>
ForecastBinding binding; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { binding = DataBindingUtil.inflate(inflater, R.layout.forecast_fra, container, false); binding.autoHorizontalScrollTextView.setScrollDirectionRight_LeftText("自動滑動TextView。。。。。。。。。。。。。。。。"); binding.autoHorizontalScrollTextView2.setScrollDirectionLeftText("自動滑動TextView。。。。。。。。。。。。。。。。"); return binding.getRoot(); }
我的業餘技術微信公眾號:YKJGZH,歡迎大家進入