Skip to content

Instantly share code, notes, and snippets.

@rogerhu
Last active May 5, 2017 20:57
Show Gist options
  • Save rogerhu/8f71319d46c613ceb2a107c8bf1a1b28 to your computer and use it in GitHub Desktop.
Save rogerhu/8f71319d46c613ceb2a107c8bf1a1b28 to your computer and use it in GitHub Desktop.
package codepath.com.recyclerviewfun;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class Contact {
private int mId;
private String mName;
private boolean mOnline;
public Contact(int id, String name, boolean online) {
mId = id;
mName = name;
mOnline = online;
}
public int getId() {
return mId;
}
public String getName() {
return mName;
}
public boolean isOnline() {
return mOnline;
}
private static int lastContactId = 0;
public static void resetContactId() {
lastContactId = 0;
}
public static List<Contact> createContactsList(int numContacts, int offset) {
List<Contact> contacts = new ArrayList<Contact>();
for (int i = 1; i <= numContacts; i++) {
contacts.add(new Contact(i + offset, randomIdentifier(), i <= numContacts / 2));
}
return contacts;
}
public static String randomIdentifier() {
final java.util.Random rand = new java.util.Random();
// class variable
final String lexicon = "ABCDEFGHIJKLMNOPQRSTUVWXYZ12345674890";
// consider using a Map<String,Boolean> to say whether the identifier is being used or not
final Set<String> identifiers = new HashSet<String>();
StringBuilder builder = new StringBuilder();
while(builder.toString().length() == 0) {
int length = rand.nextInt(5)+5;
for(int i = 0; i < length; i++) {
builder.append(lexicon.charAt(rand.nextInt(lexicon.length())));
}
if(identifiers.contains(builder.toString())) {
builder = new StringBuilder();
}
}
return builder.toString();
}
}
package codepath.com.recyclerviewfun;
import android.content.Context;
import android.support.v7.util.DiffUtil;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import java.util.List;
// Create the basic adapter extending from RecyclerView.Adapter
// Note that we specify the custom ViewHolder which gives us access to our views
public class ContactsAdapter extends
RecyclerView.Adapter<ContactsAdapter.ViewHolder> {
// Store a member variable for the contacts
private List<Contact> mContacts;
// Pass in the contact array into the constructor
public ContactsAdapter(List<Contact> contacts) {
mContacts = contacts;
}
// Provide a direct reference to each of the views within a data item
// Used to cache the views within the item layout for fast access
public static class ViewHolder extends RecyclerView.ViewHolder {
// Your holder should contain a member variable
// for any view that will be set as you render a row
public TextView nameTextView;
public Button messageButton;
// We also create a constructor that accepts the entire item row
// and does the view lookups to find each subview
public ViewHolder(View itemView) {
// Stores the itemView in a public final member variable that can be used
// to access the context from any ViewHolder instance.
super(itemView);
nameTextView = (TextView) itemView.findViewById(R.id.contact_name);
//messageButton = (Button) itemView.findViewById(R.id.message_button);
}
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
View contactView = inflater.inflate(R.layout.item_contact, parent, false);
ViewHolder viewHolder = new ViewHolder(contactView);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
Contact contact = mContacts.get(position);
TextView textView = viewHolder.nameTextView;
textView.setText(contact.getName());
Button button = viewHolder.messageButton;
if (position % 2 == 0) {
textView.setBackground(viewHolder.nameTextView.getContext().getResources().getDrawable(R.color.colorPrimaryDark));
} else {
textView.setBackground(viewHolder.nameTextView.getContext().getResources().getDrawable(R.color.colorAccent));
}
}
@Override
public int getItemCount() {
return mContacts.size();
}
}
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/contact_name"
android:background="@color/colorAccent"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="0dp"
android:paddingBottom="5dp"
android:layout_marginRight="0dp" android:layout_marginTop="0dp"
app:layout_constraintHorizontal_bias="0.0" app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/guideline2"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_default="wrap"
tools:text="CONSTRAINT LAYOUT Y U NO WORK !!!!"/>
<android.support.constraint.Guideline
android:id="@+id/guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.6"/>
</android.support.constraint.ConstraintLayout>
package codepath.com.recyclerviewfun;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import java.util.List;
public class MainActivity extends AppCompatActivity {
List<Contact> allContacts;
ContactsAdapter adapter;
RecyclerView rvItems;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
rvItems = (RecyclerView) findViewById(R.id.rvContacts);
allContacts = Contact.createContactsList(10, 0);
adapter = new ContactsAdapter(allContacts);
rvItems.setAdapter(adapter);
final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
rvItems.setLayoutManager(linearLayoutManager);
}
public void clearItems(View view) {
allContacts.clear();
Contact.resetContactId();
allContacts.addAll(Contact.createContactsList(10, 0));
adapter.notifyDataSetChanged();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment