Android Open Source - TechDissected Feed Adapter






From Project

Back to project page TechDissected.

License

The source code is released under:

Apache License

If you think the Android project TechDissected 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.pkmmte.techdissected.adapter;
/*from   ww w .  j  a  v a 2  s .  co  m*/
import android.content.Context;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.pkmmte.pkrss.Article;
import com.pkmmte.techdissected.R;
import com.pkmmte.techdissected.util.Constants;
import com.pkmmte.techdissected.util.Utils;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
import java.util.List;

public class FeedAdapter extends BaseAdapter {
  private Context mContext;
  private List<Article> mFeed;
  private ColorMatrixColorFilter mFilter;
  private OnArticleClickListener mListener;
  private boolean grayscaleRead;

  public FeedAdapter(Context context) {
    this(context, new ArrayList<Article>());
  }

  public FeedAdapter(Context context, List<Article> feed) {
    this.mContext = context;
    this.mFeed = feed;
    ColorMatrix grayscaleFilter = new ColorMatrix();
    grayscaleFilter.setSaturation(0);
    this.mFilter = new ColorMatrixColorFilter(grayscaleFilter);
    try {
      this.grayscaleRead = context.getSharedPreferences(Constants.PREFS_NAME, Context.MODE_PRIVATE).getBoolean(Constants.PREF_READ, false);
    } catch (Exception e) {
      this.grayscaleRead = true;
    }
  }

  public void updateFeed(List<Article> feed) {
    this.mFeed = feed;
    this.notifyDataSetChanged();
  }

  public void setOnClickListener(OnArticleClickListener listener) {
    this.mListener = listener;
  }

  @Override
  public int getCount() {
    return mFeed.size();
  }

  @Override
  public Article getItem(int position) {
    return mFeed.get(position);
  }

  @Override
  public long getItemId(int position) {
    return position;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    final Article mArticle = mFeed.get(position);
    ViewHolder holder;

    if(convertView == null) {
      LayoutInflater mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      convertView = mInflater.inflate(R.layout.fragment_feed_article, parent, false);

      holder = new ViewHolder();
      holder.mCard = convertView.findViewById(R.id.card);
      holder.imgPreview = (ImageView) convertView.findViewById(R.id.imgPreview);
      holder.imgFavorite = (ImageView) convertView.findViewById(R.id.imgFavorite);
      holder.txtTag = (TextView) convertView.findViewById(R.id.txtTag);
      holder.txtTitle = (TextView) convertView.findViewById(R.id.txtTitle);
      holder.txtDescription = (TextView) convertView.findViewById(R.id.txtDescription);
      holder.txtAuthor = (TextView) convertView.findViewById(R.id.txtAuthor);
      holder.txtDate= (TextView) convertView.findViewById(R.id.txtDate);
      holder.currentTag = 0;

      convertView.setTag(holder);
    }
    else
      holder = (ViewHolder) convertView.getTag();

    Picasso.with(mContext).load(mArticle.getImage()).placeholder(R.drawable.placeholder).into(holder.imgPreview);

    if(mArticle.getTags().size() > 0) {
      holder.txtTag.setVisibility(View.VISIBLE);
      holder.txtTag.setText(mArticle.getTags().get(0));
    }
    else
      holder.txtTag.setVisibility(View.GONE);
    holder.txtTitle.setText(mArticle.getTitle());
    holder.txtDescription.setText(mArticle.getDescription());
    holder.txtAuthor.setText("By " + mArticle.getAuthor());
    holder.txtDate.setText(Utils.getRelativeDate(mArticle.getDate()));

    holder.mCard.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        if(mListener != null)
          mListener.onClick(mArticle);
      }
    });
    holder.imgFavorite.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        boolean favorite = !mArticle.isFavorite();
        mArticle.saveFavorite(favorite);

        if(mListener != null)
          mListener.onAddFavorite(mArticle, favorite);

        notifyDataSetChanged();
      }
    });

    if(grayscaleRead && mArticle.isRead())
      holder.imgPreview.setColorFilter(mFilter);
    else
      holder.imgPreview.clearColorFilter();

    holder.imgFavorite.setImageResource(mArticle.isFavorite() ? R.drawable.selector_favorite_full : R.drawable.selector_favorite_empty);

    return convertView;
  }

  public interface OnArticleClickListener {
    public void onClick(Article article);
    public void onAddFavorite(Article article, boolean favorite);
  }

  private class ViewHolder {
    public View mCard;
    public ImageView imgPreview;
    public ImageView imgFavorite;
    public TextView txtTag;
    public TextView txtTitle;
    public TextView txtDescription;
    public TextView txtAuthor;
    public TextView txtDate;
    public int currentTag;
  }
}




Java Source Code List

com.pkmmte.techdissected.ApplicationTest.java
com.pkmmte.techdissected.activity.ArticleActivity.java
com.pkmmte.techdissected.activity.MainActivity.java
com.pkmmte.techdissected.activity.SearchActivity.java
com.pkmmte.techdissected.adapter.AuthorAdapter.java
com.pkmmte.techdissected.adapter.CreditsLibraryAdapter.java
com.pkmmte.techdissected.adapter.FeedAdapter.java
com.pkmmte.techdissected.adapter.NavDrawerAdapter.java
com.pkmmte.techdissected.adapter.SettingsAdapter.java
com.pkmmte.techdissected.fragment.AboutFragment.java
com.pkmmte.techdissected.fragment.ArticleFragment.java
com.pkmmte.techdissected.fragment.FavoritesFragment.java
com.pkmmte.techdissected.fragment.FeedFragment.java
com.pkmmte.techdissected.fragment.SettingsFragment.java
com.pkmmte.techdissected.model.Author.java
com.pkmmte.techdissected.model.CreditsLibraryItem.java
com.pkmmte.techdissected.model.ListBuilder.java
com.pkmmte.techdissected.model.SettingsItem.java
com.pkmmte.techdissected.util.Base64DecoderException.java
com.pkmmte.techdissected.util.Base64.java
com.pkmmte.techdissected.util.Constants.java
com.pkmmte.techdissected.util.Dialogs.java
com.pkmmte.techdissected.util.IabException.java
com.pkmmte.techdissected.util.IabHelper.java
com.pkmmte.techdissected.util.IabResult.java
com.pkmmte.techdissected.util.Inventory.java
com.pkmmte.techdissected.util.Purchase.java
com.pkmmte.techdissected.util.RoundTransform.java
com.pkmmte.techdissected.util.Security.java
com.pkmmte.techdissected.util.SkuDetails.java
com.pkmmte.techdissected.util.Utils.java
com.pkmmte.techdissected.view.BakedBezierInterpolator.java
com.pkmmte.techdissected.view.CustomShareActionProvider.java
com.pkmmte.techdissected.view.FlowLayout.java
com.pkmmte.techdissected.view.HeaderGridView.java
com.pkmmte.techdissected.view.PkDrawerLayout.java
com.pkmmte.techdissected.view.PkScrollView.java
com.pkmmte.techdissected.view.PkSwipeRefreshLayout.java
com.pkmmte.techdissected.view.StickyScrollView.java
com.pkmmte.techdissected.view.SwipeProgressBar.java