How to connect GestureDetector to ListPreference?

The task of attaching the GestureDetector to the ListPreference is 2 times:

  • Obtaining a ListPreference descriptor, which is defined only in the preferences.xml file (i.e. it is not created in Java code).
  • ListPreference is not a subclass of View and Activity.

Is it even possible to attach a GestureDetector to a ListPreference ?

If so, how would this be done? Where would I write the code to instantiate the GestureDetector and implement the listener?

+7
source share
3 answers

If I do not quite understand the question correctly, the answer is probably simpler than you think. The source code for ListPreferece teaches that this is a little more than a wrapper around AlertDialog that displays various options in a ListView . Now AlertDialog allows you to get a handle to the ListView that it wraps, which is probably all you need.

In one of the comments, you indicated that at this stage all that interests you is the detection of a long press on any item in the list. So instead of answering this by adding a GestureDetector , I just use 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; } } 

Result (which is a toast in a long click):

enter image description here

With a reference to ListView you can also attach OnTouchListener , GestureDetector , etc. Before that you can go from here.

+4
source

As suggested by @TronicZomB, this is not possible.

You can get around this by creating your own derived ListPreference class by getting its view in the inherited onBindDialogView ().

Remember, however, that the latter is complex because onBindDialogView () is only called if onCreateDialogView () does not return null, and this can only happen if you create your own custom view for YourListPreference.

The recommended way to do this is to create a custom parameter .

Once you have done this, you have a link to the YourListPreference view, which is required to attach the GestureDetector, because one of the steps requires setOnTouchListener () in the view.

+1
source

I installed the GestrueDetector in ScrollView using setOnTouchListener earlier and looked for a similar method for ListPreference, however, since ListPreference does not contain such a method, I do not think it will be possible.

0
source

All Articles