对我来说,函数cardScrollView.setSelection(cardScrollView.getSelectedItemPosition()+1)似乎不起作用。
在我的glass收到来自nodeJs服务器的消息后,我调用了该函数,但它似乎什么都没做。它没有给我看另一张卡片。
当我调用cardScrollView.getSelectedItemPosition()时,我确实得到了当前选择的索引。
也许我做错了什么?
这是我试图调用函数的mainActivity(我所说的行在client.on中("event",new EventCallback()函数):
package com.inspectie.team.inspecteren;
import com.google.android.glass.media.Sounds;
import com.google.android.glass.widget.CardBuilder;
import com.google.android.glass.widget.CardScrollAdapter;
import com.google.android.glass.widget.CardScrollView;
import com.inspectie.team.inspecteren.card.CardsActivity;
import com.inspectie.team.inspecteren.card.ContactActivity;
import com.inspectie.team.inspecteren.card.DoActivity;
import com.inspectie.team.inspecteren.card.CardAdapter;
import com.google.android.glass.widget.CardBuilder;
import com.google.android.glass.widget.CardScrollAdapter;
import com.google.android.glass.widget.CardScrollView;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import java.util.ArrayList;
import java.util.List;
import com.koushikdutta.async.http.AsyncHttpClient;
import com.koushikdutta.async.http.socketio.Acknowledge;
import com.koushikdutta.async.http.socketio.ConnectCallback;
import com.koushikdutta.async.http.socketio.EventCallback;
import com.koushikdutta.async.http.socketio.JSONCallback;
import com.koushikdutta.async.http.socketio.SocketIOClient;
import com.koushikdutta.async.http.socketio.StringCallback;
import org.json.JSONArray;
import org.json.JSONObject;
/**
* An {@link Activity} showing a tuggable "Hello World!" card.
* <p>
* The main content view is composed of a one-card {@link CardScrollView} that provides tugging
* feedback to the user when swipe gestures are detected.
* If your Glassware intends to intercept swipe gestures, you should set the content view directly
* and use a {@link com.google.android.glass.touchpad.GestureDetector}.
* @see <a href="https://developers.google.com/glass/develop/gdk/touch">GDK Developer Guide</a>
*/
public class MainActivity extends Activity {
private static final String TAG = MainActivity.class.getSimpleName();
// Index of api demo cards.
// Visible for testing.
static final int CARD_BUILDER = 0;
static final int CONTACT_MENU = 1;
static final int DO_MENU = 2;
boolean isConnected;
public static final String KEY_DEVICE_ID = "sender";
public static final String KEY_MESSAGE = "message";
public String device_id;
SocketIOClient mClient;
private CardScrollAdapter mAdapter;
private CardScrollView mCardScroller;
// Visible for testing.
CardScrollView getScroller() {
return mCardScroller;
}
@Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
// Request feature voice commands from API
//getWindow().requestFeature(WindowUtils.FEATURE_VOICE_COMMANDS);
mAdapter = new CardAdapter(createCards(this));
mCardScroller = new CardScrollView(this);
mCardScroller.setAdapter(mAdapter);
setContentView(mCardScroller);
setCardScrollerListener();
SocketIOClient.connect(AsyncHttpClient.getDefaultInstance(), "http://145.24.243.198:5000", new ConnectCallback() {
@Override
public void onConnectCompleted(Exception ex, SocketIOClient client) {
Log.i("SOCKET", "CONNECTION HIEROOOOOO");
if (ex != null) {
ex.printStackTrace();
return;
}
client.setStringCallback(new StringCallback() {
@Override
public void onString(String string, Acknowledge acknowledge) {
Log.d("SOCKET", string);
}
});
client.setJSONCallback(new JSONCallback() {
@Override
public void onJSON(JSONObject jsonObject, Acknowledge acknowledge) {
Log.d("SOCKET", jsonObject.toString());
}
});
client.on("event", new EventCallback() {
@Override
public void onEvent(JSONArray jsonArray, Acknowledge acknowledge) {
Log.i("DATA: ", "RECEIVED");
//SO HERE GOES NOTHING:
mCardScroller.setSelection(mCardScroller.getSelectedItemPosition() + 1);
mCardScroller.startAnimation(AnimationUtils.makeInAnimation(mCardScroller.getContext(), true));
}
});
mClient = client;
}
});
}
/**
* Create list of API demo cards.
*/
private List<CardBuilder> createCards(Context context) {
ArrayList<CardBuilder> cards = new ArrayList<CardBuilder>();
cards.add(CARD_BUILDER, new CardBuilder(context, CardBuilder.Layout.MENU)
.setText(R.string.main_menu_check)
.setIcon(R.drawable.icon_checklist));
cards.add(CONTACT_MENU, new CardBuilder(context, CardBuilder.Layout.MENU)
.setText(R.string.main_menu_contact)
.setIcon(R.drawable.icon_contact));
cards.add(DO_MENU, new CardBuilder(context, CardBuilder.Layout.MENU)
.setText(R.string.main_menu_do)
.setIcon(R.drawable.icon_tips));
return cards;
}
@Override
protected void onResume() {
super.onResume();
mCardScroller.activate();
}
@Override
protected void onPause() {
mCardScroller.deactivate();
super.onPause();
}
/**
* Different type of activities can be shown, when tapped on a card.
*/
private void setCardScrollerListener() {
mCardScroller.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
int soundEffect = Sounds.TAP;
switch (position) {
case CARD_BUILDER:
startActivity(new Intent(MainActivity.this, CardsActivity.class));
break;
case DO_MENU:
startActivity(new Intent(MainActivity.this, DoActivity.class));
break;
case CONTACT_MENU:
startActivity(new Intent(MainActivity.this, ContactActivity.class));
break;
default:
soundEffect = Sounds.ERROR;
Log.d(TAG, "Don't show anything");
}
// Play sound.
AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
am.playSoundEffect(soundEffect);
}
});
}
}
有人有什么建议吗?我花了几个小时的时间研究这个问题,我正在全力以赴。
谢谢!
Bram
您要查找的方法是[CardScrollView#animate
](https://developers.google.com/glass/develop/gdk/reference/com/google/android/glass/widget/CardScrollView#animate(int,com.google.android.glass.widget.CardScrollView.Animation)一:
示例CardScrollAdapter
:
private class ExampleCardScrollAdapter extends CardScrollAdapter {
...
// Inserts a card into the adapter, without notifying.
public void insertCardWithoutNotification(int position, CardBuilder card) {
mCards.add(position, card);
}
}
Activity
:的用法
private void insertNewCard(int position, CardBuilder card) {
// Insert new card in the adapter, but don't call
// notifyDataSetChanged() yet. Instead, request proper animation
// to inserted card from card scroller, which will notify the
// adapter at the right time during the animation.
mAdapter.insertCardWithoutNotification(position, card);
mCardScrollView.animate(position, CardScrollView.Animation.INSERTION);
}
这会插入一张卡片,而不会通知视图任何数据集已更改,并要求视图设置动画到新位置。