Skip to content

Instantly share code, notes, and snippets.

View sprejjs's full-sized avatar
:octocat:

Allan Spreys sprejjs

:octocat:
  • ANZx
  • Sydney/Australia
  • 12:42 (UTC +10:00)
View GitHub Profile
@sprejjs
sprejjs / step10.java
Created August 11, 2017 02:50
Step 10. Attach the new adapter.
recyclerView.setAdapter(new ContactsAdapter(this, contacts));
@sprejjs
sprejjs / step9.java
Created August 11, 2017 02:49
Step 9. Modify `getCount()` method.
@Override
public int getItemCount() {
return this.contacts.size();
}
@sprejjs
sprejjs / step8.java
Created August 11, 2017 02:48
Step 8. Override onBindViewHolder method.
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Contact contact = contacts.get(position);
holder.nameTextView.setText(contact.getName());
holder.mobileTextView.setText(contact.getMobile());
holder.landlineTextView.setText(contact.getLandline());
}
@sprejjs
sprejjs / step7.java
Created August 11, 2017 02:48
Step 7. Override onCreateViewHolder method.
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new ViewHolder(
LayoutInflater
.from(context)
.inflate(R.layout.contact_item, parent, false)
);
}
@sprejjs
sprejjs / step6.java
Created August 11, 2017 02:47
Step 6. Update the constructor.
this.context = context;
@sprejjs
sprejjs / step5.java
Created August 11, 2017 02:46
Step 5. Update the ViewHolder.
public class ViewHolder extends RecyclerView.ViewHolder {
private TextView nameTextView;
private TextView mobileTextView;
private TextView landlineTextView;
public ViewHolder(@NonNull View view) {
super(view);
this.nameTextView = (TextView)view
.findViewById(R.id.name_text_view);
this.mobileTextView = (TextView)view
@sprejjs
sprejjs / step4.java
Created August 11, 2017 02:46
Step 4. Update the adapter.
public class ContactsAdapter extends RecyclerView.Adapter<ContactsAdapter.ViewHolder> {
@sprejjs
sprejjs / step3.java
Created August 11, 2017 02:45
Step 3. Replace ListView with RecyclerView in the Java file.
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.list);
@sprejjs
sprejjs / step2.xml
Created August 11, 2017 02:44
Step 2. Replace ListView with RecyclerView.
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@null"
tools:context="com.spreys.viewholderexample.ListViewActivity" />
@sprejjs
sprejjs / step1.gradle
Created August 11, 2017 02:43
*Step 1*. Import dependency
compile 'com.android.support:recyclerview-v7:25.3.1'