/*
* Copyright 2012 GitHub Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.mobile.gauges.ui;
import android.view.LayoutInflater;
import com.github.kevinsawicki.wishlist.SingleTypeAdapter;
import com.github.mobile.gauges.R.color;
import com.github.mobile.gauges.R.drawable;
import java.util.List;
/**
* List adapter that colors rows in alternating colors
*
* @param <V>
*/
public abstract class AlternatingColorListAdapter<V> extends
SingleTypeAdapter<V> {
private final int primaryResource;
private final int secondaryResource;
/**
* Create adapter with alternating row colors
*
* @param layoutId
* @param inflater
* @param items
*/
public AlternatingColorListAdapter(final int layoutId,
final LayoutInflater inflater, final List<V> items) {
this(layoutId, inflater, items, true);
}
/**
* Create adapter with alternating row colors
*
* @param layoutId
* @param inflater
* @param items
* @param selectable
*/
public AlternatingColorListAdapter(final int layoutId,
LayoutInflater inflater, final List<V> items, boolean selectable) {
super(inflater, layoutId);
if (selectable) {
primaryResource = drawable.table_background_selector;
secondaryResource = drawable.table_background_alternate_selector;
} else {
primaryResource = color.pager_background;
secondaryResource = color.pager_background_alternate;
}
setItems(items);
}
@Override
protected void update(final int position, final V item) {
if (position % 2 != 0)
updater.view.setBackgroundResource(primaryResource);
else
updater.view.setBackgroundResource(secondaryResource);
}
}