将RecyclerView重置为其初始onCreate状态

时间:2020-05-14 16:47:24

标签: java android performance android-studio android-recyclerview

我有一个简单的应用,可以在RecyclerView中显示文本。每次fab都会在recyclerview列表中添加新词。

我现在的问题是,当我在选项菜单中单击重置时,将RecyclerView重置为其原始状态(首次创建活动时)。我尝试了类似的方法,例如mRecyclerView.invalidate();,然后在 mAdapter 上调用notifydataSetChanged();。还使用getRecycledViewPool().clear();。全部无济于事。

这是我的MainActivity代码:

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);
    }
}

这是我的适配器代码:

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();
        }
    }
}

其他:

有关如何有效管理点击的任何帮助(而不必处理适配器中的每个项目点击? 谢谢

1 个答案:

答案 0 :(得分:1)

回收站根据您的WordList显示视图。

因此要重置,请通过mWordList.clear();清除LinkedList中的数据,并通过调用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);
}

额外

  1. 请勿使用LinkedList存储数据。相反,您可以使用适合大多数用例的ArrayList