Skip to content

Commit

Permalink
Merge branch 'feature/GridViewItemPositionFix' of https://github.com/…
Browse files Browse the repository at this point in the history
…SadieYuCN/android-GridViewWithHeaderAndFooter into SadieYuCN-feature/GridViewItemPositionFix
  • Loading branch information
liaohuqiu committed May 16, 2015
2 parents 8cca8b4 + 5cfb3b8 commit b867785
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 25 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ bin/
gen/
target/
build/
.idea/

# Local configuration file (sdk path, etc)
local.properties
Expand Down
75 changes: 50 additions & 25 deletions src/in/srain/cube/views/GridViewWithHeaderAndFooter.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@
* See {@link GridViewWithHeaderAndFooter#addHeaderView(View, Object, boolean)}
* See {@link GridViewWithHeaderAndFooter#addFooterView(View, Object, boolean)}
*/
public class GridViewWithHeaderAndFooter extends GridView {
public class GridViewWithHeaderAndFooter extends GridView implements android.widget.AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener {

public static boolean DEBUG = false;
private OnItemClickListener mOnItemClickListener;
private OnItemLongClickListener mOnItemLongClickListener;

/**
* A class that represents a fixed view in a list, for example a header at the top
Expand All @@ -62,7 +64,8 @@ private static class FixedViewInfo {
private int mNumColumns = AUTO_FIT;
private View mViewForMeasureRowHeight = null;
private int mRowHeight = -1;
private static final String LOG_TAG = "grid-view-with-header-and-footer";
//log tag can be at most 23 characters
private static final String LOG_TAG = "GridViewHeaderAndFooter";

private ArrayList<FixedViewInfo> mHeaderViewInfos = new ArrayList<FixedViewInfo>();
private ArrayList<FixedViewInfo> mFooterViewInfos = new ArrayList<FixedViewInfo>();
Expand Down Expand Up @@ -310,42 +313,40 @@ public int getHeaderHeight(int row) {
}

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public int getVerticalSpacing(){
public int getVerticalSpacing() {
int value = 0;

try {
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion < Build.VERSION_CODES.JELLY_BEAN){
if (currentapiVersion < Build.VERSION_CODES.JELLY_BEAN) {
Field field = GridView.class.getDeclaredField("mVerticalSpacing");
field.setAccessible(true);
value = field.getInt(this);
} else{
} else {
value = super.getVerticalSpacing();
}

}catch (Exception ex){

} catch (Exception ignore) {
}

return value;
}

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public int getHorizontalSpacing(){
public int getHorizontalSpacing() {
int value = 0;

try {
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion < Build.VERSION_CODES.JELLY_BEAN){
if (currentapiVersion < Build.VERSION_CODES.JELLY_BEAN) {
Field field = GridView.class.getDeclaredField("mHorizontalSpacing");
field.setAccessible(true);
value = field.getInt(this);
} else{
} else {
value = super.getHorizontalSpacing();
}

}catch (Exception ex){

} catch (Exception ignore) {
}

return value;
Expand Down Expand Up @@ -590,16 +591,11 @@ public int getCount() {

@Override
public boolean areAllItemsEnabled() {
if (mAdapter != null) {
return mAreAllFixedViewsSelectable && mAdapter.areAllItemsEnabled();
} else {
return true;
}
return mAdapter == null || mAreAllFixedViewsSelectable && mAdapter.areAllItemsEnabled();
}

private int getAdapterAndPlaceHolderCount() {
final int adapterCount = (int) (Math.ceil(1f * mAdapter.getCount() / mNumColumns) * mNumColumns);
return adapterCount;
return (int) (Math.ceil(1f * mAdapter.getCount() / mNumColumns) * mNumColumns);
}

@Override
Expand Down Expand Up @@ -676,10 +672,7 @@ public long getItemId(int position) {

@Override
public boolean hasStableIds() {
if (mAdapter != null) {
return mAdapter.hasStableIds();
}
return false;
return mAdapter != null && mAdapter.hasStableIds();
}

@Override
Expand Down Expand Up @@ -712,8 +705,7 @@ public View getView(int position, View convertView, ViewGroup parent) {
adapterCount = getAdapterAndPlaceHolderCount();
if (adjPosition < adapterCount) {
if (adjPosition < mAdapter.getCount()) {
View view = mAdapter.getView(adjPosition, convertView, parent);
return view;
return mAdapter.getView(adjPosition, convertView, parent);
} else {
if (convertView == null) {
convertView = new View(parent.getContext());
Expand Down Expand Up @@ -848,4 +840,37 @@ public void notifyDataSetChanged() {
mDataSetObservable.notifyChanged();
}
}

@Override
public void setOnItemClickListener(OnItemClickListener l) {
mOnItemClickListener = l;
super.setOnItemClickListener(this);
}

@Override
public void setOnItemLongClickListener(OnItemLongClickListener listener) {
mOnItemLongClickListener = listener;
super.setOnItemLongClickListener(this);
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (mOnItemClickListener != null) {
int resPos = position - getHeaderViewCount() * getNumColumnsCompatible();
if (resPos >= 0) {
mOnItemClickListener.onItemClick(parent, view, resPos, id);
}
}
}

@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
if (mOnItemLongClickListener != null) {
int resPos = position - getHeaderViewCount() * getNumColumnsCompatible();
if (resPos >= 0) {
mOnItemLongClickListener.onItemLongClick(parent, view, resPos, id);
}
}
return true;
}
}

0 comments on commit b867785

Please sign in to comment.