从 RecyclerView 迁移到 PagedListAdapter,提交列表不显示数据 (Android Studio JAVA)

Migration from RecyclerView to PagedListAdapter, data is not shown with submitList (Android Studio JAVA)

我刚刚实现了 Android 个组件的新架构,Room、ViewModel、LiveData、Retrofit 和 RecyclerView,所以好吧,问题是我在 recyclerview 中显示的列表呈指数级增长,因为它是聊天的原型,应用程序可能会冻结一点。

在网上搜索我找到了关于 PagedList Adapter 的文档,这可能是一个可能的解决方案,问题是在 mainActivity 中,当我调用观察者时,我应该把 adapter.submitList(message), (在onchange函数里面), 我没有显示数据

MainActivity.class

public class MainActivity extends AppCompatActivity {

    public static final int NEW_WORD_ACTIVITY_REQUEST_CODE = 1;

    private MenViewModel mWordViewModel;
     MensajesAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        RecyclerView recyclerView = findViewById(R.id.recyclerview);
        adapter = new MensajesAdapter( getApplicationContext());


        mWordViewModel = 
        ViewModelProviders.of(this).get(MenViewModel.class);


        recyclerView.setHasFixedSize(true);
        mWordViewModel.usersList.observe(this, mensajes -> {
          adapter.submitList(mensajes);
        });

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);

奇怪的是,如果我通过 adapter.setwords(参数)修改 submitList 代码,如果显示列表,当然,它会使用 notifydatasetchange() 并且效率不高

PagedListAdapter-->MensajesAdapter.class

import android.arch.paging.PagedListAdapter;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.util.DiffUtil;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

public class MensajesAdapter extends PagedListAdapter<Mensajes,MensajesAdapter.MensajesViewHolder> {

    private Context mCtx;
    private List<Mensajes> heroList;

    MensajesAdapter( Context context) {
        super(DIFF_CALLBACK);
        this.mCtx=context;
    }


    **void setWords(List<Mensajes> words){
        heroList = words;
        notifyDataSetChanged();
    }**


    @NonNull
    @Override
    public MensajesViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mCtx).inflate(R.layout.recyclerview_layout, parent, false);
        return new MensajesViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MensajesViewHolder mensajesViewHolder, int i) {
        Mensajes men =getItem(i);
        if(men!=null) {

            mensajesViewHolder.textView.setText(men.getMensaje());
        }else{
            mensajesViewHolder.clear();
        }
    }

    @Override
    public int getItemCount() {
        if (heroList != null)
            return heroList.size();
        else return 0;
    }
    public static DiffUtil.ItemCallback<Mensajes> DIFF_CALLBACK = new DiffUtil.ItemCallback<Mensajes>() {
        @Override
        public boolean areItemsTheSame(@NonNull Mensajes oldItem, @NonNull Mensajes newItem) {
            Log.e("areITems",""+(oldItem.getId() == newItem.getId()));
            return oldItem.getId() == newItem.getId();
        }
        @Override
        public boolean areContentsTheSame(@NonNull Mensajes oldItem, @NonNull Mensajes newItem) {
            Log.e("areITems",""+(oldItem.getMensaje().equals(newItem.getMensaje())));
            return oldItem.getMensaje().equals(newItem.getMensaje());
        }
    };
    class MensajesViewHolder extends RecyclerView.ViewHolder {
        TextView textView;

        public MensajesViewHolder(View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.mensaje);
        }
        void clear()
        {
            textView.invalidate();
        }


    }

}

ViewModel-->MenViewModel.class

public class MenViewModel extends AndroidViewModel {

    public MensajesDao countryDao;
    private MenRepository mRepository;
    // Using LiveData and caching what getAlphabetizedWords returns has several benefits:
    // - We can put an observer on the data (instead of polling for changes) and only update the
    //   the UI when the data actually changes.
    // - Repository is completely separated from the UI through the ViewModel.
    public LiveData<PagedList<Mensajes>> mAllWords;
    public LivePagedListBuilder livePlyerList;
    public final LiveData<PagedList<Mensajes>> usersList;
    public MenViewModel(Application application) {
        super(application);
        mRepository = new MenRepository(application);
        mAllWords = mRepository.getAllWords();
        MenRoomDatabase countryDatabase = MenRoomDatabase.getDatabase(getApplication());

        countryDao = countryDatabase.mensajeDao();
        //mRepository.recargar();
        usersList =new LivePagedListBuilder<>(
                countryDao.getAllMensajes(), /* page size */ 20).build();
        livePlyerList=new LivePagedListBuilder<>(countryDatabase.mensajeDao().getAllMensajes(),new PagedList.Config.Builder()
                .setPageSize(20)
                .setPrefetchDistance(10)
                .setPageSize(20)
                .build());
    }

    LiveData<PagedList<Mensajes>> getAllWords() { return mAllWords; }

    public void insert(Mensajes mensaje) { mRepository.insert(mensaje); }

谁知道告诉我哪里出了问题?我缺少或拥有什么?

确保 countryDao.getAllMensajes() 包含在 DataSource.Factory<Int, Mensajes> 中,并且在您的 Adapter 中您不应该手动管理列表,因为您使用的是 PagedListAdapter.

在你的 @Override public int getItemCount() {} 中做:

    @Override
    public int getItemCount() {
        return super.getItemCount()
    }