java - RecyclerView 显示为空

标签 java android android-recyclerview

我的 recyclerView 没有显示任何东西。我查看了网站上发布的其他类似问题,但找不到任何相关的内容。当我打开 Activity 时,我只看到顶部有应用程序名称的栏,下面没有其他内容。
我的目标是获取项目列表,其中每个项目都有一个“删除”图标,可以单击该图标将其删除。为此,我为 recyclerView 项目定制了一个布局。 我做了一个简化版本的代码来尝试找出问题所在。在这里:

Activity :

public class CategoryManagerActivity extends AppCompatActivity {
    private RecyclerView categoryRecyclerView;

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

        categoryRecyclerView = findViewById(R.id.category_edit_recycler);

        ArrayList<String> categoryList = new ArrayList<>();
        categoryList.add("CAT1");
        categoryList.add("CAT2");
        categoryList.add("CAT3");

        CategoryEditRecyclerViewAdapter adapter = new CategoryEditRecyclerViewAdapter(categoryList);
        categoryRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        categoryRecyclerView.setAdapter(adapter);
    }
}

适配器类:

public class CategoryEditRecyclerViewAdapter extends
        RecyclerView.Adapter<CategoryEditRecyclerViewAdapter.CategoryEditViewHolder>{
    private ArrayList<String> categories;

    public CategoryEditRecyclerViewAdapter(ArrayList<String> items){
        categories = items;
    }

    @NonNull
    @Override
    public CategoryEditViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        Context context = parent.getContext();
        View view = LayoutInflater.from(context).inflate(R.layout.categories_edit_recycler_item,
                parent, false);
        return new CategoryEditRecyclerViewAdapter.CategoryEditViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull CategoryEditViewHolder holder, int position) {
        final String category = categories.get(position);
        holder.categoryName.setText(category);
        holder.deleteButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                delete_category(category);
            }
        });
    }

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

    private void delete_category(String category){ 
        //I have code here to delete an item
    }

    public class CategoryEditViewHolder extends RecyclerView.ViewHolder{
        TextView categoryName;
        ImageView deleteButton;

        public CategoryEditViewHolder(View itemView) {
            super(itemView);
            categoryName = itemView.findViewById(R.id.categoryEditText);
            deleteButton = itemView.findViewById(R.id.deleteCategory);
        }
    }
}

recyclerView 项目布局: (我在应用程序的其他 Activity 中使用了@drawable/delete,所以我认为这应该不是问题)

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

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

        <TextView
            android:id="@+id/categoryEditText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="24dp"
            android:layout_marginTop="22dp"
            android:layout_marginBottom="690dp"
            android:text="TextView"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="1.0" />

        <ImageView
            android:id="@+id/deleteCategory"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_marginTop="22dp"
            android:layout_marginEnd="16dp"
            android:layout_marginBottom="679dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:srcCompat="@drawable/delete" />
    </androidx.constraintlayout.widget.ConstraintLayout>

</LinearLayout>

Activity 布局:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".CategoryManagerActivity">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/category_edit_recycler"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>

最佳答案

试试这个项目布局

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >

    <TextView
        android:id="@+id/categoryEditText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="24dp"
        android:layout_marginTop="22dp"
        android:text="TextView"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/deleteCategory"
        app:layout_constraintVertical_bias="1.0" />

    <ImageView
        android:id="@+id/deleteCategory"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_marginTop="22dp"
        android:layout_marginEnd="16dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/delete" />
</androidx.constraintlayout.widget.ConstraintLayout>

这是 Activity 布局

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".CategoryManagerActivity">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/category_edit_recycler"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>

关于java - RecyclerView 显示为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58390457/

相关文章:

java - 需要帮助类型转换类(class)

java - 如何在我的 Android 设备上运行我的 Android 应用程序?

java - 如何将 BaseAdapter 转换为 RecyclerAdapter

android - 如何使 RecyclerView 上方的 Layout 滚动

java - java中的字符串空构造函数

java - 这是 HashMap 的正确用例吗

java - Processing 是否支持迭代器?

android - Android捕获内部(应用程序)音频

android - getSpans、getSpanStart、getSpanEnd 需要导入什么?

java - Android recyclerview 中的 recyclerview 项目