0

For me the function cardScrollView.setSelection(cardScrollView.getSelectedItemPosition() + 1) doesnt seem to work.

Im calling the function after my glass receives a message from my nodeJs server, but it just doesnt seem to do anything. It's not showing me another card.

When I call cardScrollView.getSelectedItemPosition() I DO get the currently selected index however.

Maybe I'm doing something wrong?

Here is the mainActivity with where Im trying to call the function (The line I'm talking about is in the client.on("event", new EventCallback() function):

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);
            }
        });
    }

}

Does somebody have any suggestions? I've spend a few hours of research on this and I'm pulling my hair out.

Thanks!

Bram

1 Answers1

0

The method you're looking for is the [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)) one:

Example 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);
    }
}

Usage from your 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);
}

This inserts a Card without notifying the View of any dataset changed and asks the View to animate to the new position.

Alain
  • 6,044
  • 21
  • 27