我的主页:我想在点击排序按钮时对回收器视图进行排序。
预计会发生(当点击排序按钮时:case1:如果回收者未排序......则必须按递补顺序排序case2:如果回收者未按递补顺序排序......则必须按递补顺序排序case1:如果回收者未按递补顺序排序......则必须按递补顺序排序)。
package edmt.dev.androidgridlayout;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import edmt.dev.androidgridlayout.HelperClasses.HomeAdapter.CategoryAdapter;
import edmt.dev.androidgridlayout.HelperClasses.HomeAdapter.FeaturedAdapter;
import edmt.dev.androidgridlayout.HelperClasses.HomeAdapter.FeaturedHelperClass;
import edmt.dev.androidgridlayout.HelperClasses.HomeAdapter.ProductAdapter;
public class AllProductPage extends AppCompatActivity {
RecyclerView productrecycler;
ProductAdapter productAdapter;
LinearLayout sortbtn,filterbtn;
private String[] mArrayNames=new String[]{"Pizza","Burger","Wraps","Sandwich","Chicken combos","Veg combos","Majitos","Breveges"};
ArrayList<FeaturedHelperClass> products;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_all_product_page);
productrecycler = findViewById(R.id.product_recycler);
sortbtn = findViewById(R.id.sort_btn);
filterbtn = findViewById(R.id.filter_btn);
productrecycler();
sortbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sortArray();
// sortArrayList();
}
});
}
private void sortArray() {
Collections.sort(products);
productAdapter.notifyDataSetChanged();
}
private void sortArrayList() {
Collections.sort(products, new Comparator<FeaturedHelperClass>() {
@Override
public int compare(FeaturedHelperClass o1, FeaturedHelperClass o2) {
return o1.getTitle().compareTo(o2.getTitle());
}
});
productAdapter.notifyDataSetChanged();
}
private void productrecycler() {
productrecycler.setHasFixedSize(true);
productrecycler.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
products = new ArrayList<>();
products.add(new FeaturedHelperClass(R.drawable.yarnbg2, "Pizza", "sdffsb",400));
products.add(new FeaturedHelperClass(R.drawable.yarnbg2, "Burger", "sdffsb",600));
products.add(new FeaturedHelperClass(R.drawable.yarnbg2, "Wraps", "sdffsbsfavs sdvv sbdfb ",550));
products.add(new FeaturedHelperClass(R.drawable.yarnbg2, "Sandwich", "sdffsbsfavs sdvv sbdfb ",300));
products.add(new FeaturedHelperClass(R.drawable.yarnbg2, "Chicken combos", "sdffsbsfavs sdvv sbdfb ",900));
products.add(new FeaturedHelperClass(R.drawable.yarnbg2, "Veg combos", "sdffsbsfavs sdvv sbdfb ",100));
products.add(new FeaturedHelperClass(R.drawable.yarnbg2, "Majitos", "sdffsbsfavs sdvv sbdfb ",250));
products.add(new FeaturedHelperClass(R.drawable.yarnbg2, "Breveges", "sdffsbsfavs sdvv sbdfb ",600));
productAdapter = new ProductAdapter(products);
productrecycler.setAdapter(productAdapter);
productAdapter.setOnItemClickedListner(new ProductAdapter.onItemClickListner() {
@Override
public void onItemClicked(int position) {
Toast.makeText(getApplicationContext(), "ProductAdapter" + position, Toast.LENGTH_LONG).show();
}
@Override
public void onHeartClicked(int position ,String state) {
Toast.makeText(getApplicationContext(), "onHeartClicked" + position+" "+state, Toast.LENGTH_LONG).show();
}
});
}
}
我的助手类:我在这里实现了Comparable接口,并对产品的价格进行了比较,使其按降序排列。
但是我无法将价格按降序排列。
如何实现?
package edmt.dev.androidgridlayout.HelperClasses.HomeAdapter;
public class FeaturedHelperClass implements Comparable<FeaturedHelperClass>{
int image;
String title;
String Description;
int price;
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public FeaturedHelperClass(int image, String title, String description) {
this.image = image;
this.title = title;
this.Description = description;
}
public FeaturedHelperClass(int image, String title, String description,int price) {
this.image = image;
this.title = title;
this.Description = description;
this.price = price;
}
public FeaturedHelperClass(int image, String title) {
this.image = image;
this.title = title;
}
public void setImage(int image) {
this.image = image;
}
public void setTitle(String title) {
this.title = title;
}
public void setDescription(String description) {
Description = description;
}
public int getImage() {
return image;
}
public String getTitle() {
return title;
}
public String getDescription() {
return Description;
}
@Override
public int compareTo(FeaturedHelperClass o) {
return this.price -o.getPrice();
}
}
你需要对列表进行排序。排序后只需将列表提交给适配器。联系 如何使用排序
这是排序的示例代码
Collections.sort(yourLists, new Comparator<YourList>() {
@Override
public int compare(YourList lhs, YourList rhs) {
return lhs.getId().compareTo(rhs.getId());
}
});
希望能成功!
谢谢! 编码愉快!
Java Collections.reverseOrder
默认情况下,Collection.sort按升序执行排序。如果我们想以相反的顺序对元素进行排序,我们可以使用以下方法: reverseOrder()。Returns a Comparator that imposes the reverse of natural ordering of elements of the collection.reverseOrder(Comparator cmp): Returns a Comparator that imposes reverse ordering of the specified comparator.下面是这两种方法的示例。
Collections.sort(products,Collections.reverseOrder());