为了解决使用原生自动滚动的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,欢迎大家进入