I've implemented a single row contextual action bar following the instructions from Google. The instructions say to add an OnLongClickListener
to each view and since I'm using a CustomAdapter
, I implemented it on each row using this code:
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
final ViewHolder holder;
final View tempView = convertView;
if (convertView == null) {
holder = new ViewHolder();
convertView.setOnLongClickListener(new View.OnLongClickListener() {
public boolean onLongClick(View view) {
if (mActionMode != null) {
return false;
}
mActionMode = getSherlockActivity().startActionMode(mActionModeCallback);
view.setSelected(true);
return true;
}
});
}
}
However, using that code disables the normal click, so I added an OnClickListener
to each view:
convertView.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
tempView.setBackgroundColor(getResources().getColor(R.color.blue));
final Intent details = new Intent(this, Details.class);
startActivity(details);
}
});
This more or less works, but seems hacky and the background color change isn't as "smooth" as the normal one. I also have another ListView
where I'm using a MultiChoiceModeListener
and setting the ChoiceMode
to CHOICE_MODE_MULTIPLE_MODAL
, so users can select more than one row, and the normal row click works fine.