java RecyclerView显示为空
我的回收视图没有显示任何内容。我浏览了网站上发布的其他类似问题,但找不到任何相关的问题。当我打开活动时,我只看到上面有应用程序名称的栏,下面没有其他内容
我的目标是得到一个项目列表,其中每个项目都有一个“删除”图标,可以点击删除它。为此,我为recyclerView项目定制了一个布局。
我制作了一个简化版的代码,试图隔离问题。这是:
活动:
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项目布局: (我在应用程序的其他活动中使用了@drawable/delete,所以我认为这不应该是个问题)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:app="http://schemas.安卓.com/apk/res-auto"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:orientation="vertical">
<安卓x.constraintlayout.widget.ConstraintLayout
安卓:orientation="horizontal" 安卓:layout_width="match_parent"
安卓:layout_height="match_parent">
<TextView
安卓:id="@+id/categoryEditText"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_marginStart="24dp"
安卓:layout_marginTop="22dp"
安卓:layout_marginBottom="690dp"
安卓:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
<ImageView
安卓:id="@+id/deleteCategory"
安卓:layout_width="30dp"
安卓:layout_height="30dp"
安卓:layout_marginTop="22dp"
安卓:layout_marginEnd="16dp"
安卓:layout_marginBottom="679dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/delete" />
</安卓x.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
活动布局:
<?xml version="1.0" encoding="utf-8"?>
<安卓x.constraintlayout.widget.ConstraintLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:app="http://schemas.安卓.com/apk/res-auto"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
tools:context=".CategoryManagerActivity">
<安卓x.recyclerview.widget.RecyclerView
安卓:id="@+id/category_edit_recycler"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</安卓x.constraintlayout.widget.ConstraintLayout>
# 1 楼答案
请修改下面的
categories_edit_recycler_item
项目Layouts
: