如何GestureDetector附加到一个列表preference? [英] How to attach GestureDetector to a ListPreference?

查看:127
本文介绍了如何GestureDetector附加到一个列表preference?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

附加 GestureDetector 来一个<一的挑战href="http://developer.android.com/reference/android/$p$pference/List$p$pference.html">List$p$pference是2倍:

  1. 获取一个句柄,这只是在preferences.xml定义(即Java的code没有实例化)的列表preference。
  2. 列表preference既不是一个查看,也没有活动的子类。

是否有可能在所有附加 GestureDetector 来一个<一个href="http://developer.android.com/reference/android/$p$pference/List$p$pference.html">List$p$pference?

Is it possible at all to attach a GestureDetector to a ListPreference?

如果是这样,如何​​将一去吗?我会在哪里写的code实例GestureDetector并实施监听?

If so, how would one go about this? Where would I write the code to instantiate GestureDetector and implement the listener?

推荐答案

除非我不太正确赶的问题,答案很可能是简单的比你想象的。该人士$ ​​C $下目录preferece 教导我们,它比周围的 AlertDialog ,一个包装而已在 ListView控件显示各种选项。现在, AlertDialog 实际上允许你得到的的ListView 它缠上,这可能是你所需要的句柄。

Unless I didn't quite catch the question correctly, the answer is probably simpler than you might think. The source code for ListPreferece teaches that it's little more than a wrapper around an AlertDialog that displays its various options in a ListView. Now, AlertDialog actually allows you to get a handle on the ListView it wraps, which is probably all you need.

在评论一个你表明,在这个阶段,所有你感兴趣的是检测列表中的任何项目一个长期preSS。因此,而不是回答这个通过附加 GestureDetector ,我会简单地使用 OnItemLongClickListener

In one of the comments you indicated that, at this stage, all you're interested in is detecting a long-press on any item in the list. So rather than answering that by attaching a GestureDetector, I'll simply use an OnItemLongClickListener.

public class ListPreferenceActivity extends PreferenceActivity implements OnPreferenceClickListener {

    private ListPreference mListPreference;

    @Override protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        addPreferencesFromResource(R.xml.list_prefs);

        mListPreference = (ListPreference) findPreference("pref_list");
        mListPreference.setOnPreferenceClickListener(this);
    }

    @Override
    public boolean onPreferenceClick(Preference preference) {
        AlertDialog dialog = (AlertDialog) mListPreference.getDialog();
        dialog.getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long id) {
                Toast.makeText(getApplicationContext(), "Long click on index " + position + ": " 
                        + parent.getItemAtPosition(position).toString(), Toast.LENGTH_SHORT).show();
                return false;
            }
        });
        return false;
    }

}

结果(这在长按显示吐司):

The result (which the toast in the long-click displaying):

通过一个参照的ListView ,你也可以附加一个 OnTouchListener GestureDetector 等你来从这里走了。

With a reference to the ListView, you could also attach an OnTouchListener, GestureDetector etc. Up to you to go from here.

这篇关于如何GestureDetector附加到一个列表preference?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