Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 99 additions & 3 deletions linearlistview/src/main/java/com/linearlistview/LinearListView.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these import changes correct? Except swapping ListAdapter for BaseAdapter everything should stay the same.

Expand All @@ -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
Expand Down Expand Up @@ -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.
* <p>
* 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
Expand All @@ -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
Expand All @@ -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.
*
Expand All @@ -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
Expand Down Expand Up @@ -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);
}
}
Expand All @@ -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;
}
}
}