Adapter implementation
Create a class called RecyclerViewAdapter in the adapter package created earlier. Inside the class, let's create an inner class called ViewHolder for mapping the components created in the each_item.xml layout. ViewHolder will extend RecyclerView.ViewHolder and, inside its constructor, it will map the XML component. The code looks as follows:
public class RecyclerViewAdapter { static class ViewHolder extends RecyclerView.ViewHolder{ private TextView mTextView; public ViewHolder(View view) { super(view); mTextView = (TextView) view.findViewById(R.id.note); } } }
Now, create an interface for the ItemClick events, which returns the position:
public interface ItemSelectedListener { void onItemSelected(int position); }
Inside the ViewHolder class, add the bind method for binding the click event and returning the position:
void bind(final String title, final int position, final ItemSelectedListener listener) { mTextView.setText(title); itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (listener != null) { listener.onItemSelected(position); } } }); }
In the RecyclerViewAdapter class scope, declare the following instances:
private ItemSelectedListener mItemSelectedListener; private List<Note> mListNote = new ArrayList<>();
Set the listner for ItemSelectedListner as follows:
public void setListener(ItemSelectedListener itemSelectedListener) { mItemSelectedListener = itemSelectedListener; }
Now, we need to extend RecyclerViewAdapter to WearableRecyclerView.Adapter with its view holder, as follows:
public class RecyclerViewAdapter extends WearableRecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> { }
We then need to implement the callbacks for the adapter:
- onCreateViewHolder(..);
- onBindViewHolder(..);
- getItemCount(..);
Inside onCreateViewHolder, we should attach or inflate the each_item.xml layout:
@Override public RecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup
parent, int viewType) { View view = LayoutInflater.from(parent.getContext()) .inflate(R.layout.view_holder, parent, false); return new ViewHolder(view); }
Inside onBindViewHolder, we will handle the click event by binding the data and position:
@Override public void onBindViewHolder(ViewHolder holder, int position) { holder.bind(mListNote.get(position).getNotes(), position,
mItemSelectedListener); }
Inside the getItemCount() method, we will return the count of the list item being passed to the adapter:
@Override public int getItemCount() { return mListNote.size(); }
The complete WearableRecyclerView adapter looks as follows:
package com.ashok.packt.wear_note_1.adapter; import android.support.v7.widget.RecyclerView; import android.support.wearable.view.WearableRecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import com.ashok.packt.wear_note_1.R; import com.ashok.packt.wear_note_1.model.Note; import java.util.ArrayList; import java.util.List; /** * Created by ashok.kumar on 15/02/17. */ public class RecyclerViewAdapter extends WearableRecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> { private ItemSelectedListener mItemSelectedListener; private List<Note> mListNote = new ArrayList<>(); static class ViewHolder extends RecyclerView.ViewHolder { private TextView mTextView; ViewHolder(View view) { super(view); mTextView = (TextView) view.findViewById(R.id.note); } void bind(final String title, final int position, final
ItemSelectedListener listener) { mTextView.setText(title); itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (listener != null) { listener.onItemSelected(position); } } }); } } public void setListener(ItemSelectedListener itemSelectedListener)
{ mItemSelectedListener = itemSelectedListener; } public void setListNote(List<Note> listNote) { mListNote.clear(); mListNote.addAll(listNote); notifyDataSetChanged(); } @Override public RecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup
parent, int viewType) { View view = LayoutInflater.from(parent.getContext()) .inflate(R.layout.each_item, parent, false); return new ViewHolder(view); } @Override public void onBindViewHolder(ViewHolder holder, int position) { holder.bind(mListNote.get(position).getNotes(), position,
mItemSelectedListener); } @Override public int getItemCount() { return mListNote.size(); } public interface ItemSelectedListener { void onItemSelected(int position); } }
Now, let's put all these pieces of codes for action in MainActivity.java.