home top ad

How to Refresh a list Using RecyclerView in Android Studio

Hello friends today we will learn how to refresh a list using RecyclerView in android studio.
Whenever we want to refresh the data in the folder, we use swiperefresh layout.Whenever we put any data in a folder, then it is not visible in the folder, then we have to refresh that folder.Let's see how we will implement it in our project.

activity_main

<?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.swiperefreshlayout.widget.SwipeRefreshLayout
        android:id="@+id/swipeLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>


</androidx.constraintlayout.widget.ConstraintLayout>
In this layout we have used Recyclerview inside SwipeRefreshLayout which we will give them an id to use.

MainActivity
package com.example.swiperefreshlayout;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.os.Bundle;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
  private  RecyclerAdapter recyclerAdapter;
  private RecyclerView recyclerView;
  private List<String>listFile;
  private SwipeRefreshLayout swipeRefreshLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listFile=new ArrayList<>();
        recyclerView=findViewById(R.id.recyclerView);
        recyclerAdapter=new RecyclerAdapter(listFile);
        recyclerView.setAdapter(recyclerAdapter);
        DividerItemDecoration dividerItemDecoration=new DividerItemDecoration(this,DividerItemDecoration.VERTICAL);
        recyclerView.addItemDecoration(dividerItemDecoration);
        listFile.add("CodingHubExpress");
        listFile.add("Java");
        listFile.add("Kotlin");
        listFile.add("JavaScript");
        listFile.add("C#");
        listFile.add("C");
        listFile.add("C++");
        listFile.add("Python");
        listFile.add("R");
        listFile.add("CSS");
        listFile.add("Android");
        listFile.add("Ruby");
        swipeRefreshLayout=findViewById(R.id.swipeLayout);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                listFile.add("Swift");
                listFile.add("Dart");
                listFile.add("Go");
                listFile.add("Logo");
                listFile.add("React");
                listFile.add("Data Structure");
                recyclerAdapter.notifyDataSetChanged();
                swipeRefreshLayout.setRefreshing(false);
            }
        });

    }
}
In this class we have used Recyclerview to show all the data in list by using RecyclerAdapter.

RecyclerAdapter
package com.examples.swiperefreshlayout;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
    List<String>arrayList;

    public RecyclerAdapter(List<String> arrayList) {
        this.arrayList = arrayList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater=LayoutInflater.from(parent.getContext());
        View view=layoutInflater.inflate(R.layout.item,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

      holder.textView.setText(arrayList.get(position));
    }

    @Override
    public int getItemCount() {
        return arrayList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private TextView textView;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            textView=itemView.findViewById(R.id.textView);

              itemView.setOnLongClickListener(new View.OnLongClickListener() {
                  @Override
                  public boolean onLongClick(View view) {
                      arrayList.remove(getAdapterPosition());
                      notifyItemChanged(getAdapterPosition());
                      return true;
                  }
              });

        }

        @Override
        public void onClick(View view) {
            Toast.makeText(view.getContext(), arrayList.get(getAdapterPosition())+"", Toast.LENGTH_SHORT).show();
        }
    }

}


item

<?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"
    android:layout_margin="20dp">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher_round"/>

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="show text here"
        android:textStyle="italic"
        android:layout_centerInParent="true"/>


</RelativeLayout>
Using this layout we have used this layout to show the item.

I hope this article helpfull for you
Thank you

Post a Comment

0 Comments