recyclerview未显示(显示)

rfbsl7qr  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(313)

recyclerview根本不显示。只显示文本视图。我查看了其他问题,并使用了以前问题的所有答案。其他的答案建议,回收器的宽度不能设置为wrap content,或者在设置layout manager和我满足这些条件之后应该调用setadapter。
主要活动

  1. public class MainActivity extends AppCompatActivity {
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.activity_main);
  6. RecyclerView recyclerView = findViewById(R.id.main_recycler);
  7. recyclerView.setLayoutManager(new LinearLayoutManager(this));
  8. final MainAdapter adapter = new MainAdapter();
  9. recyclerView.setAdapter(adapter);
  10. List<FirebaseProduct> firebaseProductList = new ArrayList<>();
  11. firebaseProductList = getData(); //Here is my Firebase code
  12. adapter.setList(firebaseProductList);
  13. }
  14. }

活动\u main.xml

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:app="http://schemas.android.com/apk/res-auto"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. tools:context=".MainActivity">
  7. <TextView
  8. android:id="@+id/main_text"
  9. android:layout_width="match_parent"
  10. android:layout_height="wrap_content"
  11. android:text="@string/aktualne_produkty"
  12. android:gravity="center"
  13. android:background="@color/colorPrimary"
  14. android:textAppearance="@style/TextAppearance.AppCompat.Large">
  15. </TextView>
  16. <androidx.recyclerview.widget.RecyclerView
  17. android:id="@+id/main_recycler"
  18. android:layout_height="wrap_content"
  19. android:layout_width="match_parent"
  20. tools:listitem="@layout/card_main"
  21. android:layout_below="@id/main_text"/>
  22. </RelativeLayout>

主适配器

  1. public class MainAdapter extends RecyclerView.Adapter<MainAdapter.MainHolder> {
  2. private List<FirebaseProduct> productList = new ArrayList<>();
  3. @NonNull
  4. @Override
  5. public MainHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
  6. View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_main, parent, false);
  7. return new MainHolder(itemView);
  8. }
  9. @Override
  10. public void onBindViewHolder(@NonNull MainHolder holder, int position) {
  11. FirebaseProduct product = productList.get(position);
  12. Log.d("Prod holder", "name - " + product.getProductName());
  13. Log.d("Prod holder", "num - " + product.getProductNumber());
  14. holder.nameView.setText(product.getProductName());
  15. holder.numView.setText(product.getProductNumber());
  16. }
  17. @Override
  18. public int getItemCount() {
  19. return productList.size();
  20. }
  21. public void setProductList(List<FirebaseProduct> productList1){
  22. this.productList = productList1;
  23. notifyDataSetChanged();
  24. }
  25. public List<FirebaseProduct> getList(){
  26. return productList;
  27. }
  28. public static class MainHolder extends RecyclerView.ViewHolder{
  29. private TextView nameView;
  30. private TextView numView;
  31. public MainHolder(View itemView){
  32. super(itemView);
  33. nameView = itemView.findViewById(R.id.product_name);
  34. numView = itemView.findViewById(R.id.product_count);
  35. }
  36. }
  37. }

主卡

  1. <androidx.cardview.widget.CardView
  2. xmlns:app="http://schemas.android.com/apk/res-auto"
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:layout_width="match_parent"
  5. android:layout_height="wrap_content"
  6. app:cardCornerRadius="8dp">
  7. <LinearLayout
  8. android:layout_width="match_parent"
  9. android:layout_height="wrap_content"
  10. android:orientation="horizontal">
  11. <RelativeLayout
  12. android:layout_width="0dp"
  13. android:layout_height="wrap_content"
  14. android:background="@color/colorPrimary"
  15. android:layout_weight="3">
  16. <TextView
  17. android:id="@+id/product_name"
  18. android:layout_width="match_parent"
  19. android:layout_height="wrap_content"
  20. android:gravity="center">
  21. </TextView>
  22. <TextView
  23. android:id="@+id/product_count"
  24. android:layout_width="match_parent"
  25. android:layout_height="wrap_content"
  26. android:layout_below="@id/product_name">
  27. </TextView>
  28. </RelativeLayout>
  29. <ImageView
  30. android:id="@+id/product_image"
  31. android:layout_width="0dp"
  32. android:layout_height="wrap_content"
  33. android:layout_weight="2"
  34. android:contentDescription="@string/product_description">
  35. </ImageView>
  36. </LinearLayout>
  37. </androidx.cardview.widget.CardView>
wn9m85ua

wn9m85ua1#

你在打电话吗 adapter.setList(firebaseProductList);MainActivity 但是函数名 MainAdaptersetProductList() . 所以把它改成 adapter. setProductList(firebaseProductList) 另外,你要确定 getData();MainActivity 应返回非空列表。您可以使用调试器来验证它,或者只需在之前添加一个log语句 adapter.setList(firebaseProductList) . 这样地:

  1. Log.d("LIST_SIZE", firebaseProductList.size()); //this will print list size
  2. adapter.setList(firebaseProductList); //you have to change it to adapter. setProductList(firebaseProductList)

相关问题