gpt4 book ai didi

java - RecyclerView 显示为空

转载 作者:行者123 更新时间:2023-11-30 05:00:10 24 4
gpt4 key购买 nike

我的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com