編輯:關於Android編程
最近學習自定義viewgroup,我的目標是做一個可以很想滾動的listview,使用adapter填充數據,並且使用adapter.notifyDataSetChanged()更新數據。
不過一口吃不成一個胖子(我吃成這樣可是好幾年的積累下來的~~~~),我們一步一步來,這篇筆記首先寫一個橫向的布局。
代碼:
package com.example.libingyuan.horizontallistview.ScrollViewGroup;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
/**
* 自定義ViewGroup
* 很簡單的橫向布局,把所有的子View都橫著排列起來,不可滾動
*/
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) {
//重新設置寬高
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的個數
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的個數
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);
}
}
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持本站。
Android 自定義View高級特效,神奇的貝塞爾曲線
效果圖效果圖中我們實現了一個簡單的隨手指滑動的二階貝塞爾曲線,還有一個復雜點的,穿越所有已知點的貝塞爾曲線。學會使用貝塞爾曲線後可以實現例如QQ紅點滑動刪除啦,360動態
Android 數據存儲——SharedPreference
作為一個完成的應用程序,數據存儲操作是必不可少的,因此,Android系統提供了四種數據儲存方式,分別是:SharedPreference、File、SQLite以及Co
android開發教程之wifi開發示例
1、 WIFI網卡的狀態WIFI網卡的狀態信息都以整型變量的形式存放在 android.net.wifi.WifiManager 類中,有以下狀態:WIFI_STATE_
Android Intent機制和常見用法
Activity Android中,Activity是所有程序的根本,所有程序的流程都運行在Activity之中,Activity具有自己的生命周期(見http://