将DiffUtil与LIveData +会议室数据库一起使用的最佳方法?

时间:2019-02-25 15:06:40

标签: performance android-recyclerview recycler-adapter android-room android-livedata

我将Room DatabaseLiveData一起使用,但是我的本地数据库根据我们的要求更新得太快了,同时我不得不重新加载回收视图,而不是调用{{1} }适配器,我正在尝试使用notifyDataSetChanged(),但是崩溃或无法正确加载,这是不确定的。 我正在遵循本教程: Tutorials Link here

MyAdapter:

DiffUtil

观察LiveData

public class SwitchGridAdapter extends RecyclerView.Adapter<SwitchGridAdapter.ViewHolder> {


    private  List<Object> allItemsList;

    private LayoutInflater mInflater;
    private OnItemClickListener mClickListener;
    private  Context context;

    private Queue<List<Object>> pendingUpdates =
            new ArrayDeque<>();

    // data is passed into the constructor
  public SwitchGridAdapter(Context context,List<Appliance> applianceList,List<ZmoteRemote> zmoteRemoteList) {
        this.mInflater = LayoutInflater.from(context);
         this.context = context;

       allItemsList = new ArrayList<>();
        if (applianceList!=null) allItemsList.addAll(applianceList);
        if (zmoteRemoteList!=null)allItemsList.addAll(zmoteRemoteList);

    }

    // inflates the cell layout from xml when needed
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R .layout.switch_grid_item, parent, false);

        return new ViewHolder(view);
    }


    // binds the data to the textview in each cell
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
     // Doing some update with UI Elements
    }

    // total number of cells
    @Override
    public int getItemCount() {
        return allItemsList.size();
    }

    // stores and recycles views as they are scrolled off screen
    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener,View.OnLongClickListener {
        TextView myTextView;
        ImageView imgSwitch;

        ViewHolder(View itemView) {
            super(itemView);
            myTextView = (TextView) itemView.findViewById(R.id.txtSwitchName);
            imgSwitch = (ImageView) itemView.findViewById(R.id.imgSwitchStatus);

            itemView.setOnClickListener(this);

            itemView.setOnLongClickListener(this);

        }

        @Override
        public void onClick(View view) {
           // handling click 
        }

        @Override
        public boolean onLongClick(View view) {
           return true;
    }

    // convenience method for getting data at click position
    Object getItem(int id) {
        return allItemsList.get(id);
    }

    // allows clicks events to be caught
   public void setClickListener(OnItemClickListener itemClickListener) {
        this.mClickListener = itemClickListener;
    }

    // parent activity will implement this method to respond to click events
    public interface OnItemClickListener {
        void onItemClick(View view, int position);
        void onItemLongPressListner(View view, int position);
    }



// ✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅
// From This Line Reloading with Diff Util is Done .
//✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅


    public  void  setApplianceList( List<Appliance> applianceList,List<ZmoteRemote> zmoteRemoteList)
    {

        if (allItemsList == null)
            allItemsList = new ArrayList<>();

        List<Object> newAppliances = new ArrayList<>();
        if (applianceList!=null) newAppliances.addAll(applianceList);

        updateItems(newAppliances);

    }


    // when new data becomes available
    public void updateItems(final List<Object> newItems) {
        pendingUpdates.add(newItems);
        if (pendingUpdates.size() > 1) {
            return;
        }
        updateItemsInternal(newItems);
    }
    // This method does the heavy lifting of
    // pushing the work to the background thread
    void updateItemsInternal(final List<Object> newItems) {
        final List<Object> oldItems = new ArrayList<>(this.allItemsList);
        final Handler handler = new Handler();
        new Thread(new Runnable() {
            @Override
            public void run() {
                final DiffUtil.DiffResult diffResult =
                        DiffUtil.calculateDiff(new DiffUtilHelper(oldItems, newItems));
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        applyDiffResult(newItems, diffResult);
                    }
                });
            }
        }).start();
    }
    // This method is called when the background work is done
    protected void applyDiffResult(List<Object> newItems,
                                   DiffUtil.DiffResult diffResult) {
        dispatchUpdates(newItems, diffResult);
    }
    // This method does the work of actually updating
    // the backing data and notifying the adapter
    protected void dispatchUpdates(List<Object> newItems,
                                   DiffUtil.DiffResult diffResult) {


            // ❌❌❌❌❌❌ Next Line is Crashing the app ❌❌❌❌❌
            pendingUpdates.remove();

            dispatchUpdates(newItems, diffResult);
            if (pendingUpdates.size() > 0) {
                updateItemsInternal(pendingUpdates.peek());
            }
    }

}

0 个答案:

没有答案