RecyclerView отображается как пустой - PullRequest
1 голос
/ 15 октября 2019

Мой 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 itemмакет: (я использовал @ 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>

Макет активности:

<?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>

Ответы [ 3 ]

0 голосов
/ 15 октября 2019

Пожалуйста, измените ваш categories_edit_recycler_item Элемент Layouts ниже:

<?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="wrap_content"
 android:orientation="vertical">

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

    <TextView
        android:id="@+id/categoryEditText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:layout_marginTop="5dp"
        android:text="TextView"
        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_marginEnd="10dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/delete" />
</androidx.constraintlayout.widget.ConstraintLayout>

</LinearLayout>
0 голосов
/ 15 октября 2019

попробуйте это для макета элемента

<?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>

и это для макета активности

<?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>
0 голосов
/ 15 октября 2019

Измените расположение элементов RecyclerView ниже.

<RelativeLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:android="http://schemas.android.com/apk/res/android">

        <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" />

        <ImageView
            android:id="@+id/deleteCategory"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_marginTop="22dp"
            android:layout_marginEnd="16dp"
            android:layout_alignParentEnd="true"
            android:src="@drawable/ic_drawable_delete" />
    </RelativeLayout>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...