Last active
August 29, 2015 14:05
-
-
Save px-amaac/80449d5cfe226e8004f7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.teststaggeredgrid; | |
import android.content.Context; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import com.squareup.picasso.Picasso; | |
import java.util.Collections; | |
import java.util.List; | |
import views.Item; | |
import views.ItemView; | |
public class ItemAdapter extends BindableAdapter<Item> { | |
private List<Item> images = Collections.emptyList(); | |
private final Picasso picasso; | |
public ItemAdapter(Context context, Picasso picasso) { | |
super(context); | |
this.picasso = picasso; | |
} | |
public void replaceWith(List<Item> images) { | |
this.images = images; | |
notifyDataSetChanged(); | |
} | |
@Override | |
public int getCount() { | |
return images.size(); | |
} | |
@Override | |
public Item getItem(int position) { | |
return images.get(position); | |
} | |
@Override | |
public long getItemId(int position) { | |
return position; | |
} | |
@Override | |
public View newView(LayoutInflater inflater, int position, ViewGroup container) { | |
return inflater.inflate(R.layout.leaf, container, false); | |
} | |
@Override | |
public void bindView(Item item, int position, View view) { | |
((ItemView) view).bindTo(item, picasso); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package views; | |
import android.content.Context; | |
import android.util.AttributeSet; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.widget.FrameLayout; | |
import android.widget.ImageView; | |
import android.widget.RelativeLayout; | |
import android.widget.TextView; | |
import com.squareup.picasso.Picasso; | |
import com.squareup.picasso.RequestCreator; | |
import com.teststaggeredgrid.R; | |
import Utils; | |
import butterknife.ButterKnife; | |
import butterknife.InjectView; | |
public class ItemView extends RelativeLayout { | |
@InjectView(R.id.logo_view) MyImageView logo_view; | |
@InjectView(R.id.image_view) MyImageView image_view; | |
@InjectView(R.id.distance) TextView distance; | |
private float imageAspectRatio = 1; | |
private Context mContext; | |
private Item mItem; | |
private int distanceHeight; | |
public ItemView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
mContext = context; | |
} | |
@Override | |
protected void onFinishInflate() { | |
super.onFinishInflate(); | |
ButterKnife.inject(this); | |
} | |
public void bindTo(Item item, Picasso picasso) { | |
this.mItem = item; | |
if(item.getImage() != null) { | |
image_view.bindTo(item.getImage(), picasso, R.id.logo); | |
} | |
if(item.getLogo() != null) { | |
logo_view.bindTo(item.getLogo(), picasso, R.id.image); | |
} | |
distance.setText(String.valueOf(mItem.getDistance()) + " miles away"); | |
requestLayout(); | |
} | |
@Override | |
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { | |
int mode = MeasureSpec.getMode(widthMeasureSpec); | |
int textHeight = 0; | |
if (mode != MeasureSpec.EXACTLY) { | |
throw new IllegalStateException("layout_width must be match_parent"); | |
} | |
int columnWidth = MeasureSpec.getSize(widthMeasureSpec); | |
// Honor aspect ratio for height but no larger than 2x width. | |
int imageHeight = image_view.getMeasuredHeight(); | |
if(mStore.getDistance() != 0.0) { | |
textHeight = Utils.getTextHeight(mContext, String.valueOf(mItem.getDistance()) + " miles away", distance.getTextSize(), columnWidth); | |
} | |
heightMeasureSpec = MeasureSpec.makeMeasureSpec(imageHeight, MeasureSpec.EXACTLY); | |
int textHeightMeasureSpec = MeasureSpec.makeMeasureSpec(textHeight, MeasureSpec.EXACTLY); | |
super.onMeasure(widthMeasureSpec, heightMeasureSpec + textHeightMeasureSpec); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<views.ItemView | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
android:id="@+id/panel_content" | |
android:orientation="vertical" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content"> | |
<views.MyImageView | |
android:id="@+id/logo_view" | |
android:layout_marginBottom="1dp" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_alignParentTop="true" | |
android:layout_centerHorizontal="true"> | |
<views.ForegroundImageView | |
android:id="@+id/logo" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:layout_centerHorizontal="true" | |
android:adjustViewBounds="true"/> | |
</views.MyImageView> | |
<views.MyImageView | |
android:id="@+id/image_view" | |
android:layout_width="match_parent" | |
android:layout_below="@id/logo_view" | |
android:layout_height="wrap_content"> | |
<views.ForegroundImageView | |
android:id="@+id/image" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:layout_centerHorizontal="true" | |
android:adjustViewBounds="true"/> | |
</views.MyImageView> | |
<TextView | |
android:id="@+id/distance" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:textSize="14sp" | |
android:textColor="@color/secondary_title_color" | |
android:layout_below="@id/image_view" | |
/> | |
</views.ItemView> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package views; | |
import android.content.Context; | |
import android.util.AttributeSet; | |
import android.widget.FrameLayout; | |
import android.widget.ImageView; | |
import com.squareup.picasso.Picasso; | |
import com.squareup.picasso.RequestCreator; | |
import com.teststaggeredgrid.R; | |
import butterknife.ButterKnife; | |
import butterknife.InjectView; | |
/** | |
* Created by amcintyre on 8/27/2014. | |
*/ | |
public class MyImageView extends FrameLayout{ | |
private ForegroundImageView imageView; | |
private float imageAspectRatio = 1; | |
private RequestCreator imageRequest; | |
public MyImageView(Context context) { | |
super(context); | |
} | |
public MyImageView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
} | |
public MyImageView(Context context, AttributeSet attrs, int defStyle) { | |
super(context, attrs, defStyle); | |
} | |
@Override | |
protected void onFinishInflate() { | |
super.onFinishInflate(); | |
} | |
public void bindTo(MyImage item, Picasso picasso, int viewId) { | |
imageView = ButterKnife.findById(this, viewId); | |
picasso.setLoggingEnabled(true); | |
imageRequest = picasso.load(item.getUrl()); | |
imageAspectRatio = 1f * item.getWidth() / item.getHeight(); | |
requestLayout(); | |
} | |
@Override | |
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { | |
int mode = MeasureSpec.getMode(widthMeasureSpec); | |
if (mode != MeasureSpec.EXACTLY) { | |
throw new IllegalStateException("layout_width must be match_parent"); | |
} | |
int columnWidth = MeasureSpec.getSize(widthMeasureSpec); | |
// Honor aspect ratio for height but no larger than 2x width. | |
int imageHeight = (int) (columnWidth / imageAspectRatio); | |
heightMeasureSpec = MeasureSpec.makeMeasureSpec(imageHeight, MeasureSpec.EXACTLY); | |
super.onMeasure(widthMeasureSpec, heightMeasureSpec); | |
if (imageRequest != null) { | |
imageRequest.resize(columnWidth, imageHeight).into(imageView); | |
imageRequest = null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment