Resalte el elemento personalizado de la vista de lista cuando hace clic largo

He creado una vista de lista personalizada que tiene un ImageView a la izquierda y un TextView a la derecha. E implementando una Listview selección múltiple con el menú contextual de la actionbar .

El problema es que cuando hago clic largo en un elemento, no se resalta.

Aquí es cómo configuro la vista de lista en mi ListFragment

PasswordsFragment.java

 package mohd.itcs.safewallet; import android.os.Bundle; import android.support.v4.app.ListFragment; import android.view.ActionMode; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.AbsListView.MultiChoiceModeListener; import android.widget.AdapterView; import android.widget.AdapterView.OnItemLongClickListener; import android.widget.ListView; public class PasswordsFragment extends ListFragment { private String titles[] = { "item1", "item2", "item3" }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setListAdapter(new CustomPasswordsList(getActivity(), titles)); } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); /* * Setup Multiple Selection Mode */ getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL); getListView().setOnItemLongClickListener(new OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView listview, View item, int position, long id) { getListView().setItemChecked(position, true); return true; } }); getListView().setMultiChoiceModeListener(new MultiChoiceModeListener() { @Override public boolean onActionItemClicked(ActionMode arg0, MenuItem arg1) { // TODO Auto-generated method stub return false; } /* * Inflate Actionbar Menu for Passwords Multiple Selection */ @Override public boolean onCreateActionMode(ActionMode arg0, Menu menu) { getActivity().getMenuInflater().inflate( R.menu.passwords_context_menu, menu); return true; } @Override public void onDestroyActionMode(ActionMode arg0) { } @Override public boolean onPrepareActionMode(ActionMode arg0, Menu arg1) { return false; } @Override public void onItemCheckedStateChanged(ActionMode actionMode, int position, long id, boolean arg3) { /* * Change Title bar to number of selection */ int checkedItems = getListView().getCheckedItemCount(); actionMode.setTitle(String.valueOf(checkedItems) + " Selected"); } }); } } 

CustomPasswordsList.java

 package mohd.itcs.safewallet; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.ImageView; import android.widget.TextView; public class CustomPasswordsList extends ArrayAdapter { private final Context context; private final String values[]; public CustomPasswordsList(Context context, String[] values) { super(context, R.layout.password_list_item, values); this.context = context; this.values = values; } public View getView(int position, View convertView, ViewGroup parent) { LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View rowView = inflater.inflate(R.layout.password_list_item, parent, false); TextView passwordTitle = (TextView) rowView.findViewById(R.id.textView_passwordTitle); ImageView passwordIcon = (ImageView) rowView.findViewById(R.id.imageView_passwordIcon); passwordTitle.setText(values[position]); passwordIcon.setImageResource(R.drawable.facebook); return rowView; } } 

passwords_list_item

      

usar getActivity() es suficiente

 setListAdapter(new CustomPasswordsList(getActivity(), titles)); 

onItemCheckedStateChanged anular onItemCheckedStateChanged

  public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) { final int checkedCount = getListView().getCheckedItemCount(); // get checked items count 

Tomando de las muestras @

 android-sdk-linux/samples/android-17/ApiDemos/src/com/example/android/apis/view/List16 

Ejemplo: Modifique el siguiente de acuerdo a sus necesidades

 public class MainActivity extends ListActivity { String[] GENRES = new String[] { "Action", "Adventure", "Animation", "Children", "Comedy", "Documentary", "Dtwig", "Foreign", "History", "Independent", "Romance", "Sci-Fi", "Television", "Thriller" }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ListView lv = getListView(); lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL); lv.setMultiChoiceModeListener(new ModeCallback()); setListAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_activated_1, GENRES)); } @Override protected void onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); getActionBar().setSubtitle("Long press to start selection"); } private class ModeCallback implements ListView.MultiChoiceModeListener { public boolean onCreateActionMode(ActionMode mode, Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.list_select_menu, menu); mode.setTitle("Select Items"); return true; } public boolean onPrepareActionMode(ActionMode mode, Menu menu) { return true; } public boolean onActionItemClicked(ActionMode mode, MenuItem item) { switch (item.getItemId()) { case R.id.share: Toast.makeText(MainActivity.this, "Shared " + getListView().getCheckedItemCount() + " items", Toast.LENGTH_SHORT).show(); mode.finish(); break; default: Toast.makeText(MainActivity.this, "Clicked " + item.getTitle(), Toast.LENGTH_SHORT).show(); break; } return true; } public void onDestroyActionMode(ActionMode mode) { } public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) { final int checkedCount = getListView().getCheckedItemCount(); switch (checkedCount) { case 0: mode.setSubtitle(null); break; case 1: mode.setSubtitle("One item selected"); break; default: mode.setSubtitle("" + checkedCount + " items selected"); break; } } } } 

list_select_menu.xml

    

Disparo rápido

enter image description here

Dado que está dudando si funcionaría con un adaptador personalizado

enter image description here

Editar:

en res / values-v11 / styles.xml

     

En el diseño personalizado para el elemento raíz, agregue

  style="@style/activated" 

Establezca en el archivo de diseño de la fila de su lista (en el componente de nivel superior, generalmente un LinearLayout o RelativeLayout):

android: background = “? android: attr / activatedBackgroundIndicator”

Para entender lo que hace exactamente, consulte esta otra pregunta .