滚动到最后一个位置时,RecyclerView中的notifyDataSetChanged()错误

时间:2019-06-20 18:50:48

标签: java android android-recyclerview recycler-adapter

我正在使用RecyclerView显示行列表,每行都有一个购买按钮。当用户购买元素时,将调用方法notifydatasetchanged(),并且“购买”按钮必须将背景颜色更改为例如红色。

重要,用户只需要购买一个元素,我就有一个线程每5秒运行一次,以寻找新的答案,如果有新的答案,它将添加到列表中并滚动到最后一个位置。

我的问题是,当我有按钮列表时,只有一个为红色,并且有一个新答案,我将其添加到列表中,但是某些行的按钮变为红色。

这是我的代码:

private void responses() {
 fin_thread = true;
 thread = new Thread() {
  @Override
  public void run() {
   while (fin_thread) {
    try {
     Thread.sleep(5000);
     runOnUiThread(new Runnable() {
      @Override
      public void run() {
       JsonObjectRequest service_call = new JsonObjectRequest(Request.Method.GET, url, null,
        new Response.Listener < JSONObject > () {
         @Override
         public void onResponse(JSONObject response) {
          try {
           boolean status = response.getBoolean("value_status_response");
           if (status) {
            if (response.getInt("counter") > 0 && obj_response.size() > 0) {
             JSONArray answer = response.getJSONArray("answer");
             if (answer.length() != obj_response.size()) {
              for (int i = 0; i < answer.length(); i++) {
               boolean coincidence = false;;
               JSONObject item = answer.getJSONObject(i);
               for (int j = 0; j < obj_response.size(); j++) {
                if (item.getId("id") == obj_response.get(j).getId()) {
                 coincidence = true;
                 break;
                }
               }
               if (!coincidence) {
                CResponse cr = new CResponse();
                cr.setName(item.getString("name"));
                cr.setPrice(item.getString("price"));
                cr.setId(item.getInt("id"));
                cr.setState(item.getInt("state"));
                obj_response.add(cr);
                sectionAdapter.notifyDataSetChanged();
                scroll_to_last();
               }
              }
             }
            } else {
             if (response.getInt("counter") > 0 && obj_response.size() == 0) {
              JSONArray answer = response.getJSONArray("answer");
              for (int i = 0; i < answer.length(); i++) {
               JSONObject item = answer.getJSONObject(i);
               CResponse cr = new CResponse();
               cr.setName(item.getString("name"));
               cr.setPrice(item.getString("price"));
               cr.setId(item.getInt("id"));
               cr.setState(item.getInt("state"));
               obj_response.add(cr);
               sectionAdapter.notifyDataSetChanged();
               rv.setVisibility(View.VISIBLE);
               progress_bar.setVisibility(View.GONE);
              }
             }
            }
           }
          } catch (JSONException e) {
           e.printStackTrace();
          }
         }
        },
        new Response.ErrorListener() {
         @Override
         public void onErrorResponse(VolleyError error) {
          NetworkResponse networkResponse = error.networkResponse;
          if (networkResponse != null) {
           //ERROR
          }
         }
        }
       ) {
        @Override
        public Map < String, String > getHeaders() throws AuthFailureError {
         Map < String, String > params = new HashMap < String, String > ();
         params.put("Authorization", "Basic " + auth);
         return params;
        }
       };
       Helpers.queue(Class.this).add(service_call);
      }
     });
    } catch (InterruptedException e) {
     e.printStackTrace();
    }
   }
  }
 };
 thread.start();
}

private void scroll_to_last() {
 lm.scrollToPosition(sectionAdapter.getItemCount() - 1);
}
My Adapter

@Override
public void onBindItemViewHolder(RecyclerView.ViewHolder holder, final int position) {
 final MyViewPedidoDesc itemHolder = (MyViewPedidoDesc) holder;
 control_btn_background(itemHolder, position);
}

private void control_btn_background(MyViewPedidoDesc itemHolder, int position) {
 if (mData.get(position).getState() == 1) {
  itemHolder.buy_btn.setBackgroundResource(R.drawable.red);
 } else {
  itemHolder.buy_btn.setBackgroundResource(R.drawable.blue);
 }
}

我认为错误是我调用scroll_to_last()方法时出现的,因为如果我不调用它,背景色就只有一个按钮。

感谢advace

0 个答案:

没有答案