Last active
September 4, 2015 10:08
-
-
Save tyvsmith/1611751e8322587d9f36 to your computer and use it in GitHub Desktop.
Composed View Holder with decoupled OnClickListener
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.ui.utils; | |
import android.content.Context; | |
import android.support.v7.widget.RecyclerView; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import java.util.ArrayList; | |
import java.util.List; | |
import static com.example.util.Preconditions.checkPositionIndex; | |
public abstract class ModelAdapter<Model, VH extends ModelViewHolder<Model>> | |
extends RecyclerView.Adapter<VH> { | |
private List<Model> data = new ArrayList<>(); | |
private final int layoutId; | |
private Context context; | |
protected OnItemClickListener<Model> clickListener; | |
public interface OnItemClickListener<Model> { | |
void onItemClick(View view, int position, Model model); | |
} | |
public ModelAdapter(int layoutId) { | |
this.layoutId = layoutId; | |
} | |
protected abstract VH createViewHolder(View view); | |
@Override | |
public VH onCreateViewHolder(final ViewGroup parent, final int viewType) { | |
final View view = LayoutInflater | |
.from(parent.getContext()) | |
.inflate(layoutId, parent, false); | |
context = parent.getContext(); | |
VH holder = createViewHolder(view); | |
holder.setOnItemClickListener(clickListener); | |
return holder; | |
} | |
@Override | |
public int getItemCount() { | |
return data.size(); | |
} | |
@Override | |
public void onBindViewHolder(VH holder, final int position) { | |
checkPositionIndex(position, data.size()); | |
Model model = data.get(position); | |
holder.setModel(model); | |
} | |
public void setData(List<Model> data) { | |
this.data = data; | |
notifyDataSetChanged(); | |
} | |
public void setOnItemClickListener(OnItemClickListener<Model> listener) { | |
this.clickListener = listener; | |
} | |
protected Context getContext() { | |
return this.context; | |
} | |
} |
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.ui.utils; | |
import android.support.v7.widget.RecyclerView; | |
import android.view.View; | |
public abstract class ModelViewHolder<Model> extends RecyclerView.ViewHolder | |
implements View.OnClickListener { | |
private ModelAdapter.OnItemClickListener<Model> clickListener; | |
private Model model; | |
public ModelViewHolder(View itemView) { | |
super(itemView); | |
itemView.setOnClickListener(this); | |
} | |
void setModel(Model model) { | |
this.model = model; | |
bindModel(model); | |
} | |
protected abstract void bindModel(Model model); | |
public void onClick(View view) { | |
if (clickListener != null) { | |
clickListener.onItemClick(view, getAdapterPosition(), model); | |
} | |
} | |
void setOnItemClickListener(ModelAdapter.OnItemClickListener listener) { | |
this.clickListener = listener; | |
} | |
} |
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
// This class is generated. Do not modify! | |
package com.example.data.api.model; | |
import com.google.gson.annotations.SerializedName; | |
public final class Test { | |
@SerializedName("title") | |
public final String title; | |
@SerializedName("subtitle") | |
public final String subtitle; | |
public Test( | |
String title, | |
String subtitle, | |
) { | |
this.title = title; | |
this.subtitle = subtitle; | |
} | |
} |
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.ui.test; | |
import android.view.View; | |
import android.widget.TextView; | |
import com.example.R; | |
import com.example.data.api.model.Test; | |
import com.example.ui.utils.ModelViewHolder; | |
import com.example.ui.utils.ModelAdapter; | |
import butterknife.Bind; | |
import butterknife.ButterKnife; | |
public class TestAdapter extends ModelAdapter<Test, TestAdapter.TestViewHolder>{ | |
public TestAdapter() { | |
super(R.layout.test_item); | |
} | |
@Override | |
protected TestViewHolder createViewHolder(View view) { | |
return new TestViewHolder(view); | |
} | |
public static class TestViewHolder extends ModelViewHolder<Test> { | |
@Bind(R.id.text1) | |
TextView text1; | |
@Bind(R.id.text2) | |
TextView text2; | |
public TestViewHolder(final View itemView) { | |
super(itemView); | |
ButterKnife.bind(this, itemView); | |
} | |
@Override | |
protected void bindModel(Test test) { | |
text1.setText(test.title); | |
text2.setText(test.subtitle); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment