需要帮助以在我的RecyclerView中实现微调

时间:2019-07-15 22:44:31

标签: java android android-layout android-recyclerview spinner

我正在尝试用RecyclerView对我的Spinner进行排序。列表中的每个项目都包含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.java类中):

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类中的我的增补版面:

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

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

1 个答案:

答案 0 :(得分:1)

首先,您必须知道如何根据需要对列表中的项目进行排序,为此您必须创建一个实现Comparator的类,通常您想在模型中进行此类类SupplementsItem,这就是您的SupplementsItem的外观,请注意,我添加了一个额外的属性expiryDate,其思想是您了解如何使用比较器对列表进行排序。 / p>

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()

从这里开始,您只需要按照微调器逻辑工作即可。让我知道您在评论中的疑问。 我修改了您的课程,其目的是让您了解如何进行Comparator事情,但是您不必那样做。