【问题标题】:RecyclerView not displayed even after setting the adapter即使设置了适配器,RecyclerView 也不显示
【发布时间】:2020-12-02 14:39:18
【问题描述】:

我陷入了这个问题,我不知道这有什么问题。我正在尝试使用 RecyclerView 并使用 Firebase 实时数据库中的数据填充它。我已经设置了我的适配器,但问题是 onBindViewHolder 并且 onCreateViewHolder 显然没有被调用。这是代码

WorkoutAdapter.java

public class WorkoutAdapter extends RecyclerView.Adapter<WorkoutAdapter.WorkoutViewHolder> {
private List<Workout> workoutList;

public WorkoutAdapter(List<Workout> workoutList) {
    this.workoutList = workoutList;
}

public WorkoutAdapter() {
}

public List<Workout> getWorkoutList() {
    return workoutList;
}

public void setWorkoutList(List<Workout> workoutList) {
    this.workoutList = workoutList;
}

@NonNull
@Override
public WorkoutViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    Log.i("DEBUG MENU", "onCreateViewHolder");
    LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
    ItemWorkoutBinding binding = ItemWorkoutBinding.inflate(layoutInflater, parent, false);
    return new WorkoutViewHolder(binding);
}

@Override
public void onBindViewHolder(@NonNull WorkoutViewHolder holder, int position) {
    Log.i("DEBUG MENU", "onBindViewHolder");
    Workout workout = workoutList.get(position);
    holder.bind(workout);
}

@Override
public int getItemCount() {
    return workoutList.size();
}

class WorkoutViewHolder extends RecyclerView.ViewHolder {
    private ItemWorkoutBinding binding;

    public WorkoutViewHolder(ItemWorkoutBinding binding) {
        super(binding.getRoot());
        this.binding = binding;
    }

    public void bind(Workout workout) {
        binding.setWorkout(workout);
        binding.executePendingBindings();
    }
}

WorkoutFragment.java

public class WorkoutPlanFragment extends Fragment {

private WorkoutPlanViewModel mViewModel;
private WorkoutPlanFragmentBinding binding;
private DatabaseReference mDatabase;
Plan plan;
List<Workout> workoutList;

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mDatabase = FirebaseDatabase.getInstance().getReference();
}

@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                         @Nullable Bundle savedInstanceState) {
    WorkoutPlanViewModelFactory workoutPlanViewModelFactory = new WorkoutPlanViewModelFactory(workoutList);
    mViewModel = new ViewModelProvider(this, workoutPlanViewModelFactory).get(WorkoutPlanViewModel.class);
    binding = DataBindingUtil.inflate(inflater, R.layout.workout_plan_fragment, container, false);
    binding.setViewmodel(mViewModel);
    View view = binding.getRoot();
    assert getArguments() != null;
    plan = WorkoutPlanFragmentArgs.fromBundle(getArguments()).getPlan();
    ((AppCompatActivity) getActivity()).getSupportActionBar().setTitle(plan.getPlan_name());
    Glide.with(binding.getRoot().getContext()).load(plan.getUri()).into(binding.imageView2);
    return view;
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    binding.setPlan(plan);
    setupRvWorkout();
}

public void setupRvWorkout() {
    RecyclerView recyclerView = binding.workoutRV;
    LinearLayoutManager layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
    recyclerView.setLayoutManager(layoutManager);
    mDatabase.child("exercise_workout").child("data").orderByChild("plan_id").equalTo(plan.getPlan_key()).addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            workoutList = new ArrayList<>();
            for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                String workout_id = snapshot.child("workout_id").getValue(String.class);
                mDatabase.child("workout").child(workout_id).addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot snapshot) {
                        Workout workout = snapshot.getValue(Workout.class);
                        workout.setWorkout_id(workout_id);
                        Log.d("GET", "workout_name: " + workout.getWorkout_name() + " " + workout.getWorkout_id());
                        workoutList.add(workout);
                    }

                    @Override
                    public void onCancelled(@NonNull DatabaseError error) {

                    }
                });
            }
            WorkoutAdapter adapter = new WorkoutAdapter();
            recyclerView.setAdapter(adapter);
            adapter.setWorkoutList(workoutList);
            adapter.notifyDataSetChanged();
        }

        @Override
        public void onCancelled(@NonNull DatabaseError error) {

        }
    });

}

就是这样,提前谢谢你!

编辑:将来自 firebase 的数据添加到锻炼列表中,证明如下:

data added to the list

