Once you have transferred an APK file to your Android device its stored in /data/app/<package-name>
The classes.dex file is extracted from it and it’s converted to native library, when the App is run the first time, and stored in /data/dalvik-cache. This Machine Code can be run by the CPU.
Each Android device runs a process called Zygote. When an App needs to run Zygote creates a forked version of itself. Which means its a process in memory. Using this forked process and by loading the native library the App can be loaded pretty quickly.
If you are familiar with the Java Virtual Machine(JVM) then you will know that a JVM works on class files which are comprised of byte code. A class file is a compiled version of the Java code.
When it comes to Android then the Java code is first compiled to a class file which in turn is converted to a file called classes.dex – Dalvik Executable format. This is done by a tool called dx
Like JVM in Android’s case its Dalvik which works on the dex file.
A JVM is stack based processor whereas Dalvik is register based. Both are virtual processors though.
The classes.dex file is then compressed with a bunch of other files, like resource and data files, into a ZIP file called an Application Package or APK
This is the file you will eventually upload to the Google Play Store.
You are familiar with how to respond to user clicks in a ListView using an OnItemClickListener. RecyclerView does not have a similar set of built in functionality so you will have to write some code to handle user clicks.
The code you will add will be in the Adapter’s onBindViewHolder. You will also add an interface, similar to the ay you did for Fragment interaction.
Follow the steps below:
Add a Listener interface to the Adapter.
public static interface Listener{
public void onClick(int position);
}
Add a setListener method. This method will be called from the Activity.
public void setListener(Listener listener) {
mListener = listener;
}
So far you have created an Adapter and added a View Holder and also code to onBindViewHolder.
Now it’s time to create a Recycler View which will use this Adapter. It will also pass data to this Adapter.
In order to do this you can create a new Activity, for the purpose of understanding. Let’s call this MaterialActivity. You will call this from the MainActivity on click of a button.
In the layout file for the new Activity you need to add the RecyclerView.
To create a basic Adapter which can be used by the RecyclerView create a new class and extend RecyclerView.Adapter
Once you do this you need to override the following three methods
The getItemCount method is used to return the number of items in the data set.
The onCreateViewHolder method is used to create the views.
The onBindViewHolder method is used to bind views to data.
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return null;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
}
@Override
public int getItemCount() {
return 0;
}