需要帮助在我的 RecyclerView 中实现 Spinner

Need help to implement a Spinner into my RecyclerView

我正在尝试用 Spinner 对我的 RecyclerView 进行排序。列表中的每个项目都包含 1 个 ImageView 和 2 个 TextView 组件。如果有人可以在我的代码中实现 Spinner,我会很高兴,我可以对这些项目进行排序。

我尝试实现微调器两次,但需要在没有微调器的情况下重建到回收站视图,因为我失败了。不知道如何一起设置回收器视图和微调器,即使有教程...我被卡住了。

我的Item.xml布局:

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

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="4dp"
        android:background="@color/Green">

        <ImageView
            android:id="@+id/SupItemIV1"
            android:layout_width="150dp"
            android:layout_height="75dp"
            android:padding="2dp"
            android:layout_margin="4dp"/>

        <TextView
            android:id="@+id/SupItemTV1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_alignParentLeft="true"
            android:layout_marginStart="160dp"
            android:layout_marginLeft="150dp"
            android:layout_marginTop="4dp"
            android:text="CREATINE"
            android:textColor="@color/Black"
            android:textSize="20sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/SupItemTV2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="160dp"
            android:layout_marginTop="30dp"
            android:text="amino acid"
            android:textColor="@color/GreyDark"
            android:textSize="15sp"
            android:textStyle="bold"/>

    </RelativeLayout>

</androidx.cardview.widget.CardView>

我的 MainActivity(此处Supplements.javaclass):

package com.example.etigym;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import androidx.core.view.ViewCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;

public class Supplements extends AppCompatActivity{

    private RecyclerView mRecyclerView;
    private RecyclerView.Adapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;

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

        ArrayList<SupplementsItem> supplementsList = new ArrayList<>();
        supplementsList.add(new SupplementsItem(R.drawable.creatine, "CREATINE", "amino acid"));
        supplementsList.add(new SupplementsItem(R.drawable.glutamine, "GLUTAMINE", "amino acid"));
        supplementsList.add(new SupplementsItem(R.drawable.leucine, "LEUCINE", "amino acid"));
        supplementsList.add(new SupplementsItem(R.drawable.valine, "VALINE", "amino acid"));
        supplementsList.add(new SupplementsItem(R.drawable.isoleucine, "ISOLEUCINE", "amino acid"));
        supplementsList.add(new SupplementsItem(R.drawable.alanine, "ALANINE", "amino acid"));
        supplementsList.add(new SupplementsItem(R.drawable.arginine, "ARGININE", "amino acid"));

        mRecyclerView = findViewById(R.id.Supplements_RV);
        mRecyclerView.setHasFixedSize(true);
        mLayoutManager = new LinearLayoutManager(this);
        mAdapter = new SupplementsAdapter(supplementsList);

        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.setAdapter(mAdapter);
        ViewCompat.setNestedScrollingEnabled(mRecyclerView, false);

    }
}

我的补充布局来自 Supplements.java class:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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=".Supplements">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/Supplements_RV"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="4dp"
        android:background="@color/GreyDark"
        android:scrollbars="vertical"/>

</RelativeLayout>

我的适配器:

public class SupplementsAdapter extends RecyclerView.Adapter<SupplementsAdapter.SupplementsViewHolder> {
    private ArrayList<SupplementsItem> mSupplementsList;

    public static class SupplementsViewHolder extends RecyclerView.ViewHolder{
        public ImageView mImageView;
        public TextView mTextView1;
        public TextView mTextView2;

        public SupplementsViewHolder(@NonNull View itemView) {
            super(itemView);
            mImageView = itemView.findViewById(R.id.SupItemIV1);
            mTextView1 = itemView.findViewById(R.id.SupItemTV1);
            mTextView2 = itemView.findViewById(R.id.SupItemTV2);
        }
    }

    public SupplementsAdapter(ArrayList<SupplementsItem> supplementsList){
        mSupplementsList = supplementsList;
    }

