Google Glass:无法在沉浸式卡片之间滑动

时间:2014-12-02 18:07:38

标签: java android google-glass

我的问题是我已经创建了一个浸入式活动,里面有几张卡片。当我尝试在卡片之间滑动时,没有任何反应。我可以向下滑动以结束活动,但我无法在卡片之间滑动。有趣的是,如果我让玻璃杯进入睡眠然后再将其唤醒,我可以在卡片之间滑动。由于这种奇怪,我很确定我错过了一行代码,这些代码可以让卡片集中注意力,但我不知道它是什么。

我找到了使用手势检测器的解决方法,但它看起来不像普通的刷卡,所以我想避免这种情况。我的代码如下:

package com.akqa.glass.recipie;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.TextView;

import com.google.android.glass.media.Sounds;
import com.google.android.glass.view.WindowUtils;
import com.google.android.glass.widget.CardBuilder;
import com.google.android.glass.widget.CardScrollAdapter;
import com.google.android.glass.widget.CardScrollView;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by torti_000 on 11/29/2014.
 */
public class PairingsActivity extends Activity {

    private CardAdapter mAdapter;
    private CardScrollView mCardScroller;

    private int mTapPosition;
    private final Handler handler = new Handler();
    private boolean mVoiceMenuEnabled = true;
    private String ingredientUrl = null;
    private static final String TAG = "Ingredients";
    private static final String URL_BASE = "http://www.ingredientpairings.com/?i=";
    private ArrayList<String> iList;
    //Number of ingredients to display per card.
    private static final int ITEMS_PER_CARD = 4;
    private int numCards;
    String object = null;
    Context context;
    private View mView;

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        //Loading
        mView = buildView();
        mCardScroller = new CardScrollView(this);
        mCardScroller.setAdapter(new CardScrollAdapter() {
            @Override
            public int getCount() {
                return 1;
            }

            @Override
            public Object getItem(int position) {
                return mView;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                return mView;
            }

            @Override
            public int getPosition(Object item) {
                if (mView.equals(item)) {
                    return 0;
                }
                return AdapterView.INVALID_POSITION;
            }
        });
        setContentView(mCardScroller);
        Log.d(TAG, "View has been set");
        mCardScroller.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // Plays sound.
                AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
                am.playSoundEffect(Sounds.TAP);
            }
        });
        //Recipes
        iList = new ArrayList<String>();
        context = this;
        Intent pairings = getIntent();
        object = pairings.getStringExtra("Object");
        Log.i(TAG, "Pairings thinks object is: " + object);
        new GetIngredients().execute(object);
    }

    /**
     * Create Loding Page
     */
    private View buildView() {
        Log.d(TAG, "Creating Loading Card");
        CardBuilder card = new CardBuilder(this, CardBuilder.Layout.TEXT);
        card.setText("Loading");
        return card.getView();
    }

    /**
     * Creates list of cards that showcase different type of {@link com.google.android.glass.widget.CardBuilder} API.
     */
    private List<CardBuilder> createCards(Context context) {
        ArrayList<CardBuilder> cards = new ArrayList<CardBuilder>();
        Log.i(TAG, "Creating Cards");
        /*
         *  One ingredient per card...
         */
        for(int i = 0; i < iList.size(); i++){
            cards.add(new CardBuilder(context, CardBuilder.Layout.TEXT)
                .setText(iList.get(i)));
            Log.d(TAG, "Ingredient is: " + iList.get(i));
        }
        Log.d(TAG, "Done adding pairings?");
        return cards;
    }

    @Override
    protected void onResume() {
        super.onResume();
        mCardScroller.activate();
    }

    @Override
    protected void onPause() {
        mCardScroller.deactivate();
        super.onPause();
    }

    public class GetIngredients extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... strings) {
            StringBuffer buffer = new StringBuffer();
            try {
                ingredientUrl = object.replaceAll(" ", "+");
                Log.d(TAG, ingredientUrl);
//                strings[0].replaceAll(" ", "+");
                Log.d(TAG, "Finding matches for " + object);
                Document doc  = Jsoup.connect(URL_BASE + ingredientUrl).get();
                Log.d(TAG, "Matches found for " + object);
                Elements ingredients = doc.select("div.main  p b");
//                Log.d(TAG, "Ingredients are: " + ingredients.toString());
                for (Element ingredient : ingredients) {
                    String data = ingredient.text();
                    iList.add(data);
                    buffer.append("Data [" + data + "] \r\n");
                    Log.d(TAG, "Ingredient is: " + data);
                }
            }
            catch(Throwable t) {
                t.printStackTrace();
            }
            return buffer.toString();
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            // Compute the number of cards needed to display the items with 4 per card (rounding up to
            // capture the remainder).
            numCards = (int) Math.ceil((double) iList.size() / ITEMS_PER_CARD);
            startCards();
        }
    }
    private void startCards(){
        mAdapter = new CardAdapter(createCards(this));
//        mCardScroller = new CardScrollView(this);
        mCardScroller.setAdapter(mAdapter);
        setContentView(mCardScroller);
    }
}

2 个答案:

答案 0 :(得分:1)

在你的onCreate()中,在setContentView()之前添加这一行,如下所示:

mCardScroller.activate();   //add this line
setContentView(mCardScroller);

你的onResume()中有这一行,这就是它的行为原因。

答案 1 :(得分:0)

我遇到了在CardScrollView和SurfaceView之间切换内容视图的问题。在确保onPause()和onResume()都在逻辑上正确的位置被调用之后,滚动条很好。

似乎CardScrollView失去焦点然后滚动失去了功能,调用.activate()只有在事先调用.deactivate()时才有效,因此为什么需要同时调用onPause()和onResume()在逻辑上正确的位置,在onPause()中调用.deactivate(),在onResume()中调用.activate()。我试着自己调用.deactivate()和.activate()而不是onPause()和onResume(),两个实例的行为相同,支持我在这里写的内容。

因此,简而言之,如果使用setContentView()在视图之间切换,请务必在切换之前调用onPause(),然后在切换之后调用onResume(),否则滚动条将停止响应。