- 基于Google提供的HeaderGridView,修复其HeaderView偏移的bug
- Refer
一、基于Google提供的HeaderGridView,修复其HeaderView偏移的bug
最近在做一个需求用到了PullToRefreshGridView,其中的GridView也就是普通的GridView,并没有像ListView中那样的addHeaderView()或者addFooterView()方法,但是实际开发中又会碰到这样的需求。
于是在网上搜到HeaderGridView这个Widget,而且是谷歌官方提供的,集成运行后发现HeaderView会发生偏移,而代码中并没有设置Gravity。
HeaderGridView bug可查看Google的issues:
https://code.google.com/p/android/issues/detail?id=74520
最后找到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