Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A RecyclerView Adapter for a simple list in Android
package com.yourcompany.yourapp.adapter;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.yourcompany.yourapp.R;
import java.util.ArrayList;
public class SimpleListAdapter extends RecyclerView.Adapter<SimpleListAdapter.SimpleViewHolder> {
public static class SimpleViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
TextView textView;
SimpleViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
itemView.setOnLongClickListener(this);
textView = (TextView) itemView.findViewById(R.id.item_simple_text1);
}
@Override
public void onClick(View v) {
// The user may not set a click listener for list items, in which case our listener
// will be null, so we need to check for this
if (mOnEntryClickListener != null) {
mOnEntryClickListener.onEntryClick(v, getPosition());
}
}
@Override
public boolean onLongClick(View v) {
if (mOnEntryLongClickListener != null) {
mOnEntryLongClickListener.onEntryLongClick(v, getPosition());
return true;
}
return false;
}
}
private ArrayList<String> mArrayList;
public SimpleListAdapter(ArrayList<String> arrayList) {
mArrayList = arrayList;
}
@Override
public int getItemCount() {
return mArrayList.size();
}
@Override
public SimpleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_simple, parent, false);
return new SimpleViewHolder(view);
}
@Override
public void onBindViewHolder(SimpleViewHolder holder, int position) {
holder.textView.setText(mArrayList.get(position));
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
private static OnEntryClickListener mOnEntryClickListener;
public interface OnEntryClickListener {
void onEntryClick(View view, int position);
}
public void setOnEntryClickListener(OnEntryClickListener onEntryClickListener) {
mOnEntryClickListener = onEntryClickListener;
}
private static OnEntryLongClickListener mOnEntryLongClickListener;
public interface OnEntryLongClickListener {
void onEntryLongClick(View view, int position);
}
public void setOnEntryLongClickListener(OnEntryLongClickListener onEntryLongClickListener) {
mOnEntryLongClickListener = onEntryLongClickListener;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/item_simple_text1"
android:textColor="#DE000000"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:paddingLeft="6dp"
android:paddingRight="6dp"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:background="?android:attr/selectableItemBackground" />
</LinearLayout>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.