Android Open Source - droidcon Link Adapter






From Project

Back to project page droidcon.

License

The source code is released under:

Apache License

If you think the Android project droidcon 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.timehop.droidcon2014retrofitsample.ui;
//from   www. ja va 2 s.c  o m
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.StateListDrawable;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.timehop.droidcon2014retrofitsample.R;
import com.timehop.droidcon2014retrofitsample.data.reddit.model.RedditLink;

public class LinkAdapter extends ArrayAdapter<RedditLink> {

  private static final int RED_HOT = 1000;

  public LinkAdapter(Context context) {
    super(context, 0);
  }

    private static class ViewHolder {

        private final TextView scoreTextView;
        private final TextView titleTextView;
        private final TextView authorTextView;

        private ViewHolder(final View view) {
            scoreTextView = (TextView) view.findViewById(R.id.score_textview);
            titleTextView = (TextView) view.findViewById(R.id.title_textview);
            authorTextView = (TextView) view.findViewById(R.id.author_textview);
        }
    }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    final RedditLink link = getItem(position);
    final ViewHolder vh;
    if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.view_link, parent, false);
        vh = new ViewHolder(convertView);
        convertView.setTag(vh);
    } else {
        vh = (ViewHolder) convertView.getTag();
    }
    vh.scoreTextView.setText(String.valueOf(link.getScore()));
    vh.titleTextView.setText(String.valueOf(link.getTitle()));
    vh.authorTextView.setText(String.valueOf(link.getAuthor()));
    setBackgroundColor(convertView, link.getScore());
    return convertView;
  }

  /*
   * set a background based on score, just for kicks
   */
  private static void setBackgroundColor(final View view, final int score) {
      final StateListDrawable sld = new StateListDrawable();
      sld.addState(new int[]{android.R.attr.state_pressed},
              new ColorDrawable(getScoreColor(score)));
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
          view.setBackground(sld);
      } else {
          view.setBackgroundDrawable(sld);
      }
  }

  private static int getScoreColor(final int score) {
    final float redRatio = (float) score / RED_HOT;
    final int red = redRatio <= 1
            ? (int) (redRatio * 255)
            : 255;
    final int blue = 255 - red;
    return Color.argb(100, red, 0, blue);
  }
}




Java Source Code List

com.timehop.droidcon2014retrofitsample.CommentsActivity.java
com.timehop.droidcon2014retrofitsample.MainActivity.java
com.timehop.droidcon2014retrofitsample.SubredditActivity.java
com.timehop.droidcon2014retrofitsample.VenueSearchActivity.java
com.timehop.droidcon2014retrofitsample.data.SynchronousExecutor.java
com.timehop.droidcon2014retrofitsample.data.foursquare.FoursquareService.java
com.timehop.droidcon2014retrofitsample.data.foursquare.FoursquareTests.java
com.timehop.droidcon2014retrofitsample.data.foursquare.api.FoursquareCredentials.java
com.timehop.droidcon2014retrofitsample.data.foursquare.api.FoursquareErrorHandler.java
com.timehop.droidcon2014retrofitsample.data.foursquare.api.FoursquareException.java
com.timehop.droidcon2014retrofitsample.data.foursquare.api.FoursquareRequestInterceptor.java
com.timehop.droidcon2014retrofitsample.data.foursquare.api.FoursquareResponse.java
com.timehop.droidcon2014retrofitsample.data.foursquare.legacy.VenueSearchTask.java
com.timehop.droidcon2014retrofitsample.data.foursquare.model.Category.java
com.timehop.droidcon2014retrofitsample.data.foursquare.model.Contact.java
com.timehop.droidcon2014retrofitsample.data.foursquare.model.Icon.java
com.timehop.droidcon2014retrofitsample.data.foursquare.model.Location.java
com.timehop.droidcon2014retrofitsample.data.foursquare.model.Menu.java
com.timehop.droidcon2014retrofitsample.data.foursquare.model.Meta.java
com.timehop.droidcon2014retrofitsample.data.foursquare.model.Price.java
com.timehop.droidcon2014retrofitsample.data.foursquare.model.ResponseWrapper.java
com.timehop.droidcon2014retrofitsample.data.foursquare.model.Stats.java
com.timehop.droidcon2014retrofitsample.data.foursquare.model.Venue.java
com.timehop.droidcon2014retrofitsample.data.reddit.DateTimeDeserializer.java
com.timehop.droidcon2014retrofitsample.data.reddit.MockRedditService.java
com.timehop.droidcon2014retrofitsample.data.reddit.RedditObjectDeserializer.java
com.timehop.droidcon2014retrofitsample.data.reddit.RedditService.java
com.timehop.droidcon2014retrofitsample.data.reddit.RedditTests.java
com.timehop.droidcon2014retrofitsample.data.reddit.RedditType.java
com.timehop.droidcon2014retrofitsample.data.reddit.model.MockModelHelper.java
com.timehop.droidcon2014retrofitsample.data.reddit.model.RedditAccount.java
com.timehop.droidcon2014retrofitsample.data.reddit.model.RedditComment.java
com.timehop.droidcon2014retrofitsample.data.reddit.model.RedditLink.java
com.timehop.droidcon2014retrofitsample.data.reddit.model.RedditListing.java
com.timehop.droidcon2014retrofitsample.data.reddit.model.RedditMore.java
com.timehop.droidcon2014retrofitsample.data.reddit.model.RedditObjectWrapper.java
com.timehop.droidcon2014retrofitsample.data.reddit.model.RedditObject.java
com.timehop.droidcon2014retrofitsample.data.reddit.model.RedditResponse.java
com.timehop.droidcon2014retrofitsample.data.reddit.model.RedditSubmission.java
com.timehop.droidcon2014retrofitsample.ui.CommentsAdapter.java
com.timehop.droidcon2014retrofitsample.ui.LinkAdapter.java
com.timehop.droidcon2014retrofitsample.ui.VenueAdapter.java