欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android自定義ViewGroup橫向布局(1)

 更新時(shí)間:2016年12月20日 13:38:59   作者:封魔之殤  
這篇文章主要介紹了Android自定義ViewGroup橫向布局,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

最近學(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í)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 分析Android Activity的啟動(dòng)過程

    分析Android Activity的啟動(dòng)過程

    這篇文章主要介紹了分析Android Activity的啟動(dòng)過程的相關(guān)資料,需要的朋友可以參考下
    2017-07-07
  • Android編寫簡單的聊天室應(yīng)用

    Android編寫簡單的聊天室應(yīng)用

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)簡單聊天室的相關(guān)資料,具有發(fā)送表情,更改頭像等功能
    2016-06-06
  • Gradle的緩存路徑修改的四種方法(小結(jié))

    Gradle的緩存路徑修改的四種方法(小結(jié))

    這篇文章主要介紹了Gradle的緩存路徑修改的四種方法(小結(jié)),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • Android多國語言轉(zhuǎn)換Excel及Excel轉(zhuǎn)換為string詳解

    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)方法

    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)通訊錄字母索引(仿微信通訊錄)

    本文主要介紹了Android自定義View實(shí)現(xiàn)通訊錄字母索引(仿微信通訊錄)的實(shí)現(xiàn)步驟與方法,具有很好的參考價(jià)值,下面跟著小編一起來看下吧
    2016-12-12
  • android studio無法添加 bmob sdk依賴問題及解決方法

    android studio無法添加 bmob sdk依賴問題及解決方法

    這篇文章主要介紹了android studio無法添加 bmob sdk依賴,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-05-05
  • Android編程之軟鍵盤的隱藏顯示實(shí)例詳解

    Android編程之軟鍵盤的隱藏顯示實(shí)例詳解

    這篇文章主要介紹了Android編程之軟鍵盤的隱藏顯示,結(jié)合實(shí)例形式詳細(xì)分析了Android編程中軟鍵盤的使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-12-12
  • Android SDK Manager國內(nèi)無法更新的解決方案

    Android SDK Manager國內(nèi)無法更新的解決方案

    本文主要介紹Android SDK Manager國內(nèi)無法更新的解決方案,這里提供了解決方法,及簡單說明實(shí)現(xiàn)流程,有興趣的小伙伴可以參考下
    2016-09-09
  • Kotlin新手基礎(chǔ)學(xué)習(xí)之Elvis操作符

    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

最新評論