Android ListView不再在长时间单击时设置动画?
但是没有一个对我有效的答案(显然另一个给出了对被接受/唯一答案的评论) 我有一个Android ListView不再在长时间单击时设置动画?,android,listview,animation,Android,Listview,Animation,但是没有一个对我有效的答案(显然另一个给出了对被接受/唯一答案的评论) 我有一个列表视图,并且我已经实现了一个长按按钮来复制列表中的每个项目。我用自己的适配器填写列表,该适配器设置了onlongclicklister,功能正常,只是当我触摸并按住某个项目时,它不会高亮显示,也不会褪色。我没有在列表视图上设置任何事件,也没有在视图上设置从适配器返回的其他事件 我读过一些修复方法,比如不使用wrap\u content、将clickable设置为false或将其完全从XML中删除。没有任何影响。我已
列表视图
,并且我已经实现了一个长按按钮来复制列表中的每个项目。我用自己的适配器填写列表,该适配器设置了onlongclicklister
,功能正常,只是当我触摸并按住某个项目时,它不会高亮显示,也不会褪色。我没有在列表视图
上设置任何事件,也没有在视图
上设置从适配器返回的其他事件
我读过一些修复方法,比如不使用wrap\u content
、将clickable设置为false或将其完全从XML中删除。没有任何影响。我已经注意到,如果我删除我的长点击监听器,它将动画点击,但不是一个褪色的长点击
如何在我的ListView项目上保留长时间单击淡出动画
仅供参考:该应用程序的目标是姜饼(2.3.3)
编辑:
我正在写一个简单的散列计算器。它有一项活动如下:
<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>
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;
}
}
最后是hashitem.xml,这是一个简单的布局,用于将灰色哈希名称置于黑色哈希值之上:
<?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>
在listview上设置onlongclicklistener,在您声明的listview上设置它,在适配器之外 因此,不是适配器中的每个单元格都有onlongclicklistener,而是listview
listview.setOnLongClickListener(){new OnLongClickListener....
当您想要删除对话线程时,可以像ICS中的股票消息应用程序一样使用onCreateContextMenu。如果将ContextMenuInfo转换为AdapterContextMenuInfo,则可以像在onLongClickListener中一样获取id和位置
import android.widget.AdapterView.AdapterContextMenuInfo
...
list.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
AdapterContextMenuInfo contextInfo = (AdapterContextMenuInfo) menuInfo;
int position = contextInfo.position;
long id = contextInfo.id;
// the child view who's info we're viewing (should be equal to v)
View view = contextInfo.targetView;
}
});
“冷冻酸奶”(FroYo)是Android 2.2,而不是2.3.3。哎呀。。。我知道我的目标是2.3.3,我只是对它的名字略知一二。编辑为姜饼。当您想要删除对话线程时,您可以像ICS中的股票消息应用程序一样使用
onCreateContextMenu
。如果将ContextMenuInfo
转换为AdapterContextMenuInfo
@vikki,则可以像在onLongClickListener
中一样获得id
和位置
:其功能相同,但仍然没有动画。在listview上而不是在各个视图上设置OnCreateContextMenuListener
,像listView.setOnCreateContextMenuListener(…)
我不知道为什么,但事件实际上从未触发。为了一个测试,我扔了一个祝酒词,但它从来没有出现过。不过动画又开始工作了。@CoreyOgburn发布你的代码,声明listview、xml的位置,以及设置适配器的位置。我希望这是一个常见的问题,我不必这么做,但我做到了。非常感谢你,很抱歉我花了这么长时间才明白你的意思。5天前,你在评论中提到了这一点。很好!我不知道你可能是。我想我有点太过关注这样一个事实,即我实际上并没有创建上下文菜单。我很快就忽略了答案。
listview.setOnLongClickListener(){new OnLongClickListener....
import android.widget.AdapterView.AdapterContextMenuInfo
...
list.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
AdapterContextMenuInfo contextInfo = (AdapterContextMenuInfo) menuInfo;
int position = contextInfo.position;
long id = contextInfo.id;
// the child view who's info we're viewing (should be equal to v)
View view = contextInfo.targetView;
}
});