diff --git a/library/src/main/java/com/etsy/android/grid/ExtendableListView.java b/library/src/main/java/com/etsy/android/grid/ExtendableListView.java index 115162a..8b3a40f 100644 --- a/library/src/main/java/com/etsy/android/grid/ExtendableListView.java +++ b/library/src/main/java/com/etsy/android/grid/ExtendableListView.java @@ -143,6 +143,7 @@ public abstract class ExtendableListView extends AbsListView { private FlingRunnable mFlingRunnable; protected boolean mClipToPadding; + private PerformClick mPerformClick; /** * A class that represents a fixed view in a list, for example a header at the top @@ -939,7 +940,17 @@ private boolean onTouchUpScrolling(final MotionEvent event) { } private boolean onTouchUpTap(final MotionEvent event) { - // TODO : implement onListItemClick stuff here + if (mPerformClick == null) { + invalidate(); + mPerformClick = new PerformClick(); + } + final int motionPosition = mMotionPosition; + if (!mDataChanged && mAdapter.isEnabled(motionPosition)) { + final PerformClick performClick = mPerformClick; + performClick.mClickMotionPosition = motionPosition; + performClick.rememberWindowAttachCount(); + performClick.run(); + } return true; } @@ -2679,4 +2690,40 @@ public void onRestoreInstanceState(Parcelable state) { } requestLayout(); } + + private class PerformClick extends WindowRunnnable implements Runnable { + int mClickMotionPosition; + + public void run() { + if (mDataChanged) return; + + final ListAdapter adapter = mAdapter; + final int motionPosition = mClickMotionPosition; + if (adapter != null && mItemCount > 0 && + motionPosition != INVALID_POSITION && + motionPosition < adapter.getCount() && sameWindow()) { + final View view = getChildAt(motionPosition); // a fix by @pboos + + if (view != null) { + performItemClick(view, motionPosition + mFirstPosition, adapter.getItemId(motionPosition)); + } + } + } + } + + /** + * A base class for Runnables that will check that their view is still attached to + * the original window as when the Runnable was created. + */ + private class WindowRunnnable { + private int mOriginalAttachCount; + + public void rememberWindowAttachCount() { + mOriginalAttachCount = getWindowAttachCount(); + } + + public boolean sameWindow() { + return hasWindowFocus() && getWindowAttachCount() == mOriginalAttachCount; + } + } } diff --git a/sample/src/main/java/com/etsy/android/sample/ListViewActivity.java b/sample/src/main/java/com/etsy/android/sample/ListViewActivity.java index ac704ea..9ed5a98 100644 --- a/sample/src/main/java/com/etsy/android/sample/ListViewActivity.java +++ b/sample/src/main/java/com/etsy/android/sample/ListViewActivity.java @@ -11,7 +11,7 @@ import java.util.List; -public class ListViewActivity extends Activity { +public class ListViewActivity extends Activity implements AdapterView.OnItemClickListener { @Override protected void onCreate(Bundle savedInstanceState) { @@ -36,6 +36,7 @@ protected void onCreate(Bundle savedInstanceState) { final SampleAdapter adapter = new SampleAdapter(this, R.id.txt_line1); listView.setAdapter(adapter); + listView.setOnItemClickListener(this); final List sampleData = SampleData.generateSampleData(); for (String data : sampleData) { @@ -43,4 +44,8 @@ protected void onCreate(Bundle savedInstanceState) { } } + @Override + public void onItemClick(AdapterView adapterView, View view, int position, long id) { + Toast.makeText(this, "Item Clicked: " + position, Toast.LENGTH_SHORT).show(); + } } diff --git a/sample/src/main/java/com/etsy/android/sample/StaggeredGridActivity.java b/sample/src/main/java/com/etsy/android/sample/StaggeredGridActivity.java index 71c51a4..b951a8f 100644 --- a/sample/src/main/java/com/etsy/android/sample/StaggeredGridActivity.java +++ b/sample/src/main/java/com/etsy/android/sample/StaggeredGridActivity.java @@ -8,11 +8,13 @@ import android.view.LayoutInflater; import android.view.View; import android.widget.AbsListView; +import android.widget.AdapterView; import android.widget.TextView; +import android.widget.Toast; import com.etsy.android.grid.StaggeredGridView; -public class StaggeredGridActivity extends Activity implements AbsListView.OnScrollListener { +public class StaggeredGridActivity extends Activity implements AbsListView.OnScrollListener, AbsListView.OnItemClickListener { private static final String TAG = "StaggeredGridActivity"; public static final String SAVED_DATA_KEY = "SAVED_DATA"; @@ -60,6 +62,8 @@ protected void onCreate(Bundle savedInstanceState) { mGridView.setAdapter(mAdapter); mGridView.setOnScrollListener(this); + + mGridView.setOnItemClickListener(this); } @Override @@ -100,4 +104,9 @@ private void onLoadMoreItems() { mAdapter.notifyDataSetChanged(); mHasRequestedMore = false; } + + @Override + public void onItemClick(AdapterView adapterView, View view, int position, long id) { + Toast.makeText(this, "Item Clicked: " + position, Toast.LENGTH_SHORT).show(); + } } diff --git a/sample/src/main/res/drawable/list_item_selector.xml b/sample/src/main/res/drawable/list_item_selector.xml new file mode 100644 index 0000000..28b14a3 --- /dev/null +++ b/sample/src/main/res/drawable/list_item_selector.xml @@ -0,0 +1,19 @@ + + + + + + + +   + + + + + + +   + + + + \ No newline at end of file diff --git a/sample/src/main/res/layout/activity_list_view.xml b/sample/src/main/res/layout/activity_list_view.xml index 0494ac8..43c8184 100644 --- a/sample/src/main/res/layout/activity_list_view.xml +++ b/sample/src/main/res/layout/activity_list_view.xml @@ -5,6 +5,8 @@ + android:layout_height="match_parent" + android:listSelector="@drawable/list_item_selector" + android:drawSelectorOnTop="true" /> diff --git a/sample/src/main/res/layout/activity_sgv.xml b/sample/src/main/res/layout/activity_sgv.xml index 83520c4..3d2313c 100644 --- a/sample/src/main/res/layout/activity_sgv.xml +++ b/sample/src/main/res/layout/activity_sgv.xml @@ -9,6 +9,6 @@ android:layout_height="match_parent" app:item_margin="8dp" app:column_count_portrait="2" - app:column_count_landscape="3" /> + app:column_count_landscape="3"/> diff --git a/sample/src/main/res/layout/list_item_sample.xml b/sample/src/main/res/layout/list_item_sample.xml index 965b975..29ba727 100644 --- a/sample/src/main/res/layout/list_item_sample.xml +++ b/sample/src/main/res/layout/list_item_sample.xml @@ -4,7 +4,8 @@ android:orientation="horizontal" android:id="@+id/panel_content" android:layout_width="match_parent" - android:layout_height="wrap_content"> + android:layout_height="wrap_content" + android:descendantFocusability="blocksDescendants"> #ff82e0ff #fffffbae #fff10800 + #1A000000 \ No newline at end of file