Android自定義ViewGroup橫向布局(1)
最近學(xué)習(xí)自定義viewgroup,我的目標(biāo)是做一個(gè)可以很想滾動(dòng)的listview,使用adapter填充數(shù)據(jù),并且使用adapter.notifyDataSetChanged()更新數(shù)據(jù)。
不過一口吃不成一個(gè)胖子(我吃成這樣可是好幾年的積累下來的~~~~),我們一步一步來,這篇筆記首先寫一個(gè)橫向的布局。
代碼:
package com.example.libingyuan.horizontallistview.ScrollViewGroup;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
/**
* 自定義ViewGroup
* 很簡單的橫向布局,把所有的子View都橫著排列起來,不可滾動(dòng)
*/
public class ScrollViewGroup extends ViewGroup{
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);
}
@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;
//子View的個(gè)數(shù)
int childCount = getChildCount();
//重新測量子view的寬度,以及最大高度
for (int i = 0; i < childCount; i++) {
//獲取子View
View child = getChildAt(i);
//測量子View,無論什么模式,這句必須有否則界面不顯示子View(一片空白)
measureChild(child, widthMeasureSpec, heightMeasureSpec);
//得到子View的邊距
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;
//子View的個(gè)數(shù)
int childCount = getChildCount();
//重新測量子view的寬度,以及最大高度
for (int i = 0; i < childCount; i++) {
//得到子View
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;//子View左邊的距離
int childWidth;//子View的寬度
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動(dòng)畫效果之自定義ViewGroup添加布局動(dòng)畫(五)
- Android自定義ViewGroup之實(shí)現(xiàn)FlowLayout流式布局
- Android編程重寫ViewGroup實(shí)現(xiàn)卡片布局的方法
- Android App開發(fā)中自定義View和ViewGroup的實(shí)例教程
- Android自定義ViewGroup的實(shí)現(xiàn)方法
- Android自定義ViewGroup打造各種風(fēng)格的SlidingMenu
- 從源碼解析Android中View的容器ViewGroup
- Android自定義ViewGroup實(shí)現(xiàn)標(biāo)簽浮動(dòng)效果
- Android應(yīng)用開發(fā)中自定義ViewGroup的究極攻略
- Android自定義ViewGroup實(shí)現(xiàn)帶箭頭的圓角矩形菜單
相關(guān)文章
Android多國語言轉(zhuǎn)換Excel及Excel轉(zhuǎn)換為string詳解
這篇文章主要給大家介紹了關(guān)于Android多國語言轉(zhuǎn)換Excel以及Excel轉(zhuǎn)換為string的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧2019-01-01
Android?App應(yīng)用退到后臺(tái)顯示通知的實(shí)現(xiàn)方法
當(dāng)用戶收到app發(fā)過來的消息時(shí),如果app沒有在前臺(tái)打開,需要提醒用戶有新的消息,所以這篇文章主要給大家介紹了關(guān)于Android?App應(yīng)用退到后臺(tái)顯示通知的實(shí)現(xiàn)方法,需要的朋友可以參考下2022-01-01
Android自定義View實(shí)現(xiàn)通訊錄字母索引(仿微信通訊錄)
本文主要介紹了Android自定義View實(shí)現(xiàn)通訊錄字母索引(仿微信通訊錄)的實(shí)現(xiàn)步驟與方法,具有很好的參考價(jià)值,下面跟著小編一起來看下吧2016-12-12
android studio無法添加 bmob sdk依賴問題及解決方法
這篇文章主要介紹了android studio無法添加 bmob sdk依賴,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05
Android SDK Manager國內(nèi)無法更新的解決方案
本文主要介紹Android SDK Manager國內(nèi)無法更新的解決方案,這里提供了解決方法,及簡單說明實(shí)現(xiàn)流程,有興趣的小伙伴可以參考下2016-09-09
Kotlin新手基礎(chǔ)學(xué)習(xí)之Elvis操作符
Kotlin 是一種在 Java 虛擬機(jī)上運(yùn)行的靜態(tài)類型編程語言,被稱之為 Android 世界的Swift,由 JetBrains 設(shè)計(jì)開發(fā)并開源,下面這篇文章主要給大家介紹了關(guān)于Kotlin新手基礎(chǔ)學(xué)習(xí)之Elvis操作符的相關(guān)資料,需要的朋友可以參考下。2017-12-12

