Last active
August 29, 2015 14:04
-
-
Save tyvsmith/70524a6b42962be4fbf8 to your computer and use it in GitHub Desktop.
Android ListAdapter using custom ViewGroup over ViewHolder pattern
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.example.app; | |
public class App { | |
public String id; | |
public String name; | |
public String icon_url; | |
@Override | |
public String toString() { | |
return name; | |
} | |
} |
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"?> | |
<com.example.app.AppItemView xmlns:android="http://schemas.android.com/apk/res/android" | |
android:orientation="horizontal" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:minHeight="?android:attr/listPreferredItemHeight" | |
android:paddingStart="?android:attr/listPreferredItemPaddingStart" | |
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"> | |
<ImageView | |
android:id="@+id/app_icon" | |
android:layout_width="@dimen/list_detail_image_size" | |
android:layout_height="@dimen/list_detail_image_size" | |
/> | |
<LinearLayout | |
android:layout_width="0dp" | |
android:layout_weight="1" | |
android:layout_height="wrap_content" | |
android:layout_marginLeft="8dp" | |
android:orientation="vertical"> | |
<TextView | |
android:id="@+id/app_name" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_marginTop="8dip" | |
android:textAppearance="?android:attr/textAppearanceListItem" | |
/> | |
</LinearLayout> | |
</com.example.app.AppItemView> |
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.example.app; | |
import android.content.Context; | |
import android.util.AttributeSet; | |
import android.widget.ImageView; | |
import android.widget.TextView; | |
import com.squareup.picasso.Picasso; | |
import com.twitter.crashlytics.R; | |
import com.twitter.crashlytics.app.ItemView; | |
import com.twitter.crashlytics.models.App; | |
import butterknife.InjectView; | |
public class AppItemView extends ItemView<App> { | |
@InjectView(R.id.app_icon) protected ImageView appIcon; | |
@InjectView(R.id.app_name) protected TextView appName; | |
public AppItemView(Context context) { | |
super(context); | |
} | |
public AppItemView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
} | |
public AppItemView(Context context, AttributeSet attrs, int defStyle) { | |
super(context, attrs, defStyle); | |
} | |
protected void showItem(App app) { | |
this.model = app; | |
Picasso.with(getContext()).load(model.icon_url).into(appIcon); | |
appName.setText(model.name); | |
} | |
} |
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.example.app; | |
import android.content.Context; | |
import android.util.AttributeSet; | |
import android.widget.LinearLayout; | |
public abstract class ItemView<Model> extends LinearLayout { | |
protected Model model = null; | |
public ItemView(Context context) { | |
super(context); | |
} | |
public ItemView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
} | |
public ItemView(Context context, AttributeSet attrs, int defStyle) { | |
super(context, attrs, defStyle); | |
} | |
protected abstract void showItem(Model model); | |
} |
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.example.app; | |
import android.content.Context; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.BaseAdapter; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class ModelAdapter<Model> extends BaseAdapter { | |
protected final Context context; | |
protected final int layoutId; | |
public List<Model> elements = new ArrayList<Model>(); | |
public ModelAdapter(Context context, int layoutId) { | |
this.context = context; | |
this.layoutId = layoutId; | |
} | |
@Override | |
public int getCount() { | |
return elements.size(); | |
} | |
@Override | |
public Model getItem(int position) { | |
return elements.get(position); | |
} | |
@Override | |
public long getItemId(int position) { | |
return position; | |
} | |
@Override | |
public View getView(int position, View convertView, ViewGroup parent) { | |
ItemView<Model> view; | |
if (convertView == null) { | |
view = (ItemView<Model>) LayoutInflater.from(context).inflate(layoutId, parent, false); | |
} else { | |
view = (ItemView<Model>) convertView; | |
} | |
Model t = getItem(position); | |
view.showItem(t); | |
return view; | |
} | |
public void setData(List<Model> elements) { | |
this.elements = elements; | |
notifyDataSetChanged(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment