Java tutorial
/** * The MIT License (MIT) Copyright (c) 2013 Chute Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ package com.getchute.android.photopickerplus.ui.adapter; import android.annotation.SuppressLint; import android.content.Context; import android.database.Cursor; import android.support.v4.widget.CursorAdapter; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AbsListView; import android.widget.AbsListView.OnScrollListener; import android.widget.ImageView; import com.getchute.android.photopickerplus.R; import java.util.HashMap; import java.util.Map; public abstract class BaseCursorAdapter extends CursorAdapter implements OnScrollListener { private static LayoutInflater inflater = null; protected int dataIndex; public Map<Integer, String> tick = new HashMap<Integer, String>(); protected boolean shouldLoadImages = true; @SuppressLint("NewApi") public BaseCursorAdapter(Context context, Cursor c) { super(context, c, 0); inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); dataIndex = getDataIndex(c); } public abstract int getDataIndex(Cursor cursor); abstract public void setViewClickListener(View view, String path, int position); abstract public void setPlayButtonVisibility(ImageView imageView); abstract public void loadImageView(ImageView imageView, Cursor cursor); @Override public String getItem(int position) { final Cursor cursor = getCursor(); cursor.moveToPosition(position); return cursor.getString(dataIndex); } public static class ViewHolder { public ImageView imageViewThumb; public ImageView imageViewTick; public ImageView imageViewVideo; public View viewSelect; } @Override public View newView(Context context, Cursor cursor, ViewGroup parent) { ViewHolder holder; View vi = inflater.inflate(R.layout.gc_adapter_assets_grid, null); holder = new ViewHolder(); holder.imageViewThumb = (ImageView) vi.findViewById(R.id.gcImageViewThumb); holder.imageViewTick = (ImageView) vi.findViewById(R.id.gcImageViewTick); holder.imageViewVideo = (ImageView) vi.findViewById(R.id.gcImageViewVideo); holder.viewSelect = vi.findViewById(R.id.gcViewSelect); vi.setTag(holder); return vi; } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { // TODO Auto-generated method stub } @Override public void onScrollStateChanged(AbsListView view, int scrollState) { switch (scrollState) { case OnScrollListener.SCROLL_STATE_FLING: case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL: shouldLoadImages = false; break; case OnScrollListener.SCROLL_STATE_IDLE: shouldLoadImages = true; notifyDataSetChanged(); break; } } @Override public void changeCursor(Cursor cursor) { super.changeCursor(cursor); dataIndex = getDataIndex(cursor); } @Override public void bindView(View view, Context context, Cursor cursor) { ViewHolder holder = (ViewHolder) view.getTag(); String path = cursor.getString(dataIndex); holder.imageViewTick.setTag(path); if (shouldLoadImages) { loadImageView(holder.imageViewThumb, cursor); } if (tick.containsKey(cursor.getPosition())) { holder.imageViewTick.setVisibility(View.VISIBLE); holder.viewSelect.setVisibility(View.VISIBLE); view.setBackgroundColor(context.getResources().getColor(R.color.sky_blue)); } else { holder.imageViewTick.setVisibility(View.GONE); holder.viewSelect.setVisibility(View.GONE); view.setBackgroundColor(context.getResources().getColor(R.color.gray_light)); } holder.imageViewVideo.setVisibility(View.VISIBLE); setViewClickListener(view, path, cursor.getPosition()); setPlayButtonVisibility(holder.imageViewVideo); } }