Readdy Write  
0,00 €
Your View Money
Views: Count
Self 20% 0
Your Content 60% 0

Users by Links 0
u1*(Content+Views) 10% 0
Follow-Follower 0
s2*(Income) 5% 0

Count
Followers 0
Login Register as User

Android RecyclerView: complete code sample

02.10.2018 (👁11669)

 

 

The Android RecyclerView is the successor of ListView as a list display item in Android Java.

To display a RecyclerView in an Android Activity page, insert the <RecycleView> tag into the Activity.xml and then create an adapter to load the RecyclerView element and an Item layout that corresponds to a row or grid element , Programming a REcyclerView is a bit more expensive than the predecessor ListView. So here's a complete code example in XML and Java.

 

 

 

1) Insert into the Activity

Just add the view tag to the activity.

<android.support.v7.widget.RecyclerView ..>

The RecyclerView is a newer view element and is therefore integrated with android.support.v7.widget.

In the Grade Scripts module the impements have to be extended.

 

 

<?xml version="1.0" encoding="utf-8"?>
<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="match_parent"
   
tools:context=".MainActivity">

    ..

    <
android.support.v7.widget.RecyclerView
       
android:id="@+id/List_Notes"
       
android:layout_width="240dp"
       
android:layout_height="413dp"
       
android:layout_marginStart="140dp"
       
android:layout_marginTop="84dp"
       
android:clickable="true"
       
android:longClickable="true"
       
android:scrollbars="horizontal|vertical"
       
app:layout_constraintStart_toStartOf="parent"
       
app:layout_constraintTop_toTopOf="parent" />

</
android.support.constraint.ConstraintLayout>

 

 

 

Activity Code

In MainActivity, the RecyclerView is loaded by loading the data into an ArrayList.

Then you hand over the ArrayList your own adapter, which then loads the list.

The adapter must be written separately in an adapter class.

RecyclerView.Adapter adapter = new Adapter_for_RecylerView(data_with_Notes );
list_with_notes.setAdapter(adapter);

package com.codedocu.demo07_recyclerview;

import android.database.Cursor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_main);

       
//--< load_List >--
       
load_RecyclerView_List();
       
//--</ load_List >--
   
}


   
public void load_RecyclerView_List() {
       
//------------< load_RecyclerView_List() >------------
       
RecyclerView list_with_notes=findViewById(R.id.List_Notes);
       
//ArrayList<String> arrayList = new ArrayList<String>();
       
ArrayList<DataModel_Note> data_with_Notes = new ArrayList<DataModel_Note>();


       
//< get Table >
       
data_with_Notes.add(new DataModel_Note(1,"Row 1"));
        data_with_Notes.add(
new DataModel_Note(2,"Row 2"));
        data_with_Notes.add(
new DataModel_Note(3,"Row 3"));
       
//</ get Table >

        //----< load List-View >----
        //< show results >
       
list_with_notes.setLayoutManager(new LinearLayoutManager(this));

        RecyclerView.Adapter adapter =
new Adapter_for_RecylerView(data_with_Notes );
        list_with_notes.setAdapter(adapter);
       
//</ show results >
        //----</ load List-View >----

        //--< onClick listener >--
       
((Adapter_for_RecylerView) adapter).setOnItemClickListener(new Adapter_for_RecylerView.OnItemClickListener() {
           
@Override
           
public void onItemClick(View view, int position) {
               
//------< onItemClick() >--------
                //< get Row_Fields >
               
String sPosition=String.valueOf(position);
                TextView item_lblID=view.findViewById(R.id.
lblID);
                TextView item_lblTitle=view.findViewById(R.id.
lblTitle);
                String sID=item_lblID.getText().toString();
                String sTitle=item_lblTitle.getText().toString();
               
//</ get Row_Fields >

                //-< set active_elements >
//                TextView lblIDNote=findViewById(R.id.IDNote);
//                lblIDNote.setText(sID);
//                EditText edit=findViewById(R.id.editTitle);
//                edit.setText(sTitle);
                //-</ set active_elements >

                //< show >
               
Toast.makeText(MainActivity.this, "position=" + position + " Title=" + sTitle + " ID=" + sID, Toast.LENGTH_SHORT).show();
               
//</ show >
                //------< onItemClick() >--------
           
}
        });
       
//--</ onClick listener >--

        //------------</ load_RecyclerView_List() >------------
   
}

}

 

 

 

 

Adapter code

Java code

The adapter is called with the source data as a parameter and loads at runtime the layout template of an item like a series.

This intermediate step is called ViewHolder.

Then the data is assigned to the individual view elements. It can be made during the runtime adjustments.

package com.codedocu.demo07_recyclerview;

import android.database.Cursor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_main);

       
//--< load_List >--
       
load_RecyclerView_List();
       
//--</ load_List >--
   
}


   
public void load_RecyclerView_List() {
       
//------------< load_RecyclerView_List() >------------
       
RecyclerView list_with_notes=findViewById(R.id.List_Notes);
       
//ArrayList<String> arrayList = new ArrayList<String>();
       
ArrayList<DataModel_Note> data_with_Notes = new ArrayList<DataModel_Note>();


       
//< get Table >
       
data_with_Notes.add(new DataModel_Note(1,"Row 1"));
        data_with_Notes.add(
new DataModel_Note(2,"Row 2"));
        data_with_Notes.add(
new DataModel_Note(3,"Row 3"));
       
//</ get Table >

        //----< load List-View >----
        //< show results >
       
list_with_notes.setLayoutManager(new LinearLayoutManager(this));

        RecyclerView.Adapter adapter =
new Adapter_for_RecylerView(data_with_Notes );
        list_with_notes.setAdapter(adapter);
       
//</ show results >
        //----</ load List-View >----

        //--< onClick listener >--
       
((Adapter_for_RecylerView) adapter).setOnItemClickListener(new Adapter_for_RecylerView.OnItemClickListener() {
           
@Override
           
public void onItemClick(View view, int position) {
               
//------< onItemClick() >--------
                //< get Row_Fields >
                
String sPosition=String.valueOf(position);
                TextView item_lblID=view.findViewById(R.id.
lblID);
                TextView item_lblTitle=view.findViewById(R.id.
lblTitle);
                String sID=item_lblID.getText().toString();
                String sTitle=item_lblTitle.getText().toString();
               
//</ get Row_Fields >

                //-< set active_elements >
//                TextView lblIDNote=findViewById(R.id.IDNote);
//                lblIDNote.setText(sID);
//                EditText edit=findViewById(R.id.editTitle);
//                edit.setText(sTitle);
                //-</ set active_elements >

                //< show >
               
Toast.makeText(MainActivity.this, "position=" + position + " Title=" + sTitle + " ID=" + sID, Toast.LENGTH_SHORT).show();
               
//</ show >
                //------< onItemClick() >--------
           
}
        });
       
//--</ onClick listener >--

        //------------</ load_RecyclerView_List() >------------
   
}

}

 

 

 

 

 

Data Class

The data is best structured in a data class. Here are simply data summarized as notes consist of title and text and a date

 

package com.codedocu.demo07_recyclerview;

import java.util.Date;

public class DataModel_Note {
   
//----------< DataModel: Note >------------
    //< inner >
   
public Long IDNote;
   
public String Title;
   
public String Text;
   
public Date dtNote;
   
public Date dtEdit;
   
//</ inner >

   
public DataModel_Note(long IDNote, String sTitle)
    {
       
this.IDNote=IDNote;
       
this.Title=sTitle;
    }
   
//----------< DataModel: Note >------------
}