Skip to content

Instantly share code, notes, and snippets.

@andraskindler
Last active August 29, 2015 14:07
Show Gist options
  • Save andraskindler/82681534539ff6bd6493 to your computer and use it in GitHub Desktop.
Save andraskindler/82681534539ff6bd6493 to your computer and use it in GitHub Desktop.
package com.andraskindler.playground.adapter;
import android.content.Context;
import android.os.Build;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.andraskindler.playground.R;
import java.util.List;
public class TranslucentStyleListAdapter extends BaseAdapter {
private static final int TYPE_NORMAL = 0;
private static final int TYPE_EMPTY_TOP = 1;
private static final int TYPE_EMPTY_BOTTOM = 2;
private final boolean hasTranslucentStatusBar;
private final boolean hasTranslucentNavigationBar;
private List<String> items;
public TranslucentStyleListAdapter(final Context context, final List<String> items) {
hasTranslucentStatusBar = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
final int id = context.getResources().getIdentifier("config_enableTranslucentDecor", "bool", "android");
hasTranslucentNavigationBar = id != 0 && context.getResources().getBoolean(id);
this.items = items;
}
@Override public int getCount() {
return items.size() + (hasTranslucentStatusBar ? 1 : 0) + (hasTranslucentNavigationBar ? 1 : 0);
}
@Override public int getItemViewType(int position) {
if (hasTranslucentStatusBar) {
if (position == 0) return TYPE_EMPTY_TOP;
if (hasTranslucentNavigationBar && position == getCount() - 1) return TYPE_EMPTY_BOTTOM;
}
return TYPE_NORMAL;
}
@Override public int getViewTypeCount() {
return 1 + (hasTranslucentStatusBar ? 1 : 0) + (hasTranslucentNavigationBar ? 1 : 0);
}
@Override public String getItem(int i) {
return items.get(i - (hasTranslucentStatusBar ? 1 : 0));
}
@Override public long getItemId(int i) {
return i;
}
@Override public View getView(int i, View view, ViewGroup viewGroup) {
final int itemViewType = getItemViewType(i);
switch (itemViewType) {
case TYPE_EMPTY_TOP:
if (view == null) {
final View emptyTopView = new View(viewGroup.getContext());
emptyTopView.setLayoutParams(new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight(viewGroup.getContext())));
view = emptyTopView;
}
break;
case TYPE_EMPTY_BOTTOM:
if (view == null) {
final View emptyBottomView = new View(viewGroup.getContext());
emptyBottomView.setLayoutParams(new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getNavigationBarHeight(viewGroup.getContext())));
view = emptyBottomView;
}
break;
default:
if (view == null) {
final TextView textView = new TextView(viewGroup.getContext());
textView.setBackgroundDrawable(viewGroup.getContext().getResources().getDrawable(R.drawable.list_item_background));
textView.setLayoutParams(new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 200));
textView.setGravity(Gravity.CENTER);
textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 32);
view = textView;
}
((TextView) view).setText(getItem(i));
break;
}
return view;
}
private int getStatusBarHeight(final Context context) {
final int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
return context.getResources().getDimensionPixelSize(resourceId);
}
return 0;
}
private int getNavigationBarHeight(final Context context) {
final int resourceId = context.getResources().getIdentifier("navigation_bar_height", "dimen", "android");
if (resourceId > 0) {
return context.getResources().getDimensionPixelSize(resourceId);
}
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment