How to enable scrolling on a simpleadapter on Goog

2019-02-10 16:28发布

问题:

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?

回答1:

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!