我使用可滚动静态卡作为项目选择器(滚动直到找到一个,然后使用onItemSelected事件来捕获点击)。它可以工作,但它不会像菜单项和所有标准系统卡一样显示底部的滚动条。有没有办法启用它?
这是适配器代码:
private class FooCardScrollAdapter extends CardScrollAdapter {
@Override
public int findIdPosition(Object id) {
return -1;
}
@Override
public int findItemPosition(Object item) {
return mCards.indexOf(item);
}
@Override
public int getCount() {
return mCards.size();
}
@Override
public Object getItem(int position) {
return mCards.get(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return mCards.get(position).toView();
}
}
答案 0 :(得分:5)
从XE16开始,现在可以通过设置
来实现mCardScrollView.setHorizontalScrollBarEnabled(true);
答案 1 :(得分:0)
这是一个已知问题;目前无法在GDK上CardScrollView
获取滚动指示器。请关注我们的问题跟踪器上的issue 256,以便随着GDK的发展而更新!
答案 2 :(得分:0)
所以我遇到了和你一样的问题。要解决它,我必须创建自己的滚动条视图。它不如Mirror API的内置版本好,因为它不能处理滚动滚动,但它是我们可以做的最好的,直到谷歌发布它们自己。
首先我们创建一个自定义视图SimulatedScrollBar
:
public class SimulatedScrollBar extends View {
private static final String TAG = SimulatedScrollBar.class.getSimpleName();
private static final boolean DEBUG = false;
private static final int WHITE_SCROLLBAR_COLOR = 0xfffefefe;
private int mScrollPosition;
private int mNumItems;
private Paint mPaint;
private float mInnerWidth;
private float mInnerHeight;
private int mOffsetX;
private int mWidth;
private int mHeight;
public SimulatedScrollBar(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.getTheme().obtainStyledAttributes(
attrs,
R.styleable.SimulatedScrollBar,
0, 0);
try {
mScrollPosition = a.getInteger(R.styleable.SimulatedScrollBar_scrollPosition, 0);
mNumItems = a.getInteger(R.styleable.SimulatedScrollBar_numItems, 0);
} finally {
a.recycle();
}
init();
}
private void init() {
mPaint = new Paint();
mPaint.setColor(WHITE_SCROLLBAR_COLOR);
mPaint.setStyle(Paint.Style.FILL);
}
public int getScrollPosition() {
return mScrollPosition;
}
public void setScrollPosition(int scrollPosition) {
mScrollPosition = scrollPosition;
invalidate();
requestLayout();
}
public int getNumItems() {
return mNumItems;
}
public void setNumItems(int numItems) {
mNumItems = numItems;
invalidate();
requestLayout();
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
float xpad = (float)(getPaddingLeft() + getPaddingRight());
float ypad = (float)(getPaddingTop() + getPaddingBottom());
mInnerWidth = (float)w - xpad;
mInnerHeight = (float)h - ypad;
if (DEBUG) Log.i(TAG, "onSizeChanged() mInnerWidth=" + mInnerWidth + " mInnerHeight=" + mInnerHeight);
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
float widthFraction = mNumItems > 0 ? 1.0f / (float)mNumItems : 0;
float scrollFraction = mNumItems > 0 ? (float)mScrollPosition / (float)mNumItems : 0;
mOffsetX = (int)(mInnerWidth * scrollFraction);
mWidth = (int)(mInnerWidth * widthFraction);
mHeight = (int)mInnerHeight;
Rect rect = new Rect(mOffsetX, 0, mOffsetX + mWidth, mHeight);
if (DEBUG) Log.i(TAG, "onDraw() mOffsetX=" + mOffsetX + " mWidth=" + mWidth + " mHeight=" + mHeight
+ " mScrollPosition=" + mScrollPosition + " mNumItems=" + mNumItems);
canvas.drawRect(rect, mPaint);
}
}
我们需要一个属性文件`attrs.xml'来支持配置视图:
<resources>
<declare-styleable name="SimulatedScrollBar">
<attr name="scrollPosition" format="integer"/>
<attr name="numItems" format="integer"/>
</declare-styleable>
</resources>
现在我们制作一个布局card_scroll_layout.xml
,其中包含卡片滚动视图和我们的滚动条覆盖:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res/com.chanapps.glass.chan"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black">
<com.google.android.glass.widget.CardScrollView
android:id="@+id/card_scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<ProgressBar
android:id="@+id/progress_bar"
style="?android:attr/progressBarStyleHorizontal"
android:indeterminate="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
/>
<com.chanapps.glass.chan.view.SimulatedScrollBar
android:id="@+id/simulated_scroll_bar"
android:layout_width="match_parent"
android:layout_height="10px"
android:layout_gravity="bottom"
custom:numItems="0"
custom:scrollPosition="0"
/>
</FrameLayout>
以下是您在onCreate()
中放置的内容:
mSimulatedScrollBar = (SimulatedScrollBar)rootLayout.findViewById(R.id.simulated_scroll_bar);
mSimulatedScrollBar.setScrollPosition(0);
mSimulatedScrollBar.setNumItems(mAdapter.getCount());
mCardScrollView = (CardScrollView)rootLayout.findViewById(R.id.card_scroll_view);
mCardScrollView.setAdapter(mAdapter);
mCardScrollView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (mSimulatedScrollBar != null)
mSimulatedScrollBar.setScrollPosition(position);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
mCardScrollView.activate();
现在,当您在列表中滑动时,您会看到底部的滚动条跟踪列表位置。如果适配器更改大小,请更新适配器或onLoadFinished()
加载程序回调中的项目数和滚动位置。