Java tutorial
/* * Copyright (C) 2014 The Android Open Source Project * * 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.quuzz.tbg.recyclerview; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.graphics.Bitmap; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.os.Environment; import android.support.v4.app.FragmentActivity; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageButton; import android.widget.ImageView; import android.widget.TextView; import android.widget.ViewAnimator; import com.quuzz.tbg.common.activities.Installation; import com.quuzz.tbg.common.logger.Log; /** * Provide views to RecyclerView with data from mDataSet. */ public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> { private static final String TAG = "CustomAdapter"; private static FragmentActivity context; private static TbModel[] mDataSet; // BEGIN_INCLUDE(recyclerViewSampleViewHolder) /** * ProvidRecyclerViewe a reference to the type of views that you are using (custom ViewHolder) */ public static class ViewHolder extends RecyclerView.ViewHolder { private final TextView textView; private final ImageView imageView; public ViewHolder(View v, final ViewGroup viewGroup) { super(v); // Define click listener for the ViewHolder's View. v.setOnClickListener(new View.OnClickListener() { private boolean isDrawableShown; @Override public void onClick(View v) { Log.d(TAG, "Element " + getPosition() + " clicked."); //ViewAnimator output = (ViewAnimator) context.findViewById(R.id.sample_output); TextView tv = (TextView) context.findViewById(R.id.id_1); tv.setText(mDataSet[getPosition()].getDescription()); viewGroup.setVisibility(View.GONE); final ImageView iv = (ImageView) context.findViewById(R.id.tbView); // get image data from mDataSet array // ByteArrayInputStream is = new ByteArrayInputStream(mDataSet[getPosition()].getImageData()); // Bitmap bitmap = BitmapFactory.decodeStream(is); // iv.setImageBitmap(bitmap); Drawable imgDrawable = Drawable.createFromPath("/data/" + Environment.getDataDirectory() + "/" + context.getPackageName() + "/files/file_" + mDataSet[getPosition()].getTbId()); Bitmap bitmap = ((BitmapDrawable) imgDrawable).getBitmap(); Drawable scaledDrawable = new BitmapDrawable(context.getResources(), resizeImageForImageView(bitmap, false)); iv.setImageDrawable(scaledDrawable); iv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { viewGroup.setVisibility(View.VISIBLE); iv.setImageDrawable(null); TextView tv = (TextView) context.findViewById(R.id.id_1); String message = context.getResources().getString(R.string.intro_message); tv.setText(message); } }); // ImageButton ib = (ImageButton) context.findViewById(R.id.infoBtn); // ib.setOnClickListener(new View.OnClickListener() { // @Override // public void onClick(View v) { // Log.i("INFO BTN", "Click element: " + getPosition()); // } // }); } }); textView = (TextView) v.findViewById(R.id.textView); imageView = (ImageView) v.findViewById(R.id.imageView); } public TextView getTextView() { return textView; } public ImageView getImageView() { return imageView; } } private static Bitmap resizeImageForImageView(Bitmap bitmap, boolean forThumbs) { View fragView = context.findViewById(R.id.recyclerView); Bitmap resizedBitmap = null; int originalWidth = bitmap.getWidth(); int originalHeight = bitmap.getHeight(); int newWidth = fragView.getWidth(); int newHeight = fragView.getHeight(); if (forThumbs) { newWidth = newWidth / 3; } float multFactor = -1.0F; if (originalHeight < originalWidth) { multFactor = (float) originalWidth / (float) originalHeight; newWidth = (int) (newHeight * multFactor); } else { multFactor = (float) originalHeight / (float) originalWidth; newHeight = (int) (newWidth * multFactor); } resizedBitmap = Bitmap.createScaledBitmap(bitmap, newWidth, newHeight, false); return resizedBitmap; } // END_INCLUDE(recyclerViewSampleViewHolder) /** * Initialize the dataset of the Adapter. * */ public CustomAdapter(FragmentActivity context) { this.context = context; String appUUID = Installation.getAppUUID(context); Log.d(TAG, "appUUID: " + appUUID); mDataSet = initDataset(); } // Create new views (invoked by the layout manager) @Override public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) { viewGroup.setClickable(true); // Create a new view. View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.text_row_item, viewGroup, false); return new ViewHolder(v, viewGroup); } // Replace the contents of a view (invoked by the layout manager) @Override public void onBindViewHolder(ViewHolder viewHolder, final int position) { Log.d(TAG, "Element " + position + " set."); // Get element from your dataset at this position and replace the contents of the view // with that element viewHolder.getTextView().setText(mDataSet[position].getName()); // bind image to viewHolder // Drawable imgDrawable = context.getResources().getDrawable(R.drawable.tb_1, null); // viewHolder.getImageView().setImageDrawable(imgDrawable); Drawable imgDrawable = Drawable.createFromPath("/data/" + Environment.getDataDirectory() + "/" + context.getPackageName() + "/files/file_" + mDataSet[position].getTbId()); Bitmap bitmap = ((BitmapDrawable) imgDrawable).getBitmap(); // Scale it to 50 x 50 Drawable scaledDrawable = new BitmapDrawable(context.getResources(), resizeImageForImageView(bitmap, true)); viewHolder.getImageView().setImageDrawable(scaledDrawable); // ByteArrayInputStream is = new ByteArrayInputStream(mDataSet[position].getImageData()); // Bitmap bitmap = BitmapFactory.decodeStream(is); // viewHolder.getImageView().setImageBitmap(bitmap); } // END_INCLUDE(recyclerViewOnBindViewHolder) // Return the size of your dataset (invoked by the layout manager) @Override public int getItemCount() { return mDataSet.length; } private TbModel[] initDataset() { // init DB // Gets the data repository in write mode // String[] mDataset = new String[DATASET_COUNT]; TbDbHelper tbDbHelper = new TbDbHelper(context); SQLiteDatabase db = tbDbHelper.getReadableDatabase(); Cursor cursor = db.query("tbdata", null, null, null, null, null, null); int count = cursor.getCount(); cursor.moveToFirst(); TbModel[] mDataset = new TbModel[count]; for (int i = 0; i < count; i++) { mDataset[i] = new TbModel(); mDataset[i].setName(cursor.getString(cursor.getColumnIndex("name"))); mDataset[i].setTbId(cursor.getString(cursor.getColumnIndex("tb_id"))); mDataset[i].setImageData(cursor.getBlob(cursor.getColumnIndex("image_data"))); mDataset[i].setDescription(cursor.getString(cursor.getColumnIndex("description"))); cursor.moveToNext(); } cursor.close(); db.close(); return mDataset; } }