Android Open Source - demo-flickr-feed-android Feed Adapter






From Project

Back to project page demo-flickr-feed-android.

License

The source code is released under:

This is free and unencumbered software released into the public domain. Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a co...

If you think the Android project demo-flickr-feed-android 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 uk.org.tomek.flickrfeed.adapter;
/*  ww w  . j  av  a 2 s  .com*/
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import java.lang.ref.WeakReference;
import java.util.List;

import uk.org.tomek.flickrfeed.R;
import uk.org.tomek.flickrfeed.activity.ImageDetailsActivity;
import uk.org.tomek.flickrfeed.model.Item;
import uk.org.tomek.flickrfeed.utils.Logger;

/**
 * Adapter for the intial list of images.
 *
 * Created by tomek on 23/11/14.
 */
public class FeedAdapter extends RecyclerView.Adapter<FeedAdapter.ViewHolder> {

    private final static Logger mLogger = Logger.getLogger(FeedAdapter.class);
    private final Activity mActivity;

    public FeedAdapter(Activity activity) {
        mActivity = activity;
    }

    private List<Item> mItems;

    // Provide a reference to the views for each data item
    // Complex data items may need more than one view per item, and
    // you provide access to all the views for a data item in a view holder
    public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private final ViewHolderClickInterface mClickInterface;
        // each data item is just a string in this case
        private final TextView mTitle;
        private final TextView mAuthor;
        private final ImageView mImage;

        public ViewHolder(View v, ViewHolderClickInterface clickInterface) {
            super(v);
            mClickInterface = clickInterface;
            mTitle = (TextView) v.findViewById(R.id.list_item_title);
            mAuthor = (TextView) v.findViewById(R.id.list_item_author);
            mImage = (ImageView) v.findViewById(R.id.list_item_image);

            mTitle.setOnClickListener(this);
            mAuthor.setOnClickListener(this);
            mImage.setOnClickListener(this);
        }


        @Override
        public void onClick(View v) {
            int position = getPosition();
            if (v instanceof ImageView) {
                mClickInterface.onImageClick((ImageView) v, position);
            } else {
                mClickInterface.onViewClick(v, position);
            }
        }

        public interface ViewHolderClickInterface {
            void onImageClick(ImageView image, int position);

            void onViewClick(View view, int position);
        }

    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
        View view = inflater.inflate(R.layout.list_item_layout, viewGroup, false);

        return new ViewHolder(view, new ViewHolderClicksListener(mItems, mActivity));
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int i) {
        viewHolder.mTitle.setText(mItems.get(i).getTitle());
        String author = mItems.get(i).getAuthor();
        viewHolder.mAuthor.setText(author.replace("nobody@flickr.com", "").replace("(","").replace(")",""));
        Picasso.with(mActivity).load(mItems.get(i).getMedia().getM()).fit().into(viewHolder.mImage);
    }


    @Override
    public int getItemCount() {
        if (mItems != null) {
            return mItems.size();
        }
        return 0;
    }

    public void setFeeds(List<Item> feeds) {
        mItems = feeds;
        notifyDataSetChanged();
    }


    /**
     * Listener to the list clicks.
     */
    private static class ViewHolderClicksListener implements FeedAdapter.ViewHolder.ViewHolderClickInterface {

        private final Logger mLogger = Logger.getLogger(ViewHolderClicksListener.class);
        private final WeakReference<List<Item>> mItems;
        private final WeakReference<Activity> mActivity;

        public ViewHolderClicksListener(List<Item> items, Activity activity) {
            mItems = new WeakReference<List<Item>>(items);
            mActivity = new WeakReference<Activity>(activity);
        }

        @Override
        public void onImageClick(ImageView image, int position) {
            if (mItems.get() != null && mActivity.get() != null) {
                Item item = mItems.get().get(position);
                String imageUrl = item.getMedia().getM();
                mLogger.v("Image position:%d imageUrl:%s clicked", position, imageUrl);
                Intent intent = new Intent(mActivity.get(), ImageDetailsActivity.class);
                Bundle bundle = new Bundle();
                bundle.putParcelable(ImageDetailsActivity.ITEM_ARG, item);
                intent.putExtras(bundle);
                mActivity.get().startActivity(intent);
            }
        }

        @Override
        public void onViewClick(View view, int position) {
            String toastMessage = view.getContext().getString(R.string.toast_message);
            Toast.makeText(view.getContext(), String.format(toastMessage, position,
                    ((TextView) view).getText()), Toast.LENGTH_SHORT).show();
        }
    }
}




Java Source Code List

tomek.co.uk.flickrfeed.ApplicationTest.java
uk.org.tomek.flickrfeed.App.java
uk.org.tomek.flickrfeed.activity.ImageDetailsActivity.java
uk.org.tomek.flickrfeed.activity.MainActivity.java
uk.org.tomek.flickrfeed.activity.SettingsActivity.java
uk.org.tomek.flickrfeed.adapter.FeedAdapter.java
uk.org.tomek.flickrfeed.fagment.SettingsFragment.java
uk.org.tomek.flickrfeed.fagment.StartingFragment.java
uk.org.tomek.flickrfeed.model.Feed.java
uk.org.tomek.flickrfeed.model.Item.java
uk.org.tomek.flickrfeed.model.Media.java
uk.org.tomek.flickrfeed.rest.FlickrConverter.java
uk.org.tomek.flickrfeed.rest.MockRestClient.java
uk.org.tomek.flickrfeed.rest.RealRestClient.java
uk.org.tomek.flickrfeed.rest.RestClient.java
uk.org.tomek.flickrfeed.rest.RestUtils.java
uk.org.tomek.flickrfeed.rest.service.FlickrPostsService.java
uk.org.tomek.flickrfeed.utils.Logger.java
uk.org.tomek.flickrfeed.utils.StringUtils.java