From 880a7f019ba55238a087c1441c939e8d3d5fa8f5 Mon Sep 17 00:00:00 2001 From: zgb Date: Tue, 28 Oct 2014 15:15:20 +0800 Subject: [PATCH 1/2] add onItemLongClickListener; change the adapter from ListAdapter to BaseAdapter ; --- .../com/linearlistview/LinearListView.java | 117 ++++++++++++++++-- 1 file changed, 108 insertions(+), 9 deletions(-) mode change 100644 => 100755 linearlistview/src/main/java/com/linearlistview/LinearListView.java 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..b36aea2 --- a/linearlistview/src/main/java/com/linearlistview/LinearListView.java +++ b/linearlistview/src/main/java/com/linearlistview/LinearListView.java @@ -3,16 +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.BaseAdapter; import android.widget.ListAdapter; -import android.widget.WrapperListAdapter; - -import com.linearlistview.internal.IcsLinearLayout; /** * An extension of a linear layout that supports the divider API of Android @@ -30,9 +27,11 @@ public class LinearListView extends IcsLinearLayout { private static final int LinearListView_dividerThickness = 1; private View mEmptyView; - private ListAdapter mAdapter; +// private ListAdapter mAdapter; + private BaseAdapter mAdapter; private boolean mAreAllItemsSelectable; private OnItemClickListener mOnItemClickListener; + private OnItemLongClickListener mOnItemLongClickListener; private DataSetObserver mDataObserver = new DataSetObserver() { @Override @@ -100,7 +99,7 @@ public void setDividerThickness(int thickness) { requestLayout(); } - public ListAdapter getAdapter() { + public BaseAdapter getAdapter() { return mAdapter; } @@ -114,7 +113,7 @@ public ListAdapter getAdapter() { * * @see #getAdapter() */ - public void setAdapter(ListAdapter adapter) { + public void setAdapter(BaseAdapter adapter) { if (mAdapter != null) { mAdapter.unregisterDataSetObserver(mDataObserver); } @@ -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 @@ -203,7 +270,7 @@ public boolean performItemClick(View view, int position, long id) { public void setEmptyView(View emptyView) { mEmptyView = emptyView; - final ListAdapter adapter = getAdapter(); + final BaseAdapter adapter = getAdapter(); final boolean empty = ((adapter == null) || adapter.isEmpty()); updateEmptyStatus(empty); } @@ -256,7 +323,13 @@ 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)); } + +// if(child.getLayoutParams() == null){ +// child.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); +// } + addViewInLayout(child, -1, child.getLayoutParams(), true); } } @@ -283,4 +356,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; + } + } } From 97589c039669a8191e9a049eefb942cd0de8fc9f Mon Sep 17 00:00:00 2001 From: zgb Date: Fri, 31 Oct 2014 15:53:27 +0800 Subject: [PATCH 2/2] fix pull ; --- .../java/com/linearlistview/LinearListView.java | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/linearlistview/src/main/java/com/linearlistview/LinearListView.java b/linearlistview/src/main/java/com/linearlistview/LinearListView.java index b36aea2..7538bf2 100755 --- a/linearlistview/src/main/java/com/linearlistview/LinearListView.java +++ b/linearlistview/src/main/java/com/linearlistview/LinearListView.java @@ -8,8 +8,9 @@ import android.view.SoundEffectConstants; import android.view.View; import android.widget.ArrayAdapter; -import android.widget.BaseAdapter; import android.widget.ListAdapter; +import android.widget.WrapperListAdapter; +import com.linearlistview.internal.IcsLinearLayout; /** * An extension of a linear layout that supports the divider API of Android @@ -27,8 +28,7 @@ public class LinearListView extends IcsLinearLayout { private static final int LinearListView_dividerThickness = 1; private View mEmptyView; -// private ListAdapter mAdapter; - private BaseAdapter mAdapter; + private ListAdapter mAdapter; private boolean mAreAllItemsSelectable; private OnItemClickListener mOnItemClickListener; private OnItemLongClickListener mOnItemLongClickListener; @@ -99,7 +99,7 @@ public void setDividerThickness(int thickness) { requestLayout(); } - public BaseAdapter getAdapter() { + public ListAdapter getAdapter() { return mAdapter; } @@ -113,7 +113,7 @@ public BaseAdapter getAdapter() { * * @see #getAdapter() */ - public void setAdapter(BaseAdapter adapter) { + public void setAdapter(ListAdapter adapter) { if (mAdapter != null) { mAdapter.unregisterDataSetObserver(mDataObserver); } @@ -270,7 +270,7 @@ public boolean performItemLongClick(View view, int position, long id) { public void setEmptyView(View emptyView) { mEmptyView = emptyView; - final BaseAdapter adapter = getAdapter(); + final ListAdapter adapter = getAdapter(); final boolean empty = ((adapter == null) || adapter.isEmpty()); updateEmptyStatus(empty); } @@ -326,9 +326,6 @@ private void setupChildren() { child.setOnLongClickListener(new InternalOnLongClickListener(i)); } -// if(child.getLayoutParams() == null){ -// child.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); -// } addViewInLayout(child, -1, child.getLayoutParams(), true); }