6
A
回答
1
我通過GDK的開發者指南,就在https://developers.google.com/glass/develop/gdk/dev-guides和參考在https://developers.google.com/glass/develop/gdk/reference/index而且也絕對沒有這樣的內置在GDK UI元素,如在2013年十二月
所以對於現在的答案是發佈XE 12的是你必須使用傳感器來實現這一點。
2
1
目前用於滾動使用傳感器列表(其實沒有本地GDK UI元素,根據this issue ,使用ListView
似乎不鼓勵)。
但是,我能夠在my app中得到以下相當好的工作。我的列表固定爲4個元素(這有助於確定發生多少滾動),因此您可以相應地調整它(請參閱評論)。
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;
/**
* Implements sensor-based scrolling of a 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);
}
/**
* Receive pass-through of event from View
*/
public boolean onMotionEvent(MotionEvent event) {
return mGestureDetector.onMotionEvent(event);
}
@Override
public boolean onGesture(Gesture gesture) {
switch (gesture) {
case TWO_LONG_PRESS:
// Toggle on and off accelerometer control of the list by long press
playSuccessSound();
toggleActive();
return true;
case TWO_TAP:
// Go to top of the list
playSuccessSound();
scrollToTop();
return true;
}
return false;
}
/**
* Should be called from the onResume() of Activity
*/
public void onResume() {
mSensorManager = (SensorManager) mContext.getSystemService(Context.SENSOR_SERVICE);
mSensorManager.registerListener(this,
mSensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR),
SensorManager.SENSOR_DELAY_UI);
}
/**
* Should be called from the onPause() of Activity
*/
public void onPause() {
mSensorManager.unregisterListener(this);
}
/**
* Toggles whether the controller modifies the view
*/
public void toggleActive() {
mActive = !mActive;
}
@Override
public void onSensorChanged(SensorEvent event) {
if (mList == null || !mActive) {
return;
}
if (event.sensor.getType() == Sensor.TYPE_ROTATION_VECTOR) {
SensorManager.getRotationMatrixFromVector(mRotationMatrix, event.values);
SensorManager.remapCoordinateSystem(mRotationMatrix, SensorManager.AXIS_X,
SensorManager.AXIS_Z, mRotationMatrix);
SensorManager.getOrientation(mRotationMatrix, mOrientation);
mHeading = (float) Math.toDegrees(mOrientation[0]);
mPitch = (float) Math.toDegrees(mOrientation[1]);
float xDelta = history[0] - mHeading; // Currently unused
float yDelta = history[1] - mPitch;
history[0] = mHeading;
history[1] = mPitch;
float Y_DELTA_THRESHOLD = 0.13f;
// Log.d(TAG, "Y Delta = " + yDelta);
int scrollHeight = mList.getHeight()
/19; // 4 items per page, scroll almost 1/5 an item
// Log.d(TAG, "ScrollHeight = " + scrollHeight);
if (yDelta > Y_DELTA_THRESHOLD) {
// Log.d(TAG, "Detected change in pitch up...");
mList.smoothScrollBy(-scrollHeight, 0);
} else if (yDelta < -Y_DELTA_THRESHOLD) {
// Log.d(TAG, "Detected change in pitch down...");
mList.smoothScrollBy(scrollHeight, 0);
}
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
private void scrollToTop() {
mList.smoothScrollToPosition(0);
}
private void playSuccessSound() {
// Play sound to acknowledge action
AudioManager audio = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
audio.playSoundEffect(Sounds.SUCCESS);
}
}
我在ListActivity
中使用了上述方法。我初始化它在onCreate()
,這裏是初始化它的方法:
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);
}
這也通過使透明去除視圖選擇指示符。
上述控制器還使用兩個手指按壓來暫停/恢復滾動,兩個手指點擊滾動到列表頂部(並用聲音確認這兩個動作)。請注意,這些手勢來工作,你需要重寫onGenericMotionEvent()
在您的活動,並通過活動,如:
@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;
}
相關問題
- 1. 滾動視圖內滾動視圖android
- 2. 滾動滾動視圖時自動在滾動視圖內滾動地圖
- 3. TVOS - 滾動視圖內的UICollectionView - 滾動視圖不滾動
- 4. 當滾動其他滾動視圖時滾動視圖重置
- 5. 內部自定義視圖水平滾動視圖不滾動
- 6. 拉動滾動視圖內部刷新
- 7. WheelView不滾動滾動視圖內
- 8. 滾動視圖滾動佈局內?
- 9. 滾動滾動視圖
- 10. 滾動視圖不滾動
- 11. 滾動外滾動視圖,然後滾動內
- 12. 滾動視圖內部的相對佈局不可滾動
- 13. 當滾動另一個滾動視圖時要並行滾動滾動視圖
- 14. Android的滾動視圖自動滾動
- 15. 自動滾動滾動視圖?
- 16. 在android問題的滾動視圖內滾動視圖
- 17. 以編程方式滾動到滾動視圖內的視圖
- 18. 列表視圖不滾動滾動視圖內
- 19. 嵌套滾動視圖內滾動視圖不起作用
- 20. Android:滾動滾動視圖內的線性視圖
- 21. 在滾動視圖時滾動視圖內的android動畫視圖
- 22. ViewPager內部的水平滾動視圖
- 23. 內部陰影滾動視圖
- 24. 滾動視圖與圖像不滾動
- 25. 滾動視圖內的網格不滾動鼠標滾輪
- 26. ViewPager滾動視圖內
- 27. ExpandableListView在滾動視圖內
- 28. ImageView內滾動視圖(Android)
- 29. 嵌套滾動視圖自動滾動到底部
- 30. iOS滾動視圖禁用自動滾動到頂部