简体   繁体   中英

How to implement custom CardScrollView on Google Glass?

I try'd to use a CardScrollView instead of a single View.I can set cards(with custom layouts) successfully with my code.I need to change the values of my TextFields when a new Message arrived from my LocalBroadcastReceiver. Do you have some idea how/where I can solve it?

Here are some code snippets from my project.

This is the onCreate method in the activity that the CardScrollView implements.

private List<Integer> cards;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

    createCards();
    xDataScrollView = new CardScrollView(this);
    xDataScrollView.setAdapter(new XDataCardScrollAdapter(cards, getLayoutInflater()));
    xDataScrollView.activate();
    setContentView(xDataScrollView);

    LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver, new IntentFilter("newMqttData")); 
    ...
}

With the following createCards() Method

    public void createCards() {
    cards = new ArrayList<Integer>();
    cards.add(R.layout.x_general);
    cards.add(R.layout.x_temperatures);
}

And this CardScrollAdapter.

public class XDataCardScrollAdapter extends CardScrollAdapter{

private List<Integer> cards;
private LayoutInflater inflater;
private SingletonData data;

public XDataCardScrollAdapter(List<Integer> cards, LayoutInflater inflater){
    this.cards = cards;
    this.inflater = inflater;
    this.data = SingletonData.getInstance();
}
@Override
public int getCount() {
    return cards.size();
}

@Override
public Object getItem(int i) {
    return cards.get(i);
}

@Override
public View getView(int i, View view, ViewGroup viewGroup) {
    return inflater.inflate(cards.get(i),viewGroup,false);
}

@Override
public int getPosition(Object o) {
    return cards.indexOf(o);
}

}

Edit I was able to have some values in both defined cards at the startup of the activity but I'm still unsure how to update the data from my broadcast receiver. Here my modified getView method in XDataCardScrollAdapter. Even dont know if that is an acceptable solution or that something can go wrong here.

@Override
public View getView(int i, View view, ViewGroup viewGroup) {
    if(view == null){
        view = inflater.inflate(cards.get(i), viewGroup);
    }
    if(i == 0){
        TextView textView_speed = (TextView) view.findViewById(R.id.textView_speed);
        textView_speed.setText(data.getSpeed());
    ... more values here
    }else if(i==1){
        TextView textView_temperatureValue = (TextView) view.findViewById(R.id.textView_temperatureValue);
        textView_temperatureValue.setText(data.getTemperature());
    ...more values here
    }
    return view;
}

in your code just change the data like you normally would by finding the view and then changing things to it.

I believe that should just do it for you. If you're trying to add cards during runtime you just have to notify the adapter that your datalist changed since last time by using this

xDataScrollView.getAdapter().notifyDataSetChanged();

Hope that helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM