Skip to content

Instantly share code, notes, and snippets.

@anandrex5
Created June 1, 2022 19:39
Show Gist options
  • Save anandrex5/a64bfdadb28845f04847ce8e3bf1252a to your computer and use it in GitHub Desktop.
Save anandrex5/a64bfdadb28845f04847ce8e3bf1252a to your computer and use it in GitHub Desktop.
RecyclerView_Impl_with_GUI
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffff"
android:id="@+id/recyclerview"
>
</androidx.recyclerview.widget.RecyclerView>
</androidx.constraintlayout.widget.ConstraintLayout>
package com.example.recyclerview3;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
//1- extends RecyclerView.Adapter<Adapter.ViewHolder>
public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {
private List<ModelClass> userList;
//4 - Make Constructor
public Adapter (List<ModelClass>userList){this.userList = userList; }
//2 - implements methods OnCreateViewHolder , OnBindViewHolder ,
@NonNull
@Override
public Adapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_design,parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull Adapter.ViewHolder holder, int position) {
//5- declare variable and get the values.
//i take i image view1 and 1 textview 1 so i declare it one one time. It may many which your takes
//in your xml file (i,e - item.design)
int resource = userList.get(position).getImageview1();
String name = userList.get(position).getTextview1();
//6 - Now sending our data to our Holder
//Note we pass only resource, name because we take 2 data in item_design.xml.It may many which you
//take in item_design.xml
holder.setData(resource,name); //12- shift+enter click on setData and create method automatically.
}
@Override
public int getItemCount() {
//7 - Return userList size
return userList.size();
}
//3 - Create class of extended ViewHolder i.e ( public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder>
public class ViewHolder extends RecyclerView.ViewHolder{ //8- extends RecyclerView.ViewHolder
//10- Simply declare an variable for our 1st imageview
//i declare 2 items because i implements 2 items in item_design.xml
private ImageView imageView;
private TextView textView;
//9- Create Constructor matching super (hold cursor on red line)
public ViewHolder(@NonNull View itemView) {
super(itemView);
//11- Here we assign our xml id into java ids.
//i assign 2 data , because i use 2 items in item_design.xml
imageView=itemView.findViewById(R.id.imageview1);
textView=itemView.findViewById(R.id.textview1);
}
//12- a - created through setData (shift+enter)
public void setData(int resource, String name) {
///12- b - set data into these variables
// i set 2 data only bcz i use only 2 data in item_design.xml
imageView.setImageResource(resource);
textView.setText(name);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.recyclerview3">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.RecyclerView3"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
plugins {
id 'com.android.application'
}
android {
compileSdk 32
defaultConfig {
applicationId "com.example.recyclerview3"
minSdk 21
targetSdk 32
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.6.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'androidx.recyclerview:recyclerview:1.3.0-alpha02'
implementation 'androidx.cardview:cardview:1.0.0'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="95dp"
android:background="#ffffff">
///for insert images we use CardView
<androidx.cardview.widget.CardView
android:id="@+id/imageview"
android:layout_width="60dp"
android:layout_height="60dp"
app:cardCornerRadius = "60dp"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp">
<ImageView
android:id="@+id/imageview1"
android:layout_width="60dp"
android:layout_height="60dp"
android:scaleType="centerCrop"
android:src="@drawable/img1"/>
></androidx.cardview.widget.CardView>
<TextView
android:id="@+id/textview1"
android:layout_width="120dp"
android:layout_height="30dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:layout_toRightOf="@+id/imageview"
android:text="Aradhana"
android:textColor="#000"
android:textSize="20sp" />
</RelativeLayout>
</RelativeLayout>
package com.example.recyclerview3;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
//1- we have to declare some variables
RecyclerView recyclerView;
//2- declare variable for Linear Layout
LinearLayoutManager layoutManager;
List<ModelClass>userList;
Adapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//3- set our data RecyclerView for this we have to create function.
initData(); //5- Create method from it.
initRecyclerView(); //4- Create method from it.
}
//5 - a- method
private void initData() {
//5 - b-
userList = new ArrayList<>();
//5- c- give parameter for display in our project, ( data you use in item_design data , like image1, Text ).
userList.add(new ModelClass(R.drawable.img1,"Aradhana"));
userList.add(new ModelClass(R.drawable.img2,"Aradhana"));
userList.add(new ModelClass(R.drawable.img3,"Aradhana"));
userList.add(new ModelClass(R.drawable.img4,"Aastha"));
userList.add(new ModelClass(R.drawable.img5,"Mine"));
userList.add(new ModelClass(R.drawable.img6,"Ankita and Aastha"));
userList.add(new ModelClass(R.drawable.img7,"Nikki"));
userList.add(new ModelClass(R.drawable.img8,"Gang"));
userList.add(new ModelClass(R.drawable.img9,"Squad"));
}
//4 - a - method
private void initRecyclerView() {
//4 - b-
recyclerView=findViewById(R.id.recyclerview);
layoutManager= new LinearLayoutManager(this);
layoutManager.setOrientation(RecyclerView.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
adapter=new Adapter(userList);
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
package com.example.recyclerview3;
public class ModelClass {
//1- declare that items use in xml
private int imageview1;
private String textview1;
//3-Constructor
public ModelClass(int imageview1, String textview1) {
this.imageview1 = imageview1;
this.textview1 = textview1;
}
//2-getter and setter
public int getImageview1() {
return imageview1;
}
public void setImageview1(int imageview1) {
this.imageview1 = imageview1;
}
public String getTextview1() {
return textview1;
}
public void setTextview1(String textview1) {
this.textview1 = textview1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment