This has been asked before but without an answer that works for me (and apparently one other given the comment on the accepted/only answer).
I have a ListView
and I've implemented a long-press-to-copy for each item in the list. I'm filling in the list with my own adapter that sets the OnLongClickListener
and it's functioning fine except that when I touch and hold on an item, it doesn't highlight at all and doesn't fade. I haven't set any events on the ListView
and no other events on the View
I return from my Adapter.
I've read about several fixes like not using wrap_content
, setting clickable to false or removing it entirely from XML. Nothing has had an impact. I have noticed that if I remove my Long Click Listener that it will animate a click, but not a fading long click.
What can I do to preserve the long click fading animation on my ListView items?
FYI: the app is targeting Gingerbread (2.3.3)
EDIT:
I'm writing a simple hash calculator. It has one activity as follows:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/Input"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:ems="10"
android:inputType="text">
<requestFocus/>
</EditText>
<Button
android:id="@+id/Hash"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@id/Input"
android:onClick="StartHashes"
android:text="@string/HashButtonText"/>
<ListView
android:id="@+id/HashList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@id/Hash">
</ListView>
</RelativeLayout>
Relevant code from my onCreate method:
Adapter = new HashEntryAdapter(this, Entries);// Entries is Vector<HashEntry>
ListView list = (ListView)findViewById(R.id.HashList);
list.setAdapter(Adapter);
HashEntry
is simply a class containing 2 strings: the name of the hash and the resulting output of the hash called Name
and Value
respectively. The HashEntryAdapter
is as follows:
public class HashEntryAdapter extends ArrayAdapter<HashEntry> {
private Context context;
public HashEntryAdapter(Context context, List<HashEntry> objects) {
super(context, R.layout.hashitem, objects);
this.context = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//data from your adapter
HashEntry entry = getItem(position);
//we want to reuse already constructed row views...
if(convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.hashitem, null);
convertView.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
HashEntry entry = (HashEntry)v.getTag();
ClipboardManager clipboard = (ClipboardManager)v.getContext().getSystemService(Context.CLIPBOARD_SERVICE);
clipboard.setText(entry.Value);
Toast.makeText(context, entry.Name + " copied to clipboard", Toast.LENGTH_SHORT).show();
return true;
}
});
}
convertView.setTag(entry);
TextView Name = (TextView)convertView.findViewById(R.id.HashName);
TextView Value = (TextView)convertView.findViewById(R.id.HashValue);
Name.setText(entry.Name);
Value.setText(entry.Value);
return convertView;
}
}
And finally, hashitem.xml, a simple layout to put a grayed hash name over a black hash value:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/HashName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="@string/HashNameFiller"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/DarkGray"/>
<TextView
android:id="@+id/HashValue"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/HashName"
android:layout_centerHorizontal="true"
android:text="@string/HashValueFiller"
android:textAppearance="?android:attr/textAppearanceMedium"/>
</RelativeLayout>
OnCreateContextMenu for vikki:
list.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
}
});