Recyclerview更新所有视图

时间:2017-11-17 01:03:18

标签: android firebase android-recyclerview

我有一个包含多种视图类型的recyclerview。基本上,onclick按钮,我想循环遍历所有recyclerview的子项(包括屏幕上看不到的视图)并将其保存到Firebase。然后,我想从Firebase获取该信息,并使用该数据重新填充我的recyclerview。我该怎么做?我的问题是在recyclerview上调用getChildCount()只会获取屏幕上的可见子项。但是,我需要所有的孩子。我是以正确的方式来做这件事的吗?或者我应该在屏幕上显示数据时使用数据填充我的视图?

澄清代码

这是我用来获取数据的内容,以便我可以将其插入Firebase

int numberOfViews = displayWorkoutAdapter.rowTypeList.size() - 1;
                                    Log.d("numberOfViews", String.format("%s", numberOfViews));
                                    for (int a = 0; a < numberOfViews; a++) {
                                        RowType rowType = displayWorkoutAdapter.rowTypeList.get(a);
                                        Log.d("rowTypeClass", rowType.getClass().toString());

                                        if (rowType.getClass() == ExerciseModel.class) {
                                            ExerciseModel exerciseModel = (ExerciseModel) rowType;
                                            String exerciseString = exerciseModel.getExercise();
                                            String weightString = exerciseModel.getWeight();
                                            String setString = exerciseModel.getSets();
                                            String repsString = exerciseModel.getReps();
                                            Log.d("exerciseString", exerciseString);


                                            insertDataToTable(tableName, exerciseView, null, exerciseString, weightString, setString, repsString, null, null, "0");

                                        }

OnBindViewHolder

    @Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position){

    if(holder instanceof ExerciseRowHolder) {
        ((ExerciseRowHolder) holder).exercise.setText(((ExerciseModel) rowTypeList.get(position)).getExercise());
        ((ExerciseRowHolder) holder).weight.setText(((ExerciseModel) rowTypeList.get(position)).getWeight());
        ((ExerciseRowHolder) holder).set.setText(((ExerciseModel) rowTypeList.get(position)).getSets());
        ((ExerciseRowHolder) holder).rep.setText(((ExerciseModel) rowTypeList.get(position)).getReps());
        ((ExerciseRowHolder) holder).dragHandle.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    mDragStartListener.onStartDrag(holder);
                }
                return false;
            }
        });
        ((ExerciseRowHolder) holder).exercise.setAdapter(getExerciseAdapter(home.getContext()));
    }

ExerciseRowHolder类

    public static class ExerciseRowHolder extends RecyclerView.ViewHolder{

    public ImageView dragHandle;
    public EditText weight;
    public EditText set;
    public EditText rep;
    public AutoCompleteTextView exercise;
    public ExerciseRowTextListener exerciseRowTextListener;

    public ExerciseRowHolder(View view, ExerciseRowTextListener exerciseRowTextListener){
        super(view);

        dragHandle = view.findViewById(R.id.reorder);
        weight = view.findViewById(R.id.weight);
        set = view.findViewById(R.id.sets);
        rep = view.findViewById(R.id.reps);
        exercise = view.findViewById(R.id.autoCompleteTextView);


    }
}

2 个答案:

答案 0 :(得分:0)

adapter.notifyDataSetChanged()是活动中更改所有数据的方法...您可以在活动中执行所有firebase操作,然后您可以调用此方法..这将更新所有数据。

答案 1 :(得分:0)

将数据存储在适配器而不是视图中,请参阅:RecyclerView Creating Lists and Cards

  1.   

    onclick按钮,我想循环遍历所有recyclerview的子项(包括屏幕上看不到的视图)并将其保存到Firebase。

  2. 要循环浏览所有recyclerview的数据,而不是使用recyclerview.getChildCount(),请使用adapter.getItemCount()

    1.   

      然后,我想从Firebase获取该信息,并使用该数据重新填充我的recyclerview。我该怎么做呢?

    2. 从firebase获取所有数据后,将数据存储在适配器中。然后使用adapter.notifyDataSetChanged()

      示例:

      你的适配器:     公共类YourAdapter扩展了RecyclerView.Adapter {      private ArrayList mData;

      public AppPickerGridListAdapter(@NonNull Context context, ArrayList<ExerciseModel> apps) {
          mData = apps;
      }
      @Override
      public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
         // create a new view
          View v = (View) LayoutInflater.from(parent.getContext())
                  .inflate(R.layout.your_view_layout, parent, false);
          // set the view's size, margins, paddings and layout parameters
          ...
          ExerciseRowHolder vh = new ExerciseRowHolder(v, exerciseRowHolderListener);
          vh..dragHandle.setOnTouchListener(new View.OnTouchListener() {
              @Override
              public boolean onTouch(View v, MotionEvent event) {
                  if (event.getAction() == MotionEvent.ACTION_DOWN) {
                      mDragStartListener.onStartDrag(holder);
                  }
                  return false;
              }
          });
          return vh;
      }
      
      @Override
      public void onBindViewHolder(ViewHolder holder, int position) {
          ExerciseModel item = mData.get(position);
          ((ExerciseRowHolder) holder).exercise.setText(item.getExercise());
          ((ExerciseRowHolder) holder).weight.setText(item.getWeight());
          ((ExerciseRowHolder) holder).set.setText(item.getSets());
          ((ExerciseRowHolder) holder).rep.setText(item.getReps());
      
      }
      
      @Override
      public int getItemCount() {
          return mData.size();
      }
      
      public ExerciseModel getItem(int position) {
         return mData.get(position);
      }
      
      public ArrayList<ExerciseModel> getData() {
          return mData;
      }
      
      public void setData(ArrayList<ExerciseModel> data) {
         mData = data;
         notifyDataSetChanged();
      }
      }
      

      点击保存按钮后的活动:

      ArrayList<ExerciseModel> data = mAdapter.getData();
      // save this data to firebase
      

      从firebase获取数据后:

      ArrayList<ExcerciseModal> data = getFirebaseData();
      mAdapter.setData(data);
      

      希望有所帮助