From ea43f20af1246f359c835ee5b77871f7f0ee688c Mon Sep 17 00:00:00 2001 From: SadieYu Date: Wed, 13 May 2015 12:17:51 +0800 Subject: [PATCH 1/2] fix GridViewItem position issue after adding HeaderView. --- .gitignore | 1 + .../views/GridViewWithHeaderAndFooter.java | 95 ++++++++++++++----- 2 files changed, 71 insertions(+), 25 deletions(-) diff --git a/.gitignore b/.gitignore index 0612670..5c4c420 100644 --- a/.gitignore +++ b/.gitignore @@ -18,6 +18,7 @@ bin/ gen/ target/ build/ +.idea/ # Local configuration file (sdk path, etc) local.properties diff --git a/src/in/srain/cube/views/GridViewWithHeaderAndFooter.java b/src/in/srain/cube/views/GridViewWithHeaderAndFooter.java index f3ad46c..8e1d437 100644 --- a/src/in/srain/cube/views/GridViewWithHeaderAndFooter.java +++ b/src/in/srain/cube/views/GridViewWithHeaderAndFooter.java @@ -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 @@ -309,42 +311,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; @@ -574,16 +574,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 @@ -660,10 +655,7 @@ public long getItemId(int position) { @Override public boolean hasStableIds() { - if (mAdapter != null) { - return mAdapter.hasStableIds(); - } - return false; + return mAdapter != null && mAdapter.hasStableIds(); } @Override @@ -696,8 +688,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()); @@ -832,4 +823,58 @@ 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() * getNumColumnsCompat(); + 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() * getNumColumnsCompat(); + if (resPos >= 0) { + mOnItemLongClickListener.onItemLongClick(parent, view, resPos, id); + } + } + return true; + } + + private int getNumColumnsCompat() { + if (Build.VERSION.SDK_INT >= 11) { + return getNumColumnsCompatFrom11(); + + } else { + try { + Field numColumns = getClass().getSuperclass().getDeclaredField("mNumColumns"); + numColumns.setAccessible(true); + return numColumns.getInt(this); + } catch (Exception e) { + return 1; + } + } + } + + @TargetApi(Build.VERSION_CODES.HONEYCOMB) + private int getNumColumnsCompatFrom11() { + return getNumColumns(); + } + +} \ No newline at end of file From 5cfb3b8b9c47b44aea147625c72375b3b27144a6 Mon Sep 17 00:00:00 2001 From: SadieYu Date: Wed, 13 May 2015 13:48:31 +0800 Subject: [PATCH 2/2] remove getNumColumns method which is already added --- .../views/GridViewWithHeaderAndFooter.java | 28 +++---------------- 1 file changed, 4 insertions(+), 24 deletions(-) diff --git a/src/in/srain/cube/views/GridViewWithHeaderAndFooter.java b/src/in/srain/cube/views/GridViewWithHeaderAndFooter.java index 8e1d437..5feea1f 100644 --- a/src/in/srain/cube/views/GridViewWithHeaderAndFooter.java +++ b/src/in/srain/cube/views/GridViewWithHeaderAndFooter.java @@ -64,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 mHeaderViewInfos = new ArrayList(); private ArrayList mFooterViewInfos = new ArrayList(); @@ -839,7 +840,7 @@ public void setOnItemLongClickListener(OnItemLongClickListener listener) { @Override public void onItemClick(AdapterView parent, View view, int position, long id) { if (mOnItemClickListener != null) { - int resPos = position - getHeaderViewCount() * getNumColumnsCompat(); + int resPos = position - getHeaderViewCount() * getNumColumnsCompatible(); if (resPos >= 0) { mOnItemClickListener.onItemClick(parent, view, resPos, id); } @@ -849,32 +850,11 @@ public void onItemClick(AdapterView parent, View view, int position, long id) @Override public boolean onItemLongClick(AdapterView parent, View view, int position, long id) { if (mOnItemLongClickListener != null) { - int resPos = position - getHeaderViewCount() * getNumColumnsCompat(); + int resPos = position - getHeaderViewCount() * getNumColumnsCompatible(); if (resPos >= 0) { mOnItemLongClickListener.onItemLongClick(parent, view, resPos, id); } } return true; } - - private int getNumColumnsCompat() { - if (Build.VERSION.SDK_INT >= 11) { - return getNumColumnsCompatFrom11(); - - } else { - try { - Field numColumns = getClass().getSuperclass().getDeclaredField("mNumColumns"); - numColumns.setAccessible(true); - return numColumns.getInt(this); - } catch (Exception e) { - return 1; - } - } - } - - @TargetApi(Build.VERSION_CODES.HONEYCOMB) - private int getNumColumnsCompatFrom11() { - return getNumColumns(); - } - } \ No newline at end of file