編輯:關於 Android 程式設計
最近在學自訂 ViewGroup,最終目標很明確:做一個類似可以滾動的 ListView,用 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); } }
第一是 onMeasure 中一定要呼叫 measureChild,這句話漏掉,子 View 就不會被測量,畫面直接一片空白——這是新手最常踩的坑。
第二是寬度累加時要把 MarginLayoutParams 的 leftMargin 與 rightMargin 一起算進去,而 generateLayoutParams 回傳 MarginLayoutParams 正是為了讓 margin 屬性生效。
第三是高度取平均(height / childCount),這是個簡化處理,橫向列表裡各項高度通常一致,但正式專案中若允許不等高,應改為取最大值。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持本站。
從零開始學 Android:數據存儲(四)SQLite 數據庫完整實戰
前面幾章我們分別學習了 SharedPreferences 屬性文件存儲、內部存儲與外部存儲。這一章來看看 Android 內建的輕量級數據庫——SQLite。 一、先
Android 屬性動畫完整指南:ObjectAnimator、ValueAnimator 與 AnimatorSet
Android 的 Property Animation 以改變物件屬性為核心,適合處理顏色、3D 旋轉、位移與透明度等更複雜的動畫需求。 核心概念 Duration:
自製 ImageLoader:從 LruCache 到非同步圖片載入的完整實作
先看幾張效果圖: 在載入大量圖片時,我們採用後進先出策略(滑到哪裡就先載入哪裡的圖片),既節省記憶體,使用者體驗也更好。下面就來打造自己的 ImageLoader。
Android 兩種做法實現長按返回鍵退出應用
Android 長按返回鍵退出 背景 常見的退出做法是在短時間內連按兩次返回鍵,第一次點擊還會給出提示,這類資料網上很多。這裡要記錄的是另一種思路:長按返回鍵退出。