Android Open Source - u2020-v2 Custom Adapter






From Project

Back to project page u2020-v2.

License

The source code is released under:

Apache License

If you think the Android project u2020-v2 listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.gabor.recyclerview;
// w ww  .jav a 2 s .  c  om
import android.app.NotificationManager;
import android.content.Context;
import android.support.v4.app.NotificationCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.badoo.mobile.util.WeakHandler;
import com.gabor.common.io.IOUtils;
import com.gabor.database.Car;
import com.gabor.database.Car_;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Provide views to RecyclerView with data from mDataSet.
 */
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> {
    private Car items;
    private static float densityFactor;
    private WeakHandler handler = new WeakHandler();

    /**
     * Provide a reference to the type of views that you are using (custom ViewHolder)
     */
    public class ViewHolder extends RecyclerView.ViewHolder {
        private final TextView titleView;
        private final TextView ownerView;
        private final TextView dateView;
        private final ImageView imageView;

        public ViewHolder(View v) {
            super(v);
            v.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                    MainActivity context = (MainActivity)view.getContext();
                    Car_ res = items.getCars().get(getPosition());

                    Toast.makeText(context, R.string.open_pdf, Toast.LENGTH_SHORT).show();

                    NotificationCompat.Builder mBuilder =
                        new NotificationCompat.Builder(context)
                            .setSmallIcon(R.drawable.ic_launcher)
                            .setContentTitle(context.getString(R.string.open_pdf));
                    NotificationManager mNotifyMgr =
                        (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
                    mNotifyMgr.notify(1, mBuilder.build());

                    if (res.getPdf() != null)
                        try {
                            // make sure the device runs a good PDF reader and not e.g. Quickoffice
                            IOUtils.openPdf(context.getAssets().open(res.getPdf()),
                                    res.getPdf(), context);
                        } catch (Exception e) {
                            Toast.makeText(context, e.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                }
            });
            titleView = (TextView) v.findViewById(R.id.titleHolder);
            ownerView = (TextView) v.findViewById(R.id.ownerHolder);
            dateView = (TextView) v.findViewById(R.id.dateHolder);
            imageView = (ImageView) v.findViewById(R.id.imageHolder);
        }

        public TextView getTitleView() { return titleView; }
        public TextView getOwnerView() { return ownerView; }
        public TextView getDateView() { return dateView; }
        public ImageView getImageView() { return imageView; }
    }

    /**
     * Initialize the dataset of the Adapter.
     *
     * @param dataSet List of cars to populate views to be used by RecyclerView.
     */
    public CustomAdapter(Car dataSet, int density) {
        if (dataSet == null)
            throw new IllegalArgumentException("model data is null");
        densityFactor = density / 160;
        items = dataSet;
    }

    // Create new views (invoked by the layout manager)
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        // Create a new view.
        View v = LayoutInflater.from(viewGroup.getContext())
                .inflate(R.layout.text_row_item, viewGroup, false);

        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(final ViewHolder viewHolder, final int position) {
        // Get element from your dataset at this position and replace the contents of the view
        // with that element
        Car_ car = items.getCars().get(position);
        viewHolder.getTitleView().setText(car.getTitle());
        viewHolder.getOwnerView().setText(car.getOwner());
        viewHolder.getDateView().setText(IOUtils.getDate(car.getDate()));
        if (car.getImages() != null && car.getImages().size() > 0)
            animate(viewHolder.getImageView(), car.getImages(), 0);
    }

    /**
     * Recursive slide show with fade-in effect
     * @param imageView The View which displays the images
     * @param images Holds bitmaps to display
     * @param imageIndex index of the first image to show in images[]
     */
    private void animate(final ImageView imageView, final List<String> images, final int imageIndex) {

        if (images.get(imageIndex) == null || images.get(imageIndex).length() == 0)
            return;

        Picasso.with(imageView.getContext()).
                load("file:///android_asset/" + images.get(imageIndex)).
                resize(0, (int) (100 * densityFactor)).
                into(imageView);

        Animation fadeIn = new AlphaAnimation(0, 1);
        fadeIn.setInterpolator(new DecelerateInterpolator());
        fadeIn.setDuration(2000);
        imageView.setAnimation(fadeIn);

        fadeIn.setAnimationListener(new Animation.AnimationListener() {
            public void onAnimationEnd(Animation animation) {
                if (images.size() - 1 > imageIndex)
                    handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            animate(imageView, images, imageIndex + 1);
                        }
                    }, 1000);
            }
            public void onAnimationRepeat(Animation animation) {}
            public void onAnimationStart(Animation animation) {}
        });
    }

    // Return the size of your dataset (invoked by the layout manager)
    @Override
    public int getItemCount() {
        return items.getCars().size();
    }
}




Java Source Code List

com.gabor.common.io.IOUtils.java
com.gabor.database.CarT.java
com.gabor.database.Car.java
com.gabor.database.Car_.java
com.gabor.database.DbHelper.java
com.gabor.database.ImagesT.java
com.gabor.recyclerview.CustomAdapter.java
com.gabor.recyclerview.MainActivityTest.java
com.gabor.recyclerview.MainActivity.java
com.gabor.recyclerview.RecyclerViewFragment.java