Skip to content

Commit

Permalink
Use Butterknife in a new viewholder
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Rahn committed Dec 14, 2016
1 parent 53f483d commit 5204248
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 15 deletions.
3 changes: 3 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,8 @@ dependencies {
compile 'com.squareup.retrofit2:converter-jackson:2.0.0-beta3'
compile 'com.squareup.retrofit2:retrofit:2.0.0-beta3'

compile 'com.jakewharton:butterknife:8.4.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'

testCompile 'junit:junit:4.12'
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import net.paddlefish.lemonadestand.model.GameThemeDetails;
import net.paddlefish.lemonadestand.utils.Preferences;

import butterknife.ButterKnife;

import static android.view.View.GONE;
import static android.view.View.VISIBLE;

Expand All @@ -31,25 +33,27 @@ public ThemeAdapter(Activity activity) {
@NonNull
@Override
public View getView(int position, View convertView, @NonNull ViewGroup parent) {
View view = convertView;
ThemeViewHolder view = (ThemeViewHolder) convertView;
if (view == null) {
LayoutInflater inflater = LayoutInflater.from(getContext());
view = inflater.inflate(R.layout.theme_item, parent, false);
view = (ThemeViewHolder) inflater.inflate(R.layout.theme_item, parent, false);
view.bindViews();
}
GameThemeDetails theme = getItem(position);
if (theme != null) {
ImageView iconView = (ImageView) view.findViewById(R.id.themeIcon);
ImageView iconView = view.mIconView;
String url = theme.url;

// FIXME : Use Glide to load that url into iconView
Glide.with(getContext()).load(url).into(iconView);

TextView themeInfoView = (TextView) view.findViewById(R.id.themeInfo);
TextView themeInfoView = view.mInfoView;
themeInfoView.setText(theme.info);

TextView themeNameView = (TextView) view.findViewById(R.id.themeName);
TextView themeNameView = view.mNameView;
themeNameView.setText(theme.name);

ImageView checkedView = (ImageView) view.findViewById(R.id.themeChecked);
ImageView checkedView = view.mCheckedView;
Boolean isCurrentTheme = Preferences.isSelectedTheme(theme);
if (isCurrentTheme) {
checkedView.setVisibility(VISIBLE);
Expand All @@ -60,5 +64,4 @@ public View getView(int position, View convertView, @NonNull ViewGroup parent) {
return view;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package net.paddlefish.lemonadestand.theme;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import net.paddlefish.lemonadestand.R;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
* Simple ViewHolder for Theme ListView
*
* Created by arahn on 12/14/16.
*/
public class ThemeViewHolder extends LinearLayout {
@BindView(R.id.themeIcon)
ImageView mIconView;
@BindView(R.id.themeInfo)
TextView mInfoView;
@BindView(R.id.themeName)
TextView mNameView;

ImageView mCheckedView;

public ThemeViewHolder(Context context) {
super(context);
}

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

void bindViews() {
ButterKnife.bind(this);

// FIXME: Use butterknife to bind mCheckedView to R.id.themeChecked
// Instead of this code:
// Delete from here...
mCheckedView = (ImageView) findViewById(R.id.themeChecked);

// ... to here and replace with your implementation!
}
}
17 changes: 9 additions & 8 deletions app/src/main/res/layout/theme_item.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minHeight="360dp"
android:orientation="vertical">

<net.paddlefish.lemonadestand.theme.ThemeViewHolder xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minHeight="360dp"
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
Expand Down Expand Up @@ -60,4 +61,4 @@
android:id="@+id/themeInfo"
android:text="blah blah blah blah blah blah blah blah"/>

</LinearLayout>
</net.paddlefish.lemonadestand.theme.ThemeViewHolder>

0 comments on commit 5204248

Please sign in to comment.