HeaderGridView——可以新增HeaderView的GridView,已修復HeaderView偏移的BUG
- 基於Google提供的HeaderGridView,修復其HeaderView偏移的bug
- Refer
一、基於Google提供的HeaderGridView,修復其HeaderView偏移的bug
最近在做一個需求用到了PullToRefreshGridView,其中的GridView也就是普通的GridView,並沒有像ListView中那樣的addHeaderView()或者addFooterView()方法,但是實際開發中又會碰到這樣的需求。
於是在網上搜到HeaderGridView這個Widget,而且是谷歌官方提供的,整合執行後發現HeaderView會發生偏移,而程式碼中並沒有設定Gravity。
HeaderGridView bug可檢視Google的issues:
最後找到android-GridViewWithHeaderAndFooter,該專案解決了HeaderView偏移的bug,於是檢視其原始碼研究其如何解決HeaderView偏移的bug,發現其在FullWidthFixedViewLayout類中重寫onLayout方法,程式碼如下:
/**
* @author zhangbiaojiang
* @功能 解決HeaderView偏移的bug
* @時間 2017/06/09
*/
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
int realLeft = HeaderGridView.this.getPaddingLeft() + getPaddingLeft();
// Try to make where it should be, from left, full width
if (realLeft != left) {
offsetLeftAndRight(realLeft - left);
}
super .onLayout(changed, left, top, right, bottom);
}
基於Google提供的HeaderGridView,修復其HeaderView偏移的bug。修復後的HeaderGridView原始碼如下:
import android.content.Context;
import android.database.DataSetObservable;
import android.database.DataSetObserver;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.ListAdapter;
import android.widget.WrapperListAdapter;
import java.util.ArrayList;
/**
* A {@link GridView} that supports adding header rows in a
* See {@link HeaderGridView#addHeaderView(View, Object, boolean)}
*/
public class HeaderGridView extends GridView {
private static final String TAG = "HeaderGridView";
/**
* A class that represents a fixed view in a list, for example a header at the top
* or a footer at the bottom.
*/
private static class FixedViewInfo {
/** The view to add to the grid */
public View view;
public ViewGroup viewContainer;
/** The data backing the view. This is returned from {@link ListAdapter#getItem(int)}. */
public Object data;
/** <code>true</code> if the fixed view should be selectable in the grid */
public boolean isSelectable;
}
private ArrayList<FixedViewInfo> mHeaderViewInfos = new ArrayList<FixedViewInfo>();
private void initHeaderGridView() {
super.setClipChildren(false);
}
public HeaderGridView(Context context) {
super(context);
initHeaderGridView();
}
public HeaderGridView(Context context, AttributeSet attrs) {
super(context, attrs);
initHeaderGridView();
}
public HeaderGridView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initHeaderGridView();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
ListAdapter adapter = getAdapter();
if (adapter != null && adapter instanceof HeaderViewGridAdapter) {
((HeaderViewGridAdapter) adapter).setNumColumns(getNumColumns());
}
}
@Override
public void setClipChildren(boolean clipChildren) {
// Ignore, since the header rows depend on not being clipped
}
/**
* Add a fixed view to appear at the top of the grid. If addHeaderView is
* called more than once, the views will appear in the order they were
* added. Views added using this call can take focus if they want.
* <p>
* NOTE: Call this before calling setAdapter. This is so HeaderGridView can wrap
* the supplied cursor with one that will also account for header views.
*
* @param v The view to add.
* @param data Data to associate with this view
* @param isSelectable whether the item is selectable
*/
public void addHeaderView(View v, Object data, boolean isSelectable) {
ListAdapter adapter = getAdapter();
if (adapter != null && ! (adapter instanceof HeaderViewGridAdapter)) {
throw new IllegalStateException(
"Cannot add header view to grid -- setAdapter has already been called.");
}
FixedViewInfo info = new FixedViewInfo();
FrameLayout fl = new FullWidthFixedViewLayout(getContext());
fl.addView(v);
info.view = v;
info.viewContainer = fl;
info.data = data;
info.isSelectable = isSelectable;
mHeaderViewInfos.add(info);
// in the case of re-adding a header view, or adding one later on,
// we need to notify the observer
if (adapter != null) {
((HeaderViewGridAdapter) adapter).notifyDataSetChanged();
}
}
/**
* Add a fixed view to appear at the top of the grid. If addHeaderView is
* called more than once, the views will appear in the order they were
* added. Views added using this call can take focus if they want.
* <p>
* NOTE: Call this before calling setAdapter. This is so HeaderGridView can wrap
* the supplied cursor with one that will also account for header views.
*
* @param v The view to add.
*/
public void addHeaderView(View v) {
addHeaderView(v, null, true);
}
public int getHeaderViewCount() {
return mHeaderViewInfos.size();
}
/**
* Removes a previously-added header view.
*
* @param v The view to remove
* @return true if the view was removed, false if the view was not a header
* view
*/
public boolean removeHeaderView(View v) {
if (mHeaderViewInfos.size() > 0) {
boolean result = false;
ListAdapter adapter = getAdapter();
if (adapter != null && ((HeaderViewGridAdapter) adapter).removeHeader(v)) {
result = true;
}
removeFixedViewInfo(v, mHeaderViewInfos);
return result;
}
return false;
}
private void removeFixedViewInfo(View v, ArrayList<FixedViewInfo> where) {
int len = where.size();
for (int i = 0; i < len; ++i) {
FixedViewInfo info = where.get(i);
if (info.view == v) {
where.remove(i);
break;
}
}
}
@Override
public void setAdapter(ListAdapter adapter) {
if (mHeaderViewInfos.size() > 0) {
HeaderViewGridAdapter hadapter = new HeaderViewGridAdapter(mHeaderViewInfos, adapter);
int numColumns = getNumColumns();
if (numColumns > 1) {
hadapter.setNumColumns(numColumns);
}
super.setAdapter(hadapter);
} else {
super.setAdapter(adapter);
}
}
private class FullWidthFixedViewLayout extends FrameLayout {
public FullWidthFixedViewLayout(Context context) {
super(context);
}
/**
* @author zhangbiaojiang
* @功能 解決HeaderView偏移的bug
* @時間 2017/06/09
*/
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
int realLeft = HeaderGridView.this.getPaddingLeft() + getPaddingLeft();
// Try to make where it should be, from left, full width
if (realLeft != left) {
offsetLeftAndRight(realLeft - left);
}
super.onLayout(changed, left, top, right, bottom);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int targetWidth = HeaderGridView.this.getMeasuredWidth()
- HeaderGridView.this.getPaddingLeft()
- HeaderGridView.this.getPaddingRight();
widthMeasureSpec = MeasureSpec.makeMeasureSpec(targetWidth,
MeasureSpec.getMode(widthMeasureSpec));
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
/**
* ListAdapter used when a HeaderGridView has header views. This ListAdapter
* wraps another one and also keeps track of the header views and their
* associated data objects.
*<p>This is intended as a base class; you will probably not need to
* use this class directly in your own code.
*/
private static class HeaderViewGridAdapter implements WrapperListAdapter, Filterable {
// This is used to notify the container of updates relating to number of columns
// or headers changing, which changes the number of placeholders needed
private final DataSetObservable mDataSetObservable = new DataSetObservable();
private final ListAdapter mAdapter;
private int mNumColumns = 1;
// This ArrayList is assumed to NOT be null.
ArrayList<FixedViewInfo> mHeaderViewInfos;
boolean mAreAllFixedViewsSelectable;
private final boolean mIsFilterable;
public HeaderViewGridAdapter(ArrayList<FixedViewInfo> headerViewInfos, ListAdapter adapter) {
mAdapter = adapter;
mIsFilterable = adapter instanceof Filterable;
if (headerViewInfos == null) {
throw new IllegalArgumentException("headerViewInfos cannot be null");
}
mHeaderViewInfos = headerViewInfos;
mAreAllFixedViewsSelectable = areAllListInfosSelectable(mHeaderViewInfos);
}
public int getHeadersCount() {
return mHeaderViewInfos.size();
}
@Override
public boolean isEmpty() {
return (mAdapter == null || mAdapter.isEmpty()) && getHeadersCount() == 0;
}
public void setNumColumns(int numColumns) {
if (numColumns < 1) {
throw new IllegalArgumentException("Number of columns must be 1 or more");
}
if (mNumColumns != numColumns) {
mNumColumns = numColumns;
notifyDataSetChanged();
}
}
private boolean areAllListInfosSelectable(ArrayList<FixedViewInfo> infos) {
if (infos != null) {
for (FixedViewInfo info : infos) {
if (!info.isSelectable) {
return false;
}
}
}
return true;
}
public boolean removeHeader(View v) {
for (int i = 0; i < mHeaderViewInfos.size(); i++) {
FixedViewInfo info = mHeaderViewInfos.get(i);
if (info.view == v) {
mHeaderViewInfos.remove(i);
mAreAllFixedViewsSelectable = areAllListInfosSelectable(mHeaderViewInfos);
mDataSetObservable.notifyChanged();
return true;
}
}
return false;
}
@Override
public int getCount() {
if (mAdapter != null) {
return getHeadersCount() * mNumColumns + mAdapter.getCount();
} else {
return getHeadersCount() * mNumColumns;
}
}
@Override
public boolean areAllItemsEnabled() {
if (mAdapter != null) {
return mAreAllFixedViewsSelectable && mAdapter.areAllItemsEnabled();
} else {
return true;
}
}
@Override
public boolean isEnabled(int position) {
// Header (negative positions will throw an ArrayIndexOutOfBoundsException)
int numHeadersAndPlaceholders = getHeadersCount() * mNumColumns;
if (position < numHeadersAndPlaceholders) {
return (position % mNumColumns == 0)
&& mHeaderViewInfos.get(position / mNumColumns).isSelectable;
}
// Adapter
final int adjPosition = position - numHeadersAndPlaceholders;
int adapterCount = 0;
if (mAdapter != null) {
adapterCount = mAdapter.getCount();
if (adjPosition < adapterCount) {
return mAdapter.isEnabled(adjPosition);
}
}
throw new ArrayIndexOutOfBoundsException(position);
}
@Override
public Object getItem(int position) {
// Header (negative positions will throw an ArrayIndexOutOfBoundsException)
int numHeadersAndPlaceholders = getHeadersCount() * mNumColumns;
if (position < numHeadersAndPlaceholders) {
if (position % mNumColumns == 0) {
return mHeaderViewInfos.get(position / mNumColumns).data;
}
return null;
}
// Adapter
final int adjPosition = position - numHeadersAndPlaceholders;
int adapterCount = 0;
if (mAdapter != null) {
adapterCount = mAdapter.getCount();
if (adjPosition < adapterCount) {
return mAdapter.getItem(adjPosition);
}
}
throw new ArrayIndexOutOfBoundsException(position);
}
@Override
public long getItemId(int position) {
int numHeadersAndPlaceholders = getHeadersCount() * mNumColumns;
if (mAdapter != null && position >= numHeadersAndPlaceholders) {
int adjPosition = position - numHeadersAndPlaceholders;
int adapterCount = mAdapter.getCount();
if (adjPosition < adapterCount) {
return mAdapter.getItemId(adjPosition);
}
}
return -1;
}
@Override
public boolean hasStableIds() {
if (mAdapter != null) {
return mAdapter.hasStableIds();
}
return false;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Header (negative positions will throw an ArrayIndexOutOfBoundsException)
int numHeadersAndPlaceholders = getHeadersCount() * mNumColumns ;
if (position < numHeadersAndPlaceholders) {
View headerViewContainer = mHeaderViewInfos
.get(position / mNumColumns).viewContainer;
if (position % mNumColumns == 0) {
return headerViewContainer;
} else {
if (convertView == null) {
convertView = new View(parent.getContext());
}
// We need to do this because GridView uses the height of the last item
// in a row to determine the height for the entire row.
convertView.setVisibility(View.INVISIBLE);
convertView.setMinimumHeight(headerViewContainer.getHeight());
return convertView;
}
}
// Adapter
final int adjPosition = position - numHeadersAndPlaceholders;
int adapterCount = 0;
if (mAdapter != null) {
adapterCount = mAdapter.getCount();
if (adjPosition < adapterCount) {
return mAdapter.getView(adjPosition, convertView, parent);
}
}
throw new ArrayIndexOutOfBoundsException(position);
}
@Override
public int getItemViewType(int position) {
int numHeadersAndPlaceholders = getHeadersCount() * mNumColumns;
if (position < numHeadersAndPlaceholders && (position % mNumColumns != 0)) {
// Placeholders get the last view type number
return mAdapter != null ? mAdapter.getViewTypeCount() : 1;
}
if (mAdapter != null && position >= numHeadersAndPlaceholders) {
int adjPosition = position - numHeadersAndPlaceholders;
int adapterCount = mAdapter.getCount();
if (adjPosition < adapterCount) {
return mAdapter.getItemViewType(adjPosition);
}
}
return AdapterView.ITEM_VIEW_TYPE_HEADER_OR_FOOTER;
}
@Override
public int getViewTypeCount() {
if (mAdapter != null) {
return mAdapter.getViewTypeCount() + 1;
}
return 2;
}
@Override
public void registerDataSetObserver(DataSetObserver observer) {
mDataSetObservable.registerObserver(observer);
if (mAdapter != null) {
mAdapter.registerDataSetObserver(observer);
}
}
@Override
public void unregisterDataSetObserver(DataSetObserver observer) {
mDataSetObservable.unregisterObserver(observer);
if (mAdapter != null) {
mAdapter.unregisterDataSetObserver(observer);
}
}
@Override
public Filter getFilter() {
if (mIsFilterable) {
return ((Filterable) mAdapter).getFilter();
}
return null;
}
@Override
public ListAdapter getWrappedAdapter() {
return mAdapter;
}
public void notifyDataSetChanged() {
mDataSetObservable.notifyChanged();
}
}
}
二、Refer
相關推薦
HeaderGridView——可以新增HeaderView的GridView,已修復HeaderView偏移的BUG
基於Google提供的HeaderGridView,修復其HeaderView偏移的bug Refer 一、基於Google提供的HeaderGridView,修復其HeaderView偏移的bug 最近在做一個需求用到了PullToRefre
SQL Server基礎(一) 工程上,我們用VS 新建一個數據庫,還是新增或讀取已有的資料庫呢(SSMS 可管理資料庫)?
一、VS 建立資料庫 1、轉 https://jingyan.baidu.com/album/9f63fb91893ac3c8410f0e58.html?picindex=2 2、VS建立資料庫後,新增表時,要點選"更新"按鈕。資料表才會成功建立。 轉https://blog.
給佈局,控制元件新增陰影,包裹它設定陰影顏色,長度,偏移即可
public class ShadowLayout extends FrameLayout { private int mShadowColor; private float mShadowRadius; private float mCornerRadius; p
IOT-15898 scene選擇裝置動作,在編輯頁刪除裝置,再進入新增裝置動作頁面,已刪裝置依然被勾選
解決方案遍歷一遍所有本地裝置將所有分組和子專案都變成未選中裝態 ArrayList<Device> list = DeviceMange.getInstance().getAllDevices(); for (Device device : list){
大眾點評selfxss結合兩個csrf變廢為寶(已修復,故公開,不涉及真實引數)
大眾點評selfxss結合兩個csrf變廢為寶 漏洞不值錢,但還是蠻好玩的 漏洞資訊 型別:儲存型xss 場景:收藏商戶後,去已收藏的商戶列表可以給指定商戶新增tag(與下文html標籤區別) 漏洞限制: 1.selfxss,即需要使用者自己登入賬號,然後自己輸入payload。。。繞過思路當然
2048(已更新,所有bug均已修復)【更美觀的外形】
#include<stdio.h> #include<stdlib.h> #include<time.h> #include<windows.h> #include<conio.h> int i,j,M[100][
自定義RecyclerView新增HeaderView,新增FooterView,實現滑動到底部,載入更多
顯示效果圖 PS 接觸過RecyclerView的應該會有個感覺,那就是我不想在使用ListView和GridView了,因為這個控制元件是可以實現那兩個控制元件(ListView和GridView)所實現的幾乎所有吧,哈哈我也沒用他們倆幹過多少的變
C#中的擴充套件方法(向已有類新增方法,但無需建立新的派生型別)
擴充套件方法使你能夠向現有型別“新增”方法,而無需建立新的派生型別、重新編譯或以其他方式修改原始型別。 擴充套件方法是一種特殊的靜態方法,但可以像擴充套件型別上的例項方法一樣進行呼叫。 以上是msdn官網對擴充套件方法的描述,現在我通過一個情景例子來對此進行闡釋。假設一個控制檯程式class Progr
Win8.1更新之後沒法啟動,怎樣修復?
修復 img 問題 b- 今天 mar u盤啟動 alt con 1、問題 今天開筆記本的時候,發現電腦沒法啟動。屏幕顯示“Recovery Your PC needs to be repaired...”。詳細內容見下圖: 2、解決的方法 2.1 用
雲計算之路-阿裏雲上:彈性伸縮無服務器可彈,已有服務器無兵可援
cit spec -h ebs request sca 天上 chan binding 活動起因: A scheduled task executes scaling rule "eBsJ2veNkwJkcGinmICVH1Q", changing the Total
iOS9.0.1發布,修復多項bug
wsb gty tmm aid baidu sap ios axu ios9 SQLServer2008SP3%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87%E7%89%88%E5%AE%98%E6%96%B9%E4%B8%8B%E8%BD%BD
尚吉剛-讀王堅《在線》有感:在線與否,已成新老世界分割線
www. 光有 cli yun 展會 英特爾 lan 生產 美好 王堅博士在《在線》書中,明確地提出,“大數據”這個名字叫錯了,他並沒有反應出數據最本質的東西,光有大對於數據是不夠的,王堅舉的例子是歐洲核子研究中心(CERN),它通過粒子實驗得到了世界上最大的數據庫,但這
.net 新手上路,已瘋->_<-
for math 面積 面積並 mat col spa 判斷 align 今天.net 上機實驗,已哭死在電腦前 把一些能簡單的全都簡單處理了。 1. 編寫一個控制臺應用程序,輸入三角形或者長方形邊長,計算其周長和面積並輸出。 代碼如下: using Syste
26、有一行電文,已按下面規律譯成密碼即第一個字母變成第26個字母,第I個字母變成第(26-i+1)
targe 第一個 span clas spa -i 擴展 title 知識 擴展知識 參考 請輸入一個數字,把它顯示為對應的字母,比如輸入65,顯示A,輸入97,顯示a 26、有一行電文,已按下面規律譯成密碼即第一個字母變成第26個字母,第I個字母變成第(26-i+1
(轉)APP測試教福利:Appium 國內下載地址(百度雲盤,已更新至 1.3.4.1)
monkey 語言 1.3 更新 follow zha install ast IT 鏈接是Appium相關安裝包下載地址(exe&dmg格式),如需自取:) 最新更新的是: appium-1.3.4.dmg& AppiumForWindows-1.3.4.
還原或重置Surface——官方,已實驗可行
bsp 恢復 驅動程序 reset nbsp gpo 分享 最新 body 如果 Surface 的運行速度或穩定性不如以往,請嘗試下列恢復選項之一。下表能幫助你判斷要使用哪個選項。 問題 嘗試此操作 你的 Surface 運行不佳。你
ogg新增表,進行同步
tar 步驟 進行 target opera 數據 AR operation 同步 根據需求將生產庫中PROCESS_LOG表通過ogg同步到測試庫中:操作步驟: 1. 關閉測試庫復制進程 2. 生產庫上查詢當前的scn,並根據scn用數據泵導出PROCESS_LOG表
Oracle安裝時,已有oracle用戶,將用戶添加到oinstall和dba用戶組
添加到oinstall和dba用戶組usermod -g oinstall -G dba -d /home/Oracle Oracle-g為指定用戶的主要組為oinstall組-G為指定用戶的次要組為dba組-d為指定用戶的主目錄語句詳細定義為將Oracle用戶的主要組指定為oinstall次要組指定為dba
求數列的的增幅,已知起始列和結束列,中間階梯數
圖片 分享圖片 公式 結束 com bubuko http nbsp 技術分享 求數列的的增幅,已知起始列和結束列,中間階梯數 已知 n1=2 n2=100 階梯=4 上面4個空列 每列增幅多少,正好填到100? 公式 (n2-n1)/(階梯+1) 為什
Java程序員從京東、阿裏、攜程面試回來,已成功拿到京東offer
Java 程序員 互聯網 後端 編程語言 阿裏巴巴(一面)阿裏找了一個前輩內推的,準確來說應該是直推,是他幫我直接錄的簡歷,他本科進的阿裏螞蟻金服,厲害吧?是真的佩服。第一次在官網上填資料,想想馬雲有多出名,想想螞蟻金服這樣的頂級技術,有些興奮,有些期待。錄完簡歷後等待簡歷評估,原來,找內