    @NonNull
    @Override
    public SupplementsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.supplements_item, parent, false);
        SupplementsViewHolder svh = new SupplementsViewHolder(v);
        return svh;
    }

    @Override
    public void onBindViewHolder(@NonNull SupplementsViewHolder holder, int position) {
        SupplementsItem currentSupplementsItem = mSupplementsList.get(position);

        holder.mImageView.setImageResource(currentSupplementsItem.getImageResource());
        holder.mTextView1.setText(currentSupplementsItem.getText1());
        holder.mTextView2.setText(currentSupplementsItem.getText2());
    }

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

SupplementsItem.java:

package com.example.etigym;

public class SupplementsItem {
    private int mImageResource;
    private String mText1;
    private String mText2;

    public SupplementsItem(int imageResource, String text1, String text2){
        mImageResource = imageResource;
        mText1 = text1;
        mText2 = text2;
    }

    public int getImageResource(){
        return mImageResource;
    }

    public String getText1(){
        return mText1;
    }

    public String getText2(){
        return mText2;
    }
}

之后我们应该有一个可以按类别排序的列表。

首先你必须知道如何根据你的需要对列表中的项目进行排序,为此你必须创建一个 class 实现 Comparator 通常你想这样做 class(es) 在你的模型 Class SupplementsItem 中,这就是你的 SupplementsItem 的样子,请注意我添加了一个额外的 属性 expiryDate , 这个想法是你了解如何使用比较器对你的列表进行排序。

public class SupplementsItem {
    private int itemImage;
    private String item;
    private String category;
    private long expiryDate;

    public SupplementsItem(int itemImage, String item, String category, long expiryDate) {
        this.itemImage = itemImage;
        this.item = item;
        this.category = category;
        this.expiryDate = expiryDate;
    }

    public int getItemImage() {
        return itemImage;
    }

    public void setItemImage(int itemImage) {
        this.itemImage = itemImage;
    }

    public String getItem() {
        return item;
    }

    public void setItem(String item) {
        this.item = item;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public long getExpiryDate() {
        return expiryDate;
    }

    public void setExpiryDate(long expiryDate) {
        this.expiryDate = expiryDate;
    }

    //    This will sort your list ascending by category
    public static class CategoryComparatorUp implements Comparator<SupplementsItem> {
        @Override
        public int compare(SupplementsItem o1, SupplementsItem o2) {
            return o1.getCategory().compareToIgnoreCase(o2.getCategory());
        }
    }

    //    This will sort your list descending by category
    public static class CategoryComparatorDown implements Comparator<SupplementsItem> {
        @Override
        public int compare(SupplementsItem o1, SupplementsItem o2) {
            return o2.getCategory().compareToIgnoreCase(o1.getCategory());
        }
    }

    //    This will sort your list ascending by expiration date
    public static class ExpiryComparatorUp implements Comparator<SupplementsItem> {
        @Override
        public int compare(SupplementsItem o1, SupplementsItem o2) {
            return Long.compare(o1.getExpiryDate(), o2.getExpiryDate());
        }
    }

    //    This will sort your list descending by expiration date
    public static class ExpiryComparatorDown implements Comparator<SupplementsItem> {
        @Override
        public int compare(SupplementsItem o1, SupplementsItem o2) {
            return Long.compare(o2.getExpiryDate(), o1.getExpiryDate());
        }
    }
}

我已经放置了 4 个比较器,但您实际上只需要一个按类别分类,或者制作一个更复杂的比较器。

例如,如果你想按到期日期降序排序,你想使用 ExpiryComparatorUp 然后 Collections.reverse(),这样你只能在你的模型中使用 属性 的一个比较器.

要在填充列表后使用它,请调用:

 Collections.sort(supplementsList, new SupplementsItem.CategoryComparatorDown());

其次是:

mAdapter.notifyDataSetChanged();

或者你可以为你想做的每种类型的排序创建一个方法,即:

   private void sortByCategoryDown() {
        Collections.sort(supplementsList, new SupplementsItem.CategoryComparatorDown());
        mAdapter.notifyDataSetChanged();
    }

然后在微调器的 onItemSelected 中调用 sortByCategoryDown()

从这里开始,您只需处理微调器逻辑。让我知道您在评论中的疑问。 我修改了你的 class,目的是让你知道如何做 Comparator 的事情,但你不需要那样做。