diff --git a/linearlistview/src/main/java/com/linearlistview/LinearListView.java b/linearlistview/src/main/java/com/linearlistview/LinearListView.java old mode 100644 new mode 100755 index ec01370..7538bf2 --- a/linearlistview/src/main/java/com/linearlistview/LinearListView.java +++ b/linearlistview/src/main/java/com/linearlistview/LinearListView.java @@ -3,15 +3,13 @@ import android.content.Context; import android.content.res.TypedArray; import android.database.DataSetObserver; -import android.graphics.drawable.ColorDrawable; -import android.graphics.drawable.Drawable; import android.util.AttributeSet; +import android.util.Log; import android.view.SoundEffectConstants; import android.view.View; import android.widget.ArrayAdapter; import android.widget.ListAdapter; import android.widget.WrapperListAdapter; - import com.linearlistview.internal.IcsLinearLayout; /** @@ -33,6 +31,7 @@ public class LinearListView extends IcsLinearLayout { private ListAdapter mAdapter; private boolean mAreAllItemsSelectable; private OnItemClickListener mOnItemClickListener; + private OnItemLongClickListener mOnItemLongClickListener; private DataSetObserver mDataObserver = new DataSetObserver() { @Override @@ -155,6 +154,32 @@ public interface OnItemClickListener { */ void onItemClick(LinearListView parent, View view, int position, long id); } + + /** + * Interface definition for a callback to be invoked when an item in this + * LinearListView has been long clicked. + */ + public interface OnItemLongClickListener { + + /** + * Callback method to be invoked when an item in this LinearListView has + * been long clicked. + *

+ * Implementers can call getItemAtPosition(position) if they need to + * access the data associated with the selected item. + * + * @param parent + * The LinearListView where the long click happened. + * @param view + * The view within the LinearListView that was long clicked (this + * will be a view provided by the adapter) + * @param position + * The position of the view in the adapter. + * @param id + * The row id of the item that was long clicked. + */ + void onItemLongClick(LinearListView parent, View view, int position, long id); + } /** * Register a callback to be invoked when an item in this LinearListView has @@ -166,6 +191,17 @@ public interface OnItemClickListener { public void setOnItemClickListener(OnItemClickListener listener) { mOnItemClickListener = listener; } + + /** + * Register a callback to be invoked when an item in this LinearListView has + * been clicked. + * + * @param listener + * The callback that will be invoked. + */ + public void setOnItemLongClickListener(OnItemLongClickListener listener) { + mOnItemLongClickListener = listener; + } /** * @return The callback to be invoked with an item in this LinearListView has @@ -175,6 +211,14 @@ public final OnItemClickListener getOnItemClickListener() { return mOnItemClickListener; } + /** + * @return The callback to be invoked with an item in this LinearListView has + * been long clicked, or null id no callback has been set. + */ + public final OnItemLongClickListener getOnItemLongClickListener() { + return mOnItemLongClickListener; + } + /** * Call the OnItemClickListener, if it is defined. * @@ -196,6 +240,29 @@ public boolean performItemClick(View view, int position, long id) { return false; } + + /** + * Call the OnItemLongClickListener, if it is defined. + * + * @param view + * The view within the LinearListView that was clicked. + * @param position + * The position of the view in the adapter. + * @param id + * The row id of the item that was clicked. + * @return True if there was an assigned OnItemLongClickListener that was + * called, false otherwise is returned. + */ + public boolean performItemLongClick(View view, int position, long id) { + if (mOnItemLongClickListener != null) { + playSoundEffect(SoundEffectConstants.CLICK); + mOnItemLongClickListener.onItemLongClick(this, view, position, id); + return true; + } + + return false; + } + /** * Sets the view to show if the adapter is empty @@ -256,7 +323,10 @@ private void setupChildren() { View child = mAdapter.getView(i, null, this); if (mAreAllItemsSelectable || mAdapter.isEnabled(i)) { child.setOnClickListener(new InternalOnClickListener(i)); + child.setOnLongClickListener(new InternalOnLongClickListener(i)); } + + addViewInLayout(child, -1, child.getLayoutParams(), true); } } @@ -283,4 +353,30 @@ public void onClick(View v) { } } } + + /** + * Internal OnLongClickListener that this view associate of each of its children + * so that they can respond to OnItemLongClick listener's events. Avoid setting + * an OnLongClickListener manually. If you need it you can wrap the child in a + * simple {@link android.widget.FrameLayout}. + */ + private class InternalOnLongClickListener implements OnLongClickListener { + + int mPosition; + + public InternalOnLongClickListener(int position) { + mPosition = position; + } + + @Override + public boolean onLongClick(View v) { + // TODO Auto-generated method stub + if ((mOnItemLongClickListener != null) && (mAdapter != null)) { + mOnItemLongClickListener.onItemLongClick(LinearListView.this, v, + mPosition, mAdapter.getItemId(mPosition)); + return true; + } + return false; + } + } }