【问题标题】:No adapter attached; skipping layout in Fragment, RecyclerView未连接适配器;在 Fragment、RecyclerView 中跳过布局
【发布时间】:2019-12-01 12:44:55
【问题描述】:

我的应用程序中的 Recyclerview 无法正常工作。 片段给了我这个错误,但我无法弄清楚。

我知道有很多关于这个问题的问题,但到目前为止都没有解决我的问题。

片段代码:

public class PatientsFragment extends Fragment {

    private FragmentActivity myContext;
    ClinicViewModel clinicViewModel;

    @Override
    public void onAttach(Activity activity) {
        myContext = (FragmentActivity) activity;
        super.onAttach(activity);
    }


    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_patients, container, false);

        FloatingActionButton fab = root.findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // -> Dodawanie nowego pacjenta
                FrameLayout host = getView().findViewById(R.id.nav_host_fragment);
                //host.removeAllViews();
                AddPatientFragment addPatientFragment = new AddPatientFragment();
                FragmentManager fm = myContext.getSupportFragmentManager();
                fm.beginTransaction()
                        .replace(R.id.nav_host_fragment, addPatientFragment)
                        .addToBackStack(null)
                        .commit();
            }
        });

        View rootView = inflater.inflate(R.layout.fragment_patients, container, false);

        RecyclerView recyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerViewPatients);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setHasFixedSize(true);

        final PatientsAdapter adapter = new PatientsAdapter();
        recyclerView.setAdapter(adapter);

        clinicViewModel =   ViewModelProviders.of(this).get(ClinicViewModel.class);
        clinicViewModel.getAllPatients().observe(this, new Observer<List<Patient>>() {
            @Override
            public void onChanged(List<Patient> patients) {
                //update Recycler todo
                Toast.makeText(getContext(), "onChanged", Toast.LENGTH_SHORT).show();
                adapter.setPatients(patients);
            }
        });



        return root;
    } 

还有一个适配器

    private List<Patient> patients = new ArrayList<>();

    @NonNull
    @Override
    public PatientHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.patient_item, parent, false);
        return new PatientHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull PatientHolder holder, int position) {
        Patient currentPatient = patients.get(position);
        holder.textViewPersonName.setText(currentPatient.getFirstName() + " " + currentPatient.getLastName());
    }

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

    public void setPatients(List<Patient> patients){
        this.patients = patients;
        notifyDataSetChanged();
    }

    class PatientHolder extends RecyclerView.ViewHolder {
        private TextView textViewPersonName;

        public PatientHolder(@NonNull View itemView) {
            super(itemView);
            textViewPersonName = itemView.findViewById(R.id.textViewPersonName);
        }
    }

}

就像我之前说的,我看不出这段代码有什么问题:( 提前致谢!

【问题讨论】:

  • 尝试使用root 代替rootView 并告诉我
  • 为什么要充气两次?

标签: android android-fragments android-recyclerview


【解决方案1】:

尝试使用root 而不是rootView,如下所示:

public View onCreateView(@NonNull LayoutInflater inflater,
                         ViewGroup container, Bundle savedInstanceState) {
    View root = inflater.inflate(R.layout.fragment_patients, container, false);

    ....

    //View rootView = inflater.inflate(R.layout.fragment_patients, container, false);

    RecyclerView recyclerView = (RecyclerView) root.findViewById(R.id.recyclerViewPatients);
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    recyclerView.setHasFixedSize(true);

    final PatientsAdapter adapter = new PatientsAdapter();
    recyclerView.setAdapter(adapter);

    ....

    return root;
} 

【讨论】:

  • 是的,就是这样。非常感谢!
  • 请接受@domvnski 的回答。它将帮助其他人发现它有帮助。谢谢
猜你喜欢
  • 2021-05-04
  • 1970-01-01
  • 1970-01-01
  • 2021-04-24
  • 2020-03-12
  • 1970-01-01
  • 1970-01-01
  • 2016-09-09
  • 1970-01-01
相关资源
最近更新 更多