Skip to content
Open
Changes from 1 commit
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
117 changes: 108 additions & 9 deletions linearlistview/src/main/java/com/linearlistview/LinearListView.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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
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 @@ -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;
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.

Please do not commit commented out code.

private BaseAdapter mAdapter;
private boolean mAreAllItemsSelectable;
private OnItemClickListener mOnItemClickListener;
private OnItemLongClickListener mOnItemLongClickListener;
private DataSetObserver mDataObserver = new DataSetObserver() {

@Override
Expand Down Expand Up @@ -100,7 +99,7 @@ public void setDividerThickness(int thickness) {
requestLayout();
}

public ListAdapter getAdapter() {
public BaseAdapter getAdapter() {
return mAdapter;
}

Expand All @@ -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);
}
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,14 +240,37 @@ 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
*/
public void setEmptyView(View emptyView) {
mEmptyView = emptyView;

final ListAdapter adapter = getAdapter();
final BaseAdapter adapter = getAdapter();
final boolean empty = ((adapter == null) || adapter.isEmpty());
updateEmptyStatus(empty);
}
Expand Down Expand Up @@ -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){
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.

Please do not commit these lines

// child.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
// }

addViewInLayout(child, -1, child.getLayoutParams(), true);
}
}
Expand All @@ -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;
}
}
}