exercise_plan collection

exercise_workout collection

workout collection

item_workout.xml

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/txt_workout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp"
            android:fontFamily="@font/roboto_medium"
            android:text="@{workout.workout_name}"
            android:textSize="24sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.0" />
    </androidx.constraintlayout.widget.ConstraintLayout>

    <data>

        <variable
            name="workout"
            type="com.example.zahfitclient.model.Workout" />
    </data>
</layout>

workout_plan_fragment.xml

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/frameLayout2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".ui.workoutplan.WorkoutPlanFragment">

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="0dp"
            android:layout_height="180dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:src="@tools:sample/avatars" />

        <TextView
            android:id="@+id/txt_plannameWO"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:fontFamily="@font/roboto_slab_bold"
            android:text="@{plan.plan_name}"
            android:textSize="24sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/imageView2" />

        <TextView
            android:id="@+id/txt_planlevelWO"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="16dp"
            android:fontFamily="@font/roboto_medium"
            android:text="@{plan.level_name}"
            android:textColor="?android:attr/textColorSecondary"
            app:layout_constraintEnd_toStartOf="@+id/view"
            app:layout_constraintHorizontal_bias="1.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/txt_plannameWO" />

        <TextView
            android:id="@+id/txt_plantypeWO"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginTop="8dp"
            android:fontFamily="@font/roboto_medium"
            android:text="@{plan.type_name}"
            android:textColor="?android:attr/textColorSecondary"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toEndOf="@+id/view"
            app:layout_constraintTop_toBottomOf="@+id/txt_plannameWO" />

        <View
            android:id="@+id/view"
            android:layout_width="2dp"
            android:layout_height="20dp"
            android:layout_marginStart="204dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="204dp"
            android:background="@color/black"
            android:backgroundTint="@color/material_on_surface_emphasis_medium"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/txt_plannameWO" />

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/workoutRV"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_marginTop="8dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/view" />

    </androidx.constraintlayout.widget.ConstraintLayout>

    <data>

        <variable
            name="plan"
            type="com.example.zahfitclient.model.Plan" />

        <variable
            name="viewmodel"
            type="com.example.zahfitclient.ui.workoutplan.WorkoutPlanViewModel" />
    </data>
</layout>

【问题讨论】:

  • 介意分享你的xml文件吗?
  • 请编辑您的问题并将您的数据库结构添加为 JSON 文件或至少是屏幕截图。请回复@AlexMamo
  • 我已经编辑了问题@AlexMamo
  • @SamirSpahic ii 已经编辑过了

标签: java android firebase firebase-realtime-database android-recyclerview


【解决方案1】:

notifyDataSetChanged 的调用需要在内部onDataSetChanged 内,因为您可以在其中将项目添加到视图以workoutList.add(workout) 显示的列表中:

mDatabase.child("exercise_workout").child("data").orderByChild("plan_id").equalTo(plan.getPlan_key()).addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        workoutList = new ArrayList<>();
        for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
            String workout_id = snapshot.child("workout_id").getValue(String.class);
            mDatabase.child("workout").child(workout_id).addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot snapshot) {
                    Workout workout = snapshot.getValue(Workout.class);
                    workout.setWorkout_id(workout_id);
                    Log.d("GET", "workout_name: " + workout.getWorkout_name() + " " + workout.getWorkout_id());
                    workoutList.add(workout);
                    adapter.notifyDataSetChanged();
                }

                @Override
                public void onCancelled(@NonNull DatabaseError error) {
                    throw error.toException(); // don't ignore errors
                }
            });
        }
        WorkoutAdapter adapter = new WorkoutAdapter();
        recyclerView.setAdapter(adapter);
        adapter.setWorkoutList(workoutList);
    }

    @Override
    public void onCancelled(@NonNull DatabaseError error) {
        throw error.toException(); // don't ignore errors
    }
});

您可以稍微简化一下,只需在所有数据加载之外构建适配器:

WorkoutAdapter adapter = new WorkoutAdapter();
recyclerView.setAdapter(adapter);
workoutList = new ArrayList<>();
adapter.setWorkoutList(workoutList);
mDatabase.child("exercise_workout").child("data").orderByChild("plan_id").equalTo(plan.getPlan_key()).addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        workoutList.clear();
        for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
            String workout_id = snapshot.child("workout_id").getValue(String.class);
            mDatabase.child("workout").child(workout_id).addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot snapshot) {
                    Workout workout = snapshot.getValue(Workout.class);
                    workout.setWorkout_id(workout_id);
                    Log.d("GET", "workout_name: " + workout.getWorkout_name() + " " + workout.getWorkout_id());
                    workoutList.add(workout);
                    adapter.notifyDataSetChanged();
                }

                @Override
                public void onCancelled(@NonNull DatabaseError error) {
                    throw error.toException(); // don't ignore errors
                }
            });
        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError error) {
        throw error.toException(); // don't ignore errors
    }
});

这更清楚地表明您可以在有数据更新时操作workoutList,只要在需要更新 UI 时调用notifyDataSetChanged()


另见:

【讨论】:

    【解决方案2】:

    你需要改变这个:

    WorkoutAdapter adapter = new WorkoutAdapter();
    recyclerView.setAdapter(adapter);
    adapter.setWorkoutList(workoutList);
    adapter.notifyDataSetChanged();
    

    到这里:

    WorkoutAdapter adapter = new WorkoutAdapter();
    adapter.setWorkoutList(workoutList);
    recyclerView.setAdapter(adapter);
    adapter.notifyDataSetChanged();
    

    并在 onDataChange 方法中调用它

    希望它会起作用。

    【讨论】:

      【解决方案3】:
      @Override
       public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
               workoutList = new ArrayList < > ();
               for (DataSnapshot snapshot: dataSnapshot.getChildren()) {
                   String workout_id = snapshot.child("workout_id").getValue(String.class);
                   mDatabase.child("workout").child(workout_id).
                   addListenerForSingleValueEvent(new ValueEventListener() {
                       @Override
                       public void onDataChange(@NonNull DataSnapshot snapshot) {
                           Workout workout = snapshot.getValue(Workout.class);
                           workout.setWorkout_id(workout_id);
                           Log.d("GET", "workout_name: " + workout.getWorkout_name() +
                           " " + workout.getWorkout_id());
                           workoutList.add(workout);
                       }
      
                       @Override
                       public void onCancelled(@NonNull DatabaseError error) {}
                   });
               }
               WorkoutAdapter adapter = new WorkoutAdapter();
               recyclerView.setAdapter(adapter);
               adapter.setWorkoutList(workoutList);
               adapter.notifyDataSetChanged();
      

      for each loop中,您以异步方式填写列表,在foreach之后,您在调用adapter.setWorkoutList(workoutList)时立即调用adapter.setWorkoutList(workoutList),列表没有数据,因为您以异步方式填写列表。您必须等到获取数据或使用同步方式获取数据并在所有数据获取后强制调用

      adapter.setWorkoutList(workoutList);
      adapter.notifyDataSetChanged();
      

      为了实现这一点,你可以这样做:

       @Override
       public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
               workoutList = new ArrayList < > ();
               WorkoutAdapter adapter = new WorkoutAdapter();
               recyclerView.setAdapter(adapter);
               int[] num= {0};
               for (DataSnapshot snapshot: dataSnapshot.getChildren()) {
                   String workout_id = snapshot.child("workout_id").getValue(String.class);
                   mDatabase.child("workout").child(workout_id).
                   addListenerForSingleValueEvent(new ValueEventListener() {
                       @Override
                       public void onDataChange(@NonNull DataSnapshot snapshot) {
                           Workout workout = snapshot.getValue(Workout.class);
                           workout.setWorkout_id(workout_id);
                           Log.d("GET", "workout_name: " + workout.getWorkout_name() +
                           " " + workout.getWorkout_id());
                           workoutList.add(workout);
                           num[0]=num[0]++;
                           if(num[0]==dataSnapshot.getChildren().size(){
                                adapter.setWorkoutList(workoutList);
                                adapter.notifyDataSetChanged();
                           }
                       }
      
                       @Override
                       public void onCancelled(@NonNull DatabaseError error) {}
                   });
               }
               
             
      

      【讨论】:

      • adapter.notifyDataSetChanged(); 的调用需要在onDataChange 内。
      • 在 foreach 循环中每次在 onDataChange 方法中调用 notifyDataSetChanged() 对性能不利
      • 鉴于对onDataChange 的每次调用都对应于通过网络传入的新数据,我预计网络的性能将远远超过重新渲染视图的性能。但话虽如此,您更新的解决方案检查是否所有调用都已完成是减少重绘次数的好方法,如果这成为性能问题的话。我只是建议先测量性能,然后再假设它是一个问题。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-24
      • 1970-01-01
      相关资源
      最近更新 更多