How to insert static data in recyclerview | recyclerview simple example in easy way
Step1:- Create xml where you want to show list .in this article we add in fragment
fragment_profile.xml :-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Fragments.Profile_Fragment">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rec1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
Step2:- Create a layout for recyclerview item
horizontal_item_single_row.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:gravity="center"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="@+id/productimage"
android:layout_width="60dp"
android:layout_height="60dp"
android:padding="13dp"
android:src="@drawable/userimage" />
<TextView
android:id="@+id/product_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:singleLine="false"
android:text="Hardwahfduegfheghufehvfehbvfhebvre"
android:textColor="@color/black"
android:textSize="10dp" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Step3:- Now we will create Adapter for recyclerview
Profile_Adapter.java
package com.example.firebasedatabase.Adapters;
import android.content.Context;
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 com.bumptech.glide.Glide;
import com.example.firebasedatabase.R;
public class ProfileAdapter extends RecyclerView.Adapter<ProfileAdapter.holder> {
Context context;
int[] images;
String[] title;
public ProfileAdapter(Context context, int[] images, String[] title) {
this.context = context;
this.images = images;
this.title = title;
}
@NonNull
@Override
public ProfileAdapter.holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.horizontal_item_single_row,parent, false);
return new holder(view);
}
@Override
public void onBindViewHolder(@NonNull ProfileAdapter.holder holder, int position) {
holder.product_title.setText(title[position]);
Glide.with(context).load(images[position]).into(holder.productimage);
}
@Override
public int getItemCount() {
return images.length;
}
public class holder extends RecyclerView.ViewHolder {
ImageView productimage;
TextView product_title;
public holder(@NonNull View itemView) {
super(itemView);
product_title= itemView.findViewById(R.id.product_title);
productimage= itemView.findViewById(R.id.productimage);
}
}
}
Step4:- Profile_Fragment.java
package com.example.firebasedatabase.Fragments;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.firebasedatabase.Adapters.ProfileAdapter;
import com.example.firebasedatabase.R;
import com.example.firebasedatabase.databinding.FragmentProfileBinding;
public class Profile_Fragment extends Fragment {
FragmentProfileBinding binding;
int[] trendingimage ={R.drawable.userimage,R.drawable.mainmenu,R.drawable.userimage,R.drawable.mainmenu,R.drawable.userimage,R.drawable.mainmenu};
String[] title ={"Ashish kumar","AMit kumar","Akash shakya","Shalini kumar","Priya ji","Karishma"};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
binding= FragmentProfileBinding.inflate(inflater,container,false);
horizontalscrolling();
return binding.getRoot();
}
private void horizontalscrolling(){
ProfileAdapter profileAdapter = new ProfileAdapter(getActivity(),trendingimage,title );
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
linearLayoutManager.setOrientation(RecyclerView.HORIZONTAL);
binding.rec1.setLayoutManager(linearLayoutManager);
binding.rec1.setAdapter(profileAdapter);
}
}
For Binding Concept Click Here