Last active
December 17, 2015 22:30
-
-
Save farbodsz/226f44657a41c31c486b to your computer and use it in GitHub Desktop.
A RecyclerView Adapter for a simple list in Android
This file contains hidden or 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.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; | |
} | |
} |
This file contains hidden or 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"?> | |
<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