Skip to content
This repository was archived by the owner on Nov 26, 2025. It is now read-only.

Commit a652f63

Browse files
committed
Merge pull request #14 'sHa92:master' with some modifications
2 parents 767093c + c36cc4d commit a652f63

8 files changed

Lines changed: 90 additions & 6 deletions

File tree

library/src/main/java/com/etsy/android/grid/ExtendableListView.java

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ public abstract class ExtendableListView extends AbsListView {
143143
private FlingRunnable mFlingRunnable;
144144

145145
protected boolean mClipToPadding;
146+
private PerformClick mPerformClick;
146147

147148
/**
148149
* 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) {
939940
}
940941

941942
private boolean onTouchUpTap(final MotionEvent event) {
942-
// TODO : implement onListItemClick stuff here
943+
if (mPerformClick == null) {
944+
invalidate();
945+
mPerformClick = new PerformClick();
946+
}
947+
final int motionPosition = mMotionPosition;
948+
if (!mDataChanged && mAdapter.isEnabled(motionPosition)) {
949+
final PerformClick performClick = mPerformClick;
950+
performClick.mClickMotionPosition = motionPosition;
951+
performClick.rememberWindowAttachCount();
952+
performClick.run();
953+
}
943954
return true;
944955
}
945956

@@ -2679,4 +2690,40 @@ public void onRestoreInstanceState(Parcelable state) {
26792690
}
26802691
requestLayout();
26812692
}
2693+
2694+
private class PerformClick extends WindowRunnnable implements Runnable {
2695+
int mClickMotionPosition;
2696+
2697+
public void run() {
2698+
if (mDataChanged) return;
2699+
2700+
final ListAdapter adapter = mAdapter;
2701+
final int motionPosition = mClickMotionPosition;
2702+
if (adapter != null && mItemCount > 0 &&
2703+
motionPosition != INVALID_POSITION &&
2704+
motionPosition < adapter.getCount() && sameWindow()) {
2705+
final View view = getChildAt(motionPosition); // a fix by @pboos
2706+
2707+
if (view != null) {
2708+
performItemClick(view, motionPosition + mFirstPosition, adapter.getItemId(motionPosition));
2709+
}
2710+
}
2711+
}
2712+
}
2713+
2714+
/**
2715+
* A base class for Runnables that will check that their view is still attached to
2716+
* the original window as when the Runnable was created.
2717+
*/
2718+
private class WindowRunnnable {
2719+
private int mOriginalAttachCount;
2720+
2721+
public void rememberWindowAttachCount() {
2722+
mOriginalAttachCount = getWindowAttachCount();
2723+
}
2724+
2725+
public boolean sameWindow() {
2726+
return hasWindowFocus() && getWindowAttachCount() == mOriginalAttachCount;
2727+
}
2728+
}
26822729
}

sample/src/main/java/com/etsy/android/sample/ListViewActivity.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
import java.util.List;
1313

14-
public class ListViewActivity extends Activity {
14+
public class ListViewActivity extends Activity implements AdapterView.OnItemClickListener {
1515

1616
@Override
1717
protected void onCreate(Bundle savedInstanceState) {
@@ -36,11 +36,16 @@ protected void onCreate(Bundle savedInstanceState) {
3636

3737
final SampleAdapter adapter = new SampleAdapter(this, R.id.txt_line1);
3838
listView.setAdapter(adapter);
39+
listView.setOnItemClickListener(this);
3940

4041
final List<String> sampleData = SampleData.generateSampleData();
4142
for (String data : sampleData) {
4243
adapter.add(data);
4344
}
4445
}
4546

47+
@Override
48+
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
49+
Toast.makeText(this, "Item Clicked: " + position, Toast.LENGTH_SHORT).show();
50+
}
4651
}

sample/src/main/java/com/etsy/android/sample/StaggeredGridActivity.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
import android.view.LayoutInflater;
99
import android.view.View;
1010
import android.widget.AbsListView;
11+
import android.widget.AdapterView;
1112
import android.widget.TextView;
13+
import android.widget.Toast;
1214

1315
import com.etsy.android.grid.StaggeredGridView;
1416

15-
public class StaggeredGridActivity extends Activity implements AbsListView.OnScrollListener {
17+
public class StaggeredGridActivity extends Activity implements AbsListView.OnScrollListener, AbsListView.OnItemClickListener {
1618

1719
private static final String TAG = "StaggeredGridActivity";
1820
public static final String SAVED_DATA_KEY = "SAVED_DATA";
@@ -60,6 +62,8 @@ protected void onCreate(Bundle savedInstanceState) {
6062
mGridView.setAdapter(mAdapter);
6163

6264
mGridView.setOnScrollListener(this);
65+
66+
mGridView.setOnItemClickListener(this);
6367
}
6468

6569
@Override
@@ -100,4 +104,9 @@ private void onLoadMoreItems() {
100104
mAdapter.notifyDataSetChanged();
101105
mHasRequestedMore = false;
102106
}
107+
108+
@Override
109+
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
110+
Toast.makeText(this, "Item Clicked: " + position, Toast.LENGTH_SHORT).show();
111+
}
103112
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
3+
<selector xmlns:android="http://schemas.android.com/apk/res/android">
4+
5+
<!-- shapes defined for Android 2.3 drawable issues -->
6+
<item android:state_pressed="true" >
7+
<shape android:shape="rectangle">
8+
 <solid android:color="@color/list_item_pressed" />
9+
</shape>
10+
</item>
11+
12+
<!-- default -->
13+
<item>
14+
<shape android:shape="rectangle">
15+
 <solid android:color="@android:color/transparent" />
16+
</shape>
17+
</item>
18+
19+
</selector>

sample/src/main/res/layout/activity_list_view.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
<ListView
66
android:id="@+id/list_view"
77
android:layout_width="match_parent"
8-
android:layout_height="match_parent" />
8+
android:layout_height="match_parent"
9+
android:listSelector="@drawable/list_item_selector"
10+
android:drawSelectorOnTop="true" />
911

1012
</FrameLayout>

sample/src/main/res/layout/activity_sgv.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
android:layout_height="match_parent"
1010
app:item_margin="8dp"
1111
app:column_count_portrait="2"
12-
app:column_count_landscape="3" />
12+
app:column_count_landscape="3"/>
1313

1414
</FrameLayout>

sample/src/main/res/layout/list_item_sample.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
android:orientation="horizontal"
55
android:id="@+id/panel_content"
66
android:layout_width="match_parent"
7-
android:layout_height="wrap_content">
7+
android:layout_height="wrap_content"
8+
android:descendantFocusability="blocksDescendants">
89

910
<com.etsy.android.grid.util.DynamicHeightTextView
1011
android:id="@+id/txt_line1"

sample/src/main/res/values/colors.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@
66
<color name="blue">#ff82e0ff</color>
77
<color name="yellow">#fffffbae</color>
88
<color name="red">#fff10800</color>
9+
<color name="list_item_pressed">#1A000000</color>
910
</resources>

0 commit comments

Comments
 (0)