There is currently no GDK user interface element for scrolling through a list using sensors (in fact, according to this problem , using ListView generally seems discouraged).
However, I was able to get the following to work well enough in my application . My list is fixed on 4 elements (which helps to determine how much scrolling happens), so you can configure it accordingly (see Comments).
import com.google.android.glass.media.Sounds; import com.google.android.glass.touchpad.Gesture; import com.google.android.glass.touchpad.GestureDetector; import android.content.Context; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.media.AudioManager; import android.view.MotionEvent; import android.widget.ListView; public class SensorListController implements SensorEventListener, GestureDetector.BaseListener { static final String TAG = "SensorListController"; Context mContext; ListView mList; SensorManager mSensorManager; private float[] mRotationMatrix = new float[16]; private float[] mOrientation = new float[9]; private float[] history = new float[2]; private float mHeading; private float mPitch; boolean mActive = true; GestureDetector mGestureDetector; public SensorListController(Context context, ListView list) { this.mContext = context; this.mList = list; history[0] = 10; history[1] = 10; mGestureDetector = new GestureDetector(mContext); mGestureDetector.setBaseListener(this); } public boolean onMotionEvent(MotionEvent event) { return mGestureDetector.onMotionEvent(event); } @Override public boolean onGesture(Gesture gesture) { switch (gesture) { case TWO_LONG_PRESS:
I used above in ListActivity . I initialize it in onCreate() , and here this method initializes it:
private void initListController() { mListView = getListView(); mListView.setChoiceMode(ListView.CHOICE_MODE_NONE); mListView.setSelector(android.R.color.transparent); mListView.setClickable(true); mListController = new SensorListController(this, mListView); }
It also removes the selection indicator from the view, making it transparent.
The above controller also uses two fingers to pause / resume scrolling and two fingers to scroll to the top of the list (and confirms both of these actions with sound). Please note that for these gestures you need to override onGenericMotionEvent() in your activity and go through the event, for example:
@Override public boolean onGenericMotionEvent(MotionEvent event) { // We need to pass events through to the list controller if (mListController != null) { return mListController.onMotionEvent(event); } return false; }
The full source code for this solution can be seen on Github and the APK can be downloaded.
source share