Skip to content

Commit

Permalink
Merge pull request #14 'sHa92:master' with some modifications
Browse files Browse the repository at this point in the history
  • Loading branch information
denizmveli committed Dec 28, 2013
2 parents 767093c + c36cc4d commit a652f63
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -36,11 +36,16 @@ protected void onCreate(Bundle savedInstanceState) {

final SampleAdapter adapter = new SampleAdapter(this, R.id.txt_line1);
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);

final List<String> sampleData = SampleData.generateSampleData();
for (String data : sampleData) {
adapter.add(data);
}
}

@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
Toast.makeText(this, "Item Clicked: " + position, Toast.LENGTH_SHORT).show();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -60,6 +62,8 @@ protected void onCreate(Bundle savedInstanceState) {
mGridView.setAdapter(mAdapter);

mGridView.setOnScrollListener(this);

mGridView.setOnItemClickListener(this);
}

@Override
Expand Down Expand Up @@ -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();
}
}
19 changes: 19 additions & 0 deletions sample/src/main/res/drawable/list_item_selector.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">

<!-- shapes defined for Android 2.3 drawable issues -->
<item android:state_pressed="true" >
<shape android:shape="rectangle">
 <solid android:color="@color/list_item_pressed" />
</shape>
</item>

<!-- default -->
<item>
<shape android:shape="rectangle">
 <solid android:color="@android:color/transparent" />
</shape>
</item>

</selector>
4 changes: 3 additions & 1 deletion sample/src/main/res/layout/activity_list_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
android:layout_height="match_parent"
android:listSelector="@drawable/list_item_selector"
android:drawSelectorOnTop="true" />

</FrameLayout>
2 changes: 1 addition & 1 deletion sample/src/main/res/layout/activity_sgv.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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"/>

</FrameLayout>
3 changes: 2 additions & 1 deletion sample/src/main/res/layout/list_item_sample.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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">

<com.etsy.android.grid.util.DynamicHeightTextView
android:id="@+id/txt_line1"
Expand Down
1 change: 1 addition & 0 deletions sample/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
<color name="blue">#ff82e0ff</color>
<color name="yellow">#fffffbae</color>
<color name="red">#fff10800</color>
<color name="list_item_pressed">#1A000000</color>
</resources>

0 comments on commit a652f63

Please sign in to comment.