Skip to content

Instantly share code, notes, and snippets.

@douglasjunior
Last active September 10, 2015 00:33
Show Gist options
  • Save douglasjunior/934775f2ac8753ff2490 to your computer and use it in GitHub Desktop.
Save douglasjunior/934775f2ac8753ff2490 to your computer and use it in GitHub Desktop.
Exemplo de uso do Adapter com RecyclerView
import android.content.Context;
import android.content.res.TypedArray;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import java.util.ArrayList;
import java.util.List;
/**
* Adapter genérico.
*
* Created by douglas on 25/08/15.
*/
public abstract class AbstractAdapter<VH extends AbstractAdapter.AbstractViewHolder, I> extends RecyclerView.Adapter<VH> {
protected final Context mContext;
protected List<I> mItems;
protected final LayoutInflater mLayoutInflater;
public AbstractAdapter(Context context) {
this(context, new ArrayList<I>());
}
public AbstractAdapter(Context context, List<I> items) {
mContext = context;
mItems = items;
mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public void onBindViewHolder(final VH holder, final int position) {
if (position >= 0 && position < mItems.size()) {
final I item = mItems.get(position);
onBindViewHolder(holder, position, item);
}
}
protected abstract void onBindViewHolder(VH holder, int position, I item);
@Override
public int getItemCount() {
return mItems.size();
}
public void setItems(List<I> items) {
mItems = items;
}
public List<I> getItems() {
return mItems;
}
protected void onItemClickListener(int adapterPosition, int layoutPosition) {
}
protected boolean onLongItemClickListener(int adapterPosition, int layoutPosition) {
return false;
}
public class AbstractViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
public AbstractViewHolder(View itemView) {
super(itemView);
itemView.setClickable(true);
itemView.setOnClickListener(this);
itemView.setOnLongClickListener(this);
if (itemView instanceof FrameLayout) {
int[] textSizeAttr = new int[]{android.R.attr.selectableItemBackground};
int indexOfAttr = 0;
TypedArray a = mContext.obtainStyledAttributes(textSizeAttr);
((FrameLayout) itemView).setForeground(a.getDrawable(indexOfAttr));
a.recycle();
}
}
@Override
public void onClick(View v) {
onItemClickListener(getAdapterPosition(), getLayoutPosition());
}
@Override
public boolean onLongClick(View v) {
return onLongItemClickListener(getAdapterPosition(), getLayoutPosition());
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<!-- Exemplo de XML de layout com Card para utilizar como Item do Adapter -->
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="4dp"
app:cardCornerRadius="4dp"
app:cardElevation="3dp"
app:cardPreventCornerOverlap="true"
app:cardUseCompatPadding="false" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:orientation="horizontal"
android:padding="24dp">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="@+id/tv_nome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="Carlos Alberto"
android:textColor="#000"
android:textSize="@dimen/recyclerview_primary_text" />
<TextView
android:id="@+id/tv_funcao"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="Tratorista"
android:textColor="#80000000"
android:textSize="@dimen/recyclerview_secondary_text" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingStart="24dp">
<TextView
android:id="@+id/tv_cod"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="1"
android:textColor="@color/colorAccent"
android:textSize="@dimen/recyclerview_secondary_text" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import com.projeto.pacote.R;
import com.projeto.pacote.model.Empregado;
import java.util.List;
/**
* Exemplo de Activity com instanciação do Adapter
*
* Created by douglas on 26/08/15.
*/
public class EmpregadoActivity extends AppCompatActivity {
private RecyclerView rvEmpregados;
private EmpregadosAdapter empregadosAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.empregado_activity);
rvEmpregados = (RecyclerView) findViewById(R.id.rv_empregados);
final StaggeredGridLayoutManager llm = new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL);
llm.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);
rvEmpregados.setLayoutManager(llm);
empregadosAdapter = new EmpregadosAdapter(this, getEmpregados()) {
@Override
protected void onItemClickListener(int adapterPosition, int layoutPosition) {
// evento de click simples
}
@Override
protected boolean onLongItemClickListener(int adapterPosition, int layoutPosition) {
// evento e click longo
return true;
}
};
rvEmpregados.setAdapter(empregadosAdapter);
}
public List<Empregado> getEmpregados() {
// select * from empregados
}
}
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.projeto.pacote.R;
import com.projeto.pacote.model.Empregado;
import java.util.List;
/**
* Exemplo de implementação do Adapter para a classe Empregado.
*
* Created by douglas on 18/08/15.
*/
public class EmpregadosAdapter extends AbstractAdapter<EmpregadosAdapter.ViewHolder, Empregado> {
public EmpregadosAdapter(Context context) {
super(context);
}
public EmpregadosAdapter(Context context, List<Empregado> items) {
super(context, items);
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = mLayoutInflater.inflate(R.layout.card_empregado_item, parent, false);
ViewHolder vh = new ViewHolder(v);
return vh;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position, Empregado item) {
holder.tvNome.setText(item.getNome());
holder.tvFuncao.setText(item.getFuncao());
holder.tvCod.setText(item.getCod() + "");
}
public class ViewHolder extends AbstractAdapter.AbstractViewHolder {
final TextView tvNome;
final TextView tvFuncao;
final TextView tvCod;
public ViewHolder(View view) {
super(view);
tvCod = (TextView) view.findViewById(R.id.tv_cod);
tvNome = (TextView) view.findViewById(R.id.tv_nome);
tvFuncao = (TextView) view.findViewById(R.id.tv_funcao);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment