Firebase:RecyclerView 跳过布局没有附加适配器

Posted

技术标签:

【中文标题】Firebase:RecyclerView 跳过布局没有附加适配器【英文标题】:Firebase :RecyclerView Skipping layout no Adapter Attached 【发布时间】:2021-12-19 12:41:52 【问题描述】:

我是一个在 firebase java android 上工作的初学者。我的问题是我无法加载 recyclerView 它显示 adapter not attach Skipping layout 即使我已附加适配器仍然显示相同

以下是我的代码,请帮助我。提前致谢

private RecyclerView bookList,listOrg,listcustom;

@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my_work_employee);

    mAuth = FirebaseAuth.getInstance();
    currentUserID = mAuth.getCurrentUser().getUid();

    MyCustomWorkRef = FirebaseDatabase.getInstance().getReference().child("WorksCustomAdded");
    
    bookList = findViewById(R.id.myCustomerList);
    listcustom = findViewById(R.id.myCustomerListCustom);

    bookList.setHasFixedSize(true);
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(MyWorkEmployeeActivity.this);
    linearLayoutManager.setReverseLayout(true);
    linearLayoutManager.setStackFromEnd(true);
    bookList.setLayoutManager(linearLayoutManager);




    listcustom.setHasFixedSize(true);
    LinearLayoutManager linearLayoutManager2 = new LinearLayoutManager(MyWorkEmployeeActivity.this);
    linearLayoutManager2.setReverseLayout(true);
    linearLayoutManager2.setStackFromEnd(true);
    listcustom.setLayoutManager(linearLayoutManager2);


    //LoadDat();

    DisplayCustom("ltG284DWVbbxfHlD3vAiiwkEv963","22883b15-d432-42b7-8ba5-1b3d4586ff09");


   // DisplayCustom();


这是我的显示自定义函数

private void DisplayCustom(String cmpnyUID,String empId) 


    Toast.makeText(getApplicationContext(), "CompnUID : " + cmpnyUID + "\n  empID : "+empId, Toast.LENGTH_SHORT).show();
    Query SortPostsInDecendingOrder = MyCustomWorkRef.child(cmpnyUID).orderByChild("empID").equalTo(empId);;

    FirebaseRecyclerOptions<MyCustomer> options = new FirebaseRecyclerOptions.Builder<MyCustomer>()
            .setQuery(SortPostsInDecendingOrder, MyCustomer.class)
            .build();
    FirebaseRecyclerAdapter<MyCustomer, MyWorkEmployeeActivity.PBooksViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<MyCustomer, MyWorkEmployeeActivity.PBooksViewHolder>(options)
    
        @NonNull
        @Override
        public MyWorkEmployeeActivity.PBooksViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i)
        
            View view= LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.viewwork_list_eng, viewGroup, false);
            MyWorkEmployeeActivity.PBooksViewHolder viewHolder =new MyWorkEmployeeActivity.PBooksViewHolder(view);
            return viewHolder;
        

        @Override
        protected void onBindViewHolder(@NonNull MyWorkEmployeeActivity.PBooksViewHolder holder, int position, @NonNull MyCustomer model)
        
            final String pos = getRef(position).getKey();
            String cmpnyID = model.getCmpnyID();
            String wrkID = model.getWrkSiteID();
            String engID = model.getEngKey();
            String usrID = model.getUsrkey();


       



            holder.bookname.setText(model.getUsrname());
            holder.count.setText("Work No: \n"+model.getCnt());


            holder.mView.setOnClickListener(new View.OnClickListener() 
                @Override
                public void onClick(View v) 
                    Intent reqIntent = new Intent(MyWorkEmployeeActivity.this, ViewCustomWrkActivity.class);
                    reqIntent.putExtra("wrkID", model.getWrkSiteID());
                    reqIntent.putExtra("usrkey","0");

                    startActivity(reqIntent);
                
            );

        
    ;


    listcustom.setAdapter(firebaseRecyclerAdapter);
    firebaseRecyclerAdapter.startListening();


    


    public class PBooksViewHolder extends RecyclerView.ViewHolder 

    TextView bookname, bookauthor,fees ,count;
    ImageView profImage;
    LinearLayout mView;
    String t_area, b_area, plan, allow;
    public PBooksViewHolder(@NonNull View itemView) 

        super(itemView);


        profImage = itemView.findViewById(R.id.ivRequestProfile);
        bookname = itemView.findViewById(R.id.tvRequestUserName);
        bookauthor = itemView.findViewById(R.id.tvWorksCompletedEngineerList);
        mView = itemView.findViewById(R.id.request_layout);
        fees =  itemView.findViewById(R.id.tvFeesEngineerList);

        count =  itemView.findViewById(R.id.wrkCount);


    

【问题讨论】:

我认为您应该在某个时候将firebaseRecyclerAdapter 分配给 RecyclerView?我在你的代码中看不到。 @MartinMarconcini listcustom.setAdapter(firebaseRecyclerAdapter); 你是说这个吗? 是的,你说得对,我没看到。 【参考方案1】:

我找到了这个问题的答案。在我的 xml 文件中,我一直在高度上使用match_parent。这导致主线程跳过布局,因为它找不到布局比例。

所以我将值更改为 dp 中的某个标准高度。例如:600dp。它正确定义了布局,因此现在 recyclerView 可以完美加载而不会跳过布局。

我的 XML 代码

`

   <LinearLayout
                    android:layout_
                    android:layout_
                    android:orientation="vertical">


                    <androidx.recyclerview.widget.RecyclerView
                        android:id="@+id/myWorkListemp"

                        android:layout_
                        android:layout_
                        android:layout_marginTop="15dp" />

                    <androidx.recyclerview.widget.RecyclerView
                        android:visibility="gone"

                        android:id="@+id/myCustomerListCustom"
                        android:layout_
                        android:layout_
                        android:layout_marginTop="15dp" />

                </LinearLayout>

`

【讨论】:

以上是关于Firebase:RecyclerView 跳过布局没有附加适配器的主要内容,如果未能解决你的问题,请参考以下文章

Firebase:Recyclerview 没有附加适配器,跳过布局

Firebase:RecyclerView 跳过布局没有附加适配器

错误:E/RecyclerView:未连接适配器;跳过片段上的布局

kotlin 在片段中的 RecyclerView 中显示来自 Firebase 数据库的数据

E/RecyclerView:没有附加适配器;在片段内的recyclerview上跳过布局

Fragment,RecyclerView:没有附加适配器;跳过布局