Quwaysim
Quwaysim

Reputation: 407

Resetting RecyclerView to its initail onCreate state

I have an simple app that displays texts in a RecyclerView. The fab adds new words to the recyclerview list each time it'os clicked.

My problem now is resetting the RecyclerView to its original state (when activity was first created) when I click on Reset in the options menu. I have tried different ways like mRecyclerView.invalidate(); then calling notifydataSetChanged(); on the mAdapter. Also using getRecycledViewPool().clear();. All to no avail.

Here's my MainActivity Code:

package com.example.recyclerview;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.recyclerview.adapters.WordListAdapter;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

import java.util.LinkedList;

public class MainActivity extends AppCompatActivity {
    private final LinkedList<String> mWordList = new LinkedList<>();
    private RecyclerView mRecyclerView;
    private WordListAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        mRecyclerView = findViewById(R.id.recyclerView);
        mAdapter = new WordListAdapter(this, mWordList);
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));



        for (int i = 0; i < 20; i++) {
            mWordList.addLast("Word " + i);
        }

        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                int mWordListSize = mWordList.size();
                mWordList.addLast("Word " + mWordListSize);
                mRecyclerView.getAdapter().notifyItemInserted(mWordListSize);
                mRecyclerView.smoothScrollToPosition(mWordListSize);
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.reset) {
            // Resetting the RecyclerView
            mAdapter = new WordListAdapter(this, mWordList);
            mRecyclerView.setAdapter(mAdapter);
            mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

and here'os my Adapter Code:

package com.example.recyclerview.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.recyclerview.R;

import java.util.LinkedList;

public class WordListAdapter extends RecyclerView.Adapter<WordListAdapter.WordViewHolder> {
    private final LinkedList<String> mWordList;
    private LayoutInflater mInflater;

    public WordListAdapter(Context context, LinkedList<String> wordList) {
        mInflater = LayoutInflater.from(context);
        this.mWordList = wordList;
    }

    @NonNull
    @Override
    public WordListAdapter.WordViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = mInflater.inflate(R.layout.wordlist_item, parent, false);
        return new WordViewHolder(itemView, this);
    }

    @Override
    public void onBindViewHolder(@NonNull WordListAdapter.WordViewHolder holder, int position) {
        String mCurrent = mWordList.get(position);
        holder.wordItemView.setText(mCurrent);
    }

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

    class WordViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        public final TextView wordItemView;
        final WordListAdapter mAdapter;

        public WordViewHolder(@NonNull View itemView, WordListAdapter adapter) {
            super(itemView);
            wordItemView = itemView.findViewById(R.id.word);
            this.mAdapter = adapter;
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            int mPosition = getLayoutPosition();
            String element = mWordList.get(mPosition);
            mWordList.set(mPosition, "Clicked" + element);
            mAdapter.notifyDataSetChanged();
        }
    }
}

Extra:

Any help as to how i can manage click efficiently (without having to handle each item click in the adapter?? Thanks

Upvotes: 0

Views: 929

Answers (1)

Pavan Varma
Pavan Varma

Reputation: 1239

Recycler is displaying views depending on your WordList.

So to reset, clear the data in your LinkedList by mWordList.clear(); and notify the adapter that data has changed by calling mAdapter.notifyDataSetChanged();

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.reset) {
        // Resetting the RecyclerView

        // to remove all items
        mWordList.clear();

        // add default items again
        for (int i = 0; i < 20; i++) {
            mWordList.addLast("Word " + I);
        }

        mAdapter.notifyDataSetChanged();

        return true;
    }
    return super.onOptionsItemSelected(item);
}

Extra

  1. Don't use LinkedList for storing data. Instead, you can use an ArrayList which is suitable for most of the use cases.

Upvotes: 1

Related Questions