4

After updating my Google Glass up to XE16 my listview, which I have built by using a simpleadapter, is not able to scroll anymore. Is there a way to manually enable scrolling nonetheless with the GDK or fix this issue?

BarryK88
  • 1,806
  • 2
  • 25
  • 41
  • 2
    I filed an issue on the broken `ListView` scrolling on XE16 and 16.1 here - https://code.google.com/p/google-glass-api/issues/detail?id=484 – Sean Barbeau Apr 22 '14 at 18:13

1 Answers1

6

My listview stopped scrolling as well with the X16 update. You can build scrolling back in by doing the following:

In your activity's onCreate method, be sure to:

  1. set the list's choice mode
  2. set the list's clickable property to true.
  3. set the list's onItemClick listener
  4. create a gesture detector (see below)

For example:

myListView = (ListView)findViewById(R.id.MY_LIST_VIEW);
if(myListView != null){
    myListView.setAdapter(mAdapter);
    myListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    myListView.setClickable(true);

    myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
         public void onItemClick(AdapterView<?> parent, View view, int position, long id){
            Log.d("MY_LOG", "click at position " + position);
         }
    });
}

mGestureDetector = createGestureDetector(this);

Now, we need to write a new method for the createGestureDetector() call above (last line). Basically, you can modify the code given in the GDK docs to scroll up and down based on SWIPE_LEFT and SWIPE_RIGHT gestures. Note that in the above code, I assigned my listView to a variable called myListView. Here's a sample method for the gesture detector that will scroll based on the swipe gestures:

private GestureDetector createGestureDetector(Context context) {
    GestureDetector gestureDetector = new GestureDetector(context);
    //Create a base listener for generic gestures
    gestureDetector.setBaseListener( new GestureDetector.BaseListener() {
        @Override
        public boolean onGesture(Gesture gesture) {
            if (gesture == Gesture.TAP) { // On Tap, generate a new number
                return true;
            } else if (gesture == Gesture.TWO_TAP) {
                // do something on two finger tap
                return true;
            } else if (gesture == Gesture.SWIPE_RIGHT) {
                // do something on right (forward) swipe
                myListView.setSelection(myListView.getSelectedItemPosition()+1);
                return true;
            } else if (gesture == Gesture.SWIPE_LEFT) {
                // do something on left (backwards) swipe
                myListView.setSelection(myListView.getSelectedItemPosition()-1);
                return true;
            }
            return false;
        }
    });
    gestureDetector.setFingerListener(new GestureDetector.FingerListener() {
        @Override
        public void onFingerCountChanged(int previousCount, int currentCount) {
          // do something on finger count changes
        }
    });
    gestureDetector.setScrollListener(new GestureDetector.ScrollListener() {
        @Override
        public boolean onScroll(float displacement, float delta, float velocity) {
            // do something on scrolling

            return false;
        }
    });
    return gestureDetector;
}

Hope this helps!

adamup
  • 1,508
  • 19
  • 29
  • Awesome thanks a lot for your help, it works now. One thing; I also needed to add these imports "import com.google.android.glass.touchpad.Gesture; import com.google.android.glass.touchpad.GestureDetector;". – BarryK88 Apr 18 '14 at 21:19
  • Wow! Any idea why this changed in XE16? I have what appears like the exact same problem in a PreferenceFragment, which I presume contains a ListView. http://stackoverflow.com/questions/23160084/preferencefragment-cannot-scroll-up-down-on-xe16-worked-fine-on-xe12 Unfortunately I do not know how to get a handle to the ListView in a PreferenceFragment, or get/set its scroll position programmatically via a GestureDetector. – swooby Apr 19 '14 at 00:17
  • No idea, yet. I've been digging but haven't figured out what changed. Let me know if you discover anything. – adamup Apr 19 '14 at 00:18
  • 1
    This issue appears to affect AlertDialogs too - https://code.google.com/p/google-glass-api/issues/detail?id=326#c2 – Sean Barbeau Apr 22 '14 at 14:55
  • 1
    (Copying here so everyone on answer is notified) I filed an issue on the broken ListView scrolling on XE16 and 16.1 here - code.google.com/p/google-glass-api/issues/detail?id=484 – Sean Barbeau Apr 23 '14 at 13:18
  • 3
    As I noted in http://stackoverflow.com/a/23164323/937715, for this to work with ListActivity, you need to override onGenericMotionEvent(MotionEvent event) and pass these to the GestureDetector, as shown in http://stackoverflow.com/q/23160084/937715. – Sean Barbeau Apr 23 '14 at 19:16
  • 1
    fyi - I posted a question about the recommended practices for vertical lists in Glass Dev Community on G+ - https://plus.google.com/104230720789224785692/posts/cpzD4xjjJbo – Sean Barbeau Apr 24 '14 at 19:18
  • @adamup what if less items(ex: two items) which would not activate scrolling? How to get `setOnItemClickListener`? I have tried the code but `setOnItemClickListener` is not working? Any improvement on that? – hrskrs May 26 '15 at 08:24