【问题标题】:ListView no longer animates on long click?ListView 在长按时不再动画?
【发布时间】:2012-10-11 19:49:24
【问题描述】:

This has been asked before 但没有一个适合我的答案(显然是另一个给出了对已接受/唯一答案的评论)。

我有一个ListView,并且我已经为列表中的每个项目实现了长按复制。我正在用我自己的设置OnLongClickListener 的适配器填写列表,它运行良好,除了当我触摸并按住一个项目时,它根本不会突出显示并且不会褪色。我没有在ListView 上设置任何事件,也没有在View 上设置任何其他事件我从我的适配器返回。

我已经阅读了一些修复,例如不使用 wrap_content、将 clickable 设置为 false 或将其完全从 XML 中删除。没有任何影响。我注意到,如果我删除我的 Long Click Listener,它会为点击设置动画,但不会为淡入淡出的长点击设置动画。

如何在我的 ListView 项目上保留长点击淡化动画?

仅供参考:该应用的目标是 Gingerbread (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>

我的 onCreate 方法中的相关代码:

    Adapter = new HashEntryAdapter(this, Entries);// Entries is Vector<HashEntry>
    ListView list = (ListView)findViewById(R.id.HashList);
    list.setAdapter(Adapter);

HashEntry 只是一个包含 2 个字符串的类:散列的名称和散列的结果输出,分别称为 NameValueHashEntryAdapter如下:

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>

vikki 的 OnCreateContextMenu:

    list.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
        @Override
        public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
        }
    });

【问题讨论】:

  • “冷冻酸奶”(FroYo) 是 Android 2.2,而不是 2.3.3。
  • 哎呀...我知道我的目标是 2.3.3,我只是稍微了解了一下它的名称。编辑说姜饼。
  • 当您想删除对话线程时,您可以像股票消息应用程序在 ICS 中那样使用 onCreateContextMenu。如果您将ContextMenuInfo 转换为AdapterContextMenuInfo,您可以获得idposition,就像在onLongClickListener 中一样。
  • @vikki:它的功能相同,但仍然没有动画。
  • 在列表视图而不是单个视图上设置OnCreateContextMenuListener,例如listView.setOnCreateContextMenuListener(...)

标签: android listview animation


【解决方案1】:

当您想要删除对话线程时,您可以像股票消息应用程序在 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;
    }
});

【讨论】:

  • 非常感谢,很抱歉我花了这么长时间才明白你在说什么。 5 天前,您将此放在评论中。效果很好!
  • 我不知道你本来可以。我想我有点太关注我实际上并没有创建上下文菜单的事实。我很快就忽略了答案。
【解决方案2】:

在listview上设置onlongclicklistener,在你声明的listview上设置它,在你的适配器之外

因此,不是适配器中的每个单元格都有一个 onlongclicklistener,而是 listview 代替

listview.setOnLongClickListener(){new OnLongClickListener....

【讨论】:

  • 我不知道为什么,但该事件从未真正触发。对于测试,我输入了一条 Toast 消息,但它从未出现。不过,动画又可以工作了。
  • @CoreyOgburn 发布您的代码、声明列表视图的位置、xml 以及设置适配器的位置
  • 我希望这是一个足够常见的问题,我不必这样做,但我做到了。
猜你喜欢
  • 2010-12-22
  • 2023-03-15
  • 2011-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-07
  • 1970-01-01
相关资源
最近更新 更多