Android自定義ViewGroup實(shí)現(xiàn)可滾動的橫向布局(2)
上一篇文章自定義viewgroup(1)地址:http://chabaoo.cn/article/100608.htm
這里直接代碼:
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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- android開發(fā)之橫向滾動/豎向滾動的ListView(固定列頭)
- Android程序開發(fā)之ListView實(shí)現(xiàn)橫向滾動(帶表頭與固定列)
- 詳解Android使GridView橫向水平滾動的實(shí)現(xiàn)方式
- Android使用GridView實(shí)現(xiàn)橫向滾動效果
- Android開發(fā)實(shí)現(xiàn)橫向列表GridView橫向滾動的方法【附源碼下載】
- Android GridView實(shí)現(xiàn)橫向列表水平滾動
- Android實(shí)現(xiàn)自定義的彈幕效果
- 實(shí)例解析如何在Android應(yīng)用中實(shí)現(xiàn)彈幕動畫效果
- Android 實(shí)現(xiàn)仿網(wǎng)絡(luò)直播彈幕功能詳解及實(shí)例
- Android實(shí)現(xiàn)橫向無限循環(huán)滾動的單行彈幕效果
相關(guān)文章
Android基于widget組件實(shí)現(xiàn)物體移動/控件拖動功能示例
這篇文章主要介紹了Android基于widget組件實(shí)現(xiàn)物體移動/控件拖動功能,結(jié)合實(shí)例形式分析了widget組件在桌面應(yīng)用中的事件響應(yīng)與屬性動態(tài)操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-10-10
Android實(shí)現(xiàn)長按圓環(huán)動畫View效果的思路代碼
這篇文章主要介紹了Android實(shí)現(xiàn)長按圓環(huán)動畫View效果,本文給大家分享實(shí)現(xiàn)思路,通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09
Kotlin設(shè)計(jì)模式之委托模式使用方法詳解
Kotlin提供了兩個本機(jī)功能來實(shí)現(xiàn)委托模式,第一個是接口委托(例如策略模式),另一種是屬性委托,它專注于類成員/屬性(例如延遲加載、observable等),它們共同提供了一組豐富而簡潔的功能,通過本博客,您將了解在什么情況下使用此模式2023-09-09
TextView實(shí)現(xiàn)圖文混合編排的方法
這篇文章主要為大家詳細(xì)介紹了TextView實(shí)現(xiàn)圖文混合編排的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08
Android基于ImageView繪制的開關(guān)按鈕效果示例
這篇文章主要介紹了Android基于ImageView繪制的開關(guān)按鈕效果,結(jié)合實(shí)例形式分析了Android使用ImageView進(jìn)行按鈕繪制的界面布局、功能實(shí)現(xiàn)及相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-03-03
Android 跨進(jìn)程通Messenger(簡單易懂)
這篇文章主要介紹了Android Messenger跨進(jìn)程通的相關(guān)資料,非常簡單容易理解,對android messenger 進(jìn)程通訊的相關(guān)知識感興趣的朋友一起學(xué)習(xí)吧2016-08-08
Android開發(fā)之多線程中實(shí)現(xiàn)利用自定義控件繪制小球并完成小球自動下落功能實(shí)例
這篇文章主要介紹了Android開發(fā)之多線程中實(shí)現(xiàn)利用自定義控件繪制小球并完成小球自動下落功能的方法,涉及Android多線程編程及圖形繪制相關(guān)技巧,需要的朋友可以參考下2015-12-12

