Skip to content

Commit

Permalink
Loading random images
Browse files Browse the repository at this point in the history
  • Loading branch information
CullyCross committed Nov 5, 2015
1 parent 91363a2 commit 49678be
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 12 deletions.
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

<uses-permission android:name="android.permission.CALL_PHONE" />

<uses-permission android:name="android.permission.INTERNET" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package me.cullycross.test4tabs.adapters;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import butterknife.Bind;
import butterknife.ButterKnife;
import com.squareup.picasso.Picasso;
import java.util.Random;
import me.cullycross.test4tabs.R;

/**
* Created by: Anton Shkurenko (cullycross)
* Project: Test4tabs
* Date: 11/5/15
* Code style: SquareAndroid (https://github.com/square/java-code-styles)
* Follow me: @tonyshkurenko
*/
public class DoggyAdapter extends RecyclerView.Adapter<DoggyAdapter.DogItem> {

private static final String URL = "http://lorempixel.com/200/200/food/%d/%s";

private final Context mContext;
private final Random mRandom;

private int mCount = 50;

public DoggyAdapter(Context ctx) {
super();
mContext = ctx;
mRandom = new Random();
}

@Override public DogItem onCreateViewHolder(ViewGroup viewGroup, int viewType) {

View v = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.recycler_view_item_picture, viewGroup, false);

return new DogItem(v);
}

@Override public void onBindViewHolder(DogItem holder, final int position) {

Picasso.with(mContext)
.load(String.format(URL, mRandom.nextInt(10) + 1, "Position " + position))
.fit()
.centerCrop().placeholder(R.mipmap.ic_launcher).into(holder.mImage);
}

@Override public int getItemCount() {
return mCount;
}

public static class DogItem extends RecyclerView.ViewHolder {

@Bind(R.id.image) ImageView mImage;

public DogItem(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import butterknife.Bind;
import butterknife.ButterKnife;
import me.cullycross.test4tabs.R;
import me.cullycross.test4tabs.adapters.DoggyAdapter;

public class PicturesFragment extends Fragment {

@Bind(R.id.recycler_view_images) RecyclerView mRecyclerViewImages;

public static PicturesFragment newInstance() {
final PicturesFragment fragment = new PicturesFragment();
final Bundle args = new Bundle();
Expand All @@ -26,6 +32,15 @@ public PicturesFragment() {
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_pictures, container, false);
View view = inflater.inflate(R.layout.fragment_pictures, container, false);
ButterKnife.bind(this, view);

mRecyclerViewImages.setAdapter(new DoggyAdapter(getContext()));
return view;
}

@Override public void onDestroyView() {
super.onDestroyView();
ButterKnife.unbind(this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package me.cullycross.test4tabs.views;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;

/**
* Created by: Anton Shkurenko (cullycross)
* Project: Test4tabs
* Date: 11/5/15
* Code style: SquareAndroid (https://github.com/square/java-code-styles)
* Follow me: @tonyshkurenko
*
* see <a href="https://github.com/chiuki/android-recyclerview/blob/master/app/src/main/java/com/sqisland/android/recyclerview/AutofitRecyclerView.java">
* this</>
*/
public class AutofitRecyclerView extends RecyclerView {
private GridLayoutManager mManager;
private int mColumnWidth = -1;

public AutofitRecyclerView(Context context) {
super(context);
init(context, null);
}

public AutofitRecyclerView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}

public AutofitRecyclerView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context, attrs);
}

private void init(Context context, AttributeSet attrs) {
if (attrs != null) {
int[] attrsArray = {
android.R.attr.columnWidth
};
TypedArray array = context.obtainStyledAttributes(attrs, attrsArray);
mColumnWidth = array.getDimensionPixelSize(0, -1);
array.recycle();
}

mManager = new GridLayoutManager(getContext(), 1);
setLayoutManager(mManager);
}

@Override protected void onMeasure(int widthSpec, int heightSpec) {
super.onMeasure(widthSpec, heightSpec);
if (mColumnWidth > 0) {
int spanCount = Math.max(1, getMeasuredWidth() / mColumnWidth);
mManager.setSpanCount(spanCount);
}
}
}
18 changes: 7 additions & 11 deletions app/src/main/res/layout/fragment_pictures.xml
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
<me.cullycross.test4tabs.views.AutofitRecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/recycler_view_images"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="me.cullycross.test4tabs.fragments.PicturesFragment">

<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment"/>

</FrameLayout>
android:columnWidth="100dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
/>
8 changes: 8 additions & 0 deletions app/src/main/res/layout/recycler_view_item_picture.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/image"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_margin="4dp"
android:src="@mipmap/ic_launcher"
/>

0 comments on commit 49678be

Please sign in to comment.