日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

android滚动条布局横向,Android自定义ViewGroup实现可滚动的横向布局(2)

發(fā)布時間:2023/12/19 Android 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android滚动条布局横向,Android自定义ViewGroup实现可滚动的横向布局(2) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

這里直接代碼:

package com.example.libingyuan.horizontallistview.ScrollViewGroup;

import android.content.Context;

import android.util.AttributeSet;

import android.view.MotionEvent;

import android.view.View;

import android.view.ViewGroup;

import android.widget.Scroller;

/**

* 自定義ViewGroup

* 在橫向布局的基礎(chǔ)上,增加啦滾動效果,但是沒有邊界限制

*/

public class ScrollViewGroup extends ViewGroup {

private Scroller mScroller;

private float mLastMotionX = 0;

public ScrollViewGroup(Context context) {

this(context, null);

}

public ScrollViewGroup(Context context, AttributeSet attrs) {

this(context, attrs, 0);

}

public ScrollViewGroup(Context context, AttributeSet attrs, int defStyleAttr) {

super(context, attrs, defStyleAttr);

init(context);

}

private void init(Context context) {

mScroller = new Scroller(context);

}

@Override

public void computeScroll() {

if (mScroller.computeScrollOffset()) {

scrollTo(mScroller.getCurrX(), mScroller.getCurrY());

postInvalidate();

}

}

@Override

public boolean onTouchEvent(MotionEvent event) {

// TODO Auto-generated method stub

int action = event.getAction();

float x = event.getX();

switch (action) {

case MotionEvent.ACTION_DOWN:

if (!mScroller.isFinished()) {

mScroller.abortAnimation();

}

mLastMotionX = event.getX();

break;

case MotionEvent.ACTION_MOVE:

float delt = mLastMotionX - x;

mLastMotionX = x;

scrollBy((int) delt, 0);

break;

case MotionEvent.ACTION_UP:

invalidate();

break;

default:

break;

}

return true;

}

@Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

//重新設(shè)置寬高

this.setMeasuredDimension(measureWidth(widthMeasureSpec, heightMeasureSpec), measureHeight(widthMeasureSpec, heightMeasureSpec));

}

/**

* 測量寬度

*/

private int measureWidth(int widthMeasureSpec, int heightMeasureSpec) {

// 寬度

int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);

int modeWidth = MeasureSpec.getMode(widthMeasureSpec);

//父控件的寬(wrap_content)

int width = 0;

int childCount = getChildCount();

//重新測量子view的寬度,以及最大高度

for (int i = 0; i < childCount; i++) {

View child = getChildAt(i);

measureChild(child, widthMeasureSpec, heightMeasureSpec);

MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();

int childWidth = child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;

width += childWidth;

}

return modeWidth == MeasureSpec.EXACTLY ? sizeWidth : width;

}

/**

* 測量高度

*/

private int measureHeight(int widthMeasureSpec, int heightMeasureSpec) {

//高度

int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);

int modeHeight = MeasureSpec.getMode(heightMeasureSpec);

//父控件的高(wrap_content)

int height = 0;

int childCount = getChildCount();

//重新測量子view的寬度,以及最大高度

for (int i = 0; i < childCount; i++) {

View child = getChildAt(i);

measureChild(child, widthMeasureSpec, heightMeasureSpec);

MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();

int childHeight = child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin;

height += childHeight;

}

height = height / childCount;

return modeHeight == MeasureSpec.EXACTLY ? sizeHeight : height;

}

@Override

protected void onLayout(boolean changed, int l, int t, int r, int b) {

int childLeft = 0;

int childWidth;

int height = getHeight();

int childCount = getChildCount();

for (int i = 0; i < childCount; i++) {

View child = getChildAt(i);

MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();

childWidth = child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;

child.layout(childLeft, 0, childLeft + childWidth, height);

childLeft += childWidth;

}

}

@Override

public LayoutParams generateLayoutParams(AttributeSet attrs) {

return new MarginLayoutParams(getContext(), attrs);

}

}

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎

總結(jié)

以上是生活随笔為你收集整理的android滚动条布局横向,Android自定义ViewGroup实现可滚动的横向布局(2)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。