I am using the following list layout for items that have associated comments. The number of comments are indicated by the box on the right side.
Currently, I am using the onListItemClick
handler to launch another details view.
public class CustomListFragment extends ListFragment
implements LoaderCallbacks<Cursor> {
private Activity mActivity;
private CursorAdapter mAdapter;
private QueryParameters mQueryParameters;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setEmptyText("No data to display");
mActivity = getActivity();
if (mActivity == null) {
Log.e(getClass().getName(), "Activity is null");
return;
}
// Prepare query.
mQueryParameters = QueryHelper.getQueryParameters(mActivity);
if (mQueryParameters == null || !mQueryParameters.isPreparedForLoader()) {
Log.d(getClass().getName(), "One or more query parameters are null.");
return;
}
mAdapter = new CustomCursorAdapter(mActivity, null, 0);
setListAdapter(mAdapter);
getLoaderManager().initLoader(0, null, this);
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle extras) {
return new CursorLoader(mActivity,
mQueryParameters.uri,
mQueryParameters.fromColumnsLoader,
mQueryParameters.selection,
mQueryParameters.selectionArgs,
mQueryParameters.sortOrder);
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
mAdapter.swapCursor(cursor);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
mAdapter.swapCursor(null);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
Intent intent = new Intent(mActivity, ItemDetailsActivity.class);
intent.putExtra("ITEM_URI", mItemUri);
mActivity.startActivity(intent);
}
}
I would like to implement that the user can touch the box element on the right. This action should then start the associated CommentsActivity
. When the user touches the left side, however, it should still launch the ItemDetailsActivity
.
In both cases, I need to pass the itemUri
with the intent so that either the details view or the comments list for the particular item can be loaded.
The ListFragment
uses a CursorAdapter
to bind the TextView
s to the properties of an item.
Question:
- How can I attach a second touch action to a single list item?
You hadn't posted any code relating to the adapter itself, but I found your previous question and you are most of the way there.
The Quick and Dirty Answer
In
bindView()
, let's modify yourcomments_count
TextView to save the index of the current row in the tag (for youritemUri
) and add a simple OnClickListener:When the user clicks on the row it will still call
onListItemClick()
, except if they click on the comments box. The comments box fires the OnClickListener above where you can direct the user to yourCommentsActivity
. You didn't mention where you fetch the different values foritemUri
but I assumed you need the row's id to get it.Superior Answer
In your previous question, I noticed that you are making some repetitive calls and that Thiago Moreira Rocha's layout was extremely complex to be used repeatedly (for every ListView row.) So I propose a different approach. I've divided my answer into parts relating to the adapter, row layout, and colors for
comments_count
:The Adapter
I'll post the code in full and then explain at the bottom:
I made a few changes:
mFrom
holds the indices of the columns that you are using. You only need to get the column index once, it won't change unless you change the CursorcommentsClick
is one generic OnClickListener that I use for every row and I set it while creating aViewHolder
applyColorFilter()
ViewHolder
intonewView()
rather than checking for anull
one inbindView()
The Row Layout
You probably noticed that I change the comments' color a little differently, that is because I use a simpler row layout:
(While Thiago Moreira Rocha's layout works, the nested ViewGroups seem like overkill. Anytime you have a ViewGroup with only one child, their is usually an alternative.)
I use a LayerDrawable to replace the two LinearLayouts, that I will explain in in steps. First, the border (
border.xml
), very similar to the previous one:(Notice the padding is the width of the stroke.)
Second, the adjustable background color (
color.xml
):Last, I created a LayerDrawable to combine the two images (
comments_layers.xml
):(Optional)
You adjust the saturation of your HSV value in
applyColorFilter()
, but it seems that this is the equivalent to adjusting the alpha of a green background. If this is true, the changing the alpha value is a much simpler task. Find my comments inbindView()
:applyColorFilter(holder.comments_color, count);
holder.comments_color.setAlpha(count * 45);
color.xml
file and change thecolor
attribute of thesolid
element from#ffffff
to#2aff00
In all truth I had never used LayerDrawables like this before, there may be a faster way, but I think this is pretty slick.