Android 垂直滚动条

垂直滚动,不过只能是单个的string类型数据

/**
 * 自动滚动条——垂直
 * Created by IKL on 2019/1/7.
 */

public class EndlessScrollView extends ScrollView {
    private LinearLayout llData;
    private static final int MESSAGE_SCROLL = 10010;
    private static int offset = 1000; //滚动的时间间隔
    private int pageSize, itemHeight, maxScrollHeight;


    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MESSAGE_SCROLL:
                    int scrollY = getScrollY();
                    int delay = 0;

                    if (scrollY >= maxScrollHeight) {
                        scrollTo(0, 0);
                    } else {
                        smoothScrollBy(0, itemHeight);
                        delay = offset;
                    }
                    postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            sendEmptyMessage(MESSAGE_SCROLL);
                        }
                    }, delay);

                    break;

            }
        }
    };

    public EndlessScrollView(Context context) {
        this(context, null);
    }

    public EndlessScrollView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public EndlessScrollView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init() {
        llData = new LinearLayout(getContext());
        llData.setOrientation(LinearLayout.VERTICAL);
        llData.setLayoutParams(new ScrollView.LayoutParams(-1, -1));

        addView(llData);
    }

    /**
     *
     * @param data  显示的数据集合
     * @param pageSize  显示的条目数
     */
    public void setData(final List<String> data, final int pageSize) {

        post(new Runnable() {
            @Override
            public void run() {
                int containerHeight = getMeasuredHeight();

                EndlessScrollView.this.pageSize = pageSize;
                EndlessScrollView.this.itemHeight = containerHeight / pageSize;
                EndlessScrollView.this.maxScrollHeight = data.size() * itemHeight;

                data.addAll(data);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(-1, itemHeight);
                for (int i = 0; i < data.size(); i++) {
                    TextView textView = new TextView(getContext());
                    textView.setTextColor(Color.RED);
                    textView.setGravity(Gravity.CENTER_VERTICAL);
                    textView.setText(data.get(i));
                    textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
                    llData.addView(textView, params);

                }

                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        handler.sendEmptyMessage(MESSAGE_SCROLL);
                    }
                }, offset);
            }
        });

    }
}

控件

<qnkj.cn.scadacloud.view.EndlessScrollView
    android:id="@+id/esv_project"
    android:layout_width="wrap_content"
    android:layout_height="48px"
    android:layout_centerVertical="true"
    >
</qnkj.cn.scadacloud.view.EndlessScrollView>

调用:

EndlessScrollView sc= (EndlessScrollView) findViewById(R.id.esv_project);
List<String> newsList = new ArrayList<>();
for(int i = 0; i < 50; i++) {
    newsList.add(String.valueOf(i));
}
sc.setData(newsList, 1);

猜你喜欢

转载自blog.csdn.net/Liu_ser/article/details/86093612
今日推荐