简体   繁体   English

RecyclerView 视图绑定闪烁且不始终呈现 Android MVVM

[英]RecyclerView view binding flickering & not rendering consistently Android MVVM

I'm using view binding w/ MVVM pattern in my Android app.我在我的 Android 应用程序中使用带有 MVVM 模式的视图绑定。 From the home activity, the user can click on an item which will open a fragment with items displayed in a recycler view.从主页活动中,用户可以单击一个项目,该项目将打开一个片段,其中项目显示在回收站视图中。 The first time the fragment is rendered the recycler view flickers on and then disappears all together.第一次渲染片段时,回收器视图会闪烁,然后一起消失。 If I go back to the home activity and select the same item, the recycler view displays as expected without any issues.如果我 go 回到主页活动和 select 相同的项目,回收站视图按预期显示,没有任何问题。

This is my call for the recycler view data:这是我对回收站视图数据的呼吁:

private void getData() {
    titlesViewModel.titlesListLiveData.observe(getViewLifecycleOwner(), titles -> {
        titlesList.clear();

        for (Titles title : titles) {
            titlesList.add(title);
        }

        binding.rvTitles.setAdapter(TitlesAdapter);
        TitlesAdapter.setTitlesList(titlesList);
    });
    titlesViewModel.getAllTitles(Tag);
}

This is my recycler view adapter:这是我的回收站视图适配器:

public class TitlesAdapter extends RecyclerView.Adapter<TitlesAdapter.TitlesViewHolder> {
    private ItemTitleBinding binding;
    private ArrayList<Titles> titles;
    private Context context;

    public ShowTitlesAdapter(Context context, ArrayList<Titles> titles){
        this.context = context;
        this.titles = titles;
    }

    @NonNull
    @Override
    public TitlesViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(context);
        binding = ItemTitleBinding.inflate(inflater, parent, false);
        return new TitlesViewHolder(binding);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void onBindViewHolder(@NonNull TitlesViewHolder holder, int position) {
        holder.binding.titleNo.setText(Integer.toString(titles.get(position).getNumber()));
        holder.binding.titleName.setText(titles.get(position).getName());
    }

    @Override
    public int getItemCount() {
        if(titles != null){
            return titles.size();
        }
        return 0;
    }

    public class TitlesViewHolder extends RecyclerView.ViewHolder {

        private ItemTitleBinding binding;

        public TitlesViewHolder(ItemTitleBinding binding) {
            super(binding.getRoot());
            this.binding = binding;

        }
    }

    public void setTitlesList(ArrayList<Titles> titles){
        this.titles = titles;
        notifyDataSetChanged();
    }
  }

I found this , but they're not having issues with a first time render.我发现了这个,但他们在第一次渲染时没有问题。 Has anyone experienced this issue or know what I'm doing wrong?有没有人遇到过这个问题或知道我做错了什么?

You can try this code, where you initialise your adapter.您可以尝试使用此代码来初始化适配器。

adapter.setHasStableIds(true)

And use this in adapter.并在适配器中使用它。 In this method you have to return your ListItem id.在此方法中,您必须返回您的 ListItem id。

override fun getItemId(position: Int): Long {
    if (profileList.size>0){
        return profileList[position].profileId.toLong()
    }else{
        return super.getItemId(position)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM