如何使用 Switch 基于字符串对 RecyclerView 中的 CardView 进行排序?
How to sort CardViews in a RecyclerView based on a String using a Switch?
我想按字母顺序或受欢迎程度排序一个 RecyclerView,其中包含基于开关的 cardView,这是我目前所做的:
public class CategoriesRecyclerView extends Activity {
private List<Categories> categories;
private RecyclerView rv;
private Switch categoriesSortingSwitch;
private TextView switchStatus;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.categories_recycler_view);
rv = (RecyclerView) findViewById(R.id.cardList);
rv.setHasFixedSize(true);
LinearLayoutManager llm = new LinearLayoutManager(this);
llm.setOrientation(LinearLayoutManager.VERTICAL);
rv.setLayoutManager(llm);
//HERE I CALL THE METHOD THAT INITIALIZE ALL THE CARDVIEWS WITH IT'S ELEMENTS ISIDE THE RECYCLERVIEW
initializeData();
CategoriesAdapter ca = new CategoriesAdapter(categories);
rv.setAdapter(ca);
//SWITCH IMPLEMENTATION FOR THE SORTING
categoriesSortingSwitch = (Switch) findViewById(R.id.switchsortcategories);
switchStatus = (TextView) findViewById(R.id.testswitch);
//set the switch to OFF
categoriesSortingSwitch.setChecked(false);
//attach a listener to check for changes in state
categoriesSortingSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
//HERE I SHOW A TEXT VIEW WITH THE SWITCH STATUS (WORKS FINE)
switchStatus.setText("Sorting alphabetically");
//HERE I TRY TO ADD ELEMENTS TO THE LIST categories ORGANIZED BY THEIR NAME
categories.add(new Categories("CARS", "CARS"));
categories.add(new Categories("COUSINE", "COUSINE"));
categories.add(new Categories("GAMBLING", "GAMBLING"));
categories.add(new Categories("GAMING", "GAMING"));
categories.add(new Categories("HISTORY", "HISTORY"));
categories.add(new Categories("MUSIC", "MUSIC"));
categories.add(new Categories("NATURE", "NATURE"));
categories.add(new Categories("RANDOM", "RANDOM"));
categories.add(new Categories("SPORTS", "SPORTS"));
categories.add(new Categories("STUDIES", "STUDIES"));
categories.add(new Categories("TECH", "TECH"));
}else{
switchStatus.setText("Sorting by popularity");
//HERE I TRY TO ADD ELEMENTS TO THE LIST categories ORGANIZED BY THEIR POPULARITY
categories.add(new Categories("CARS", "CARS"));
categories.add(new Categories("SPORTS", "SPORTS"));
categories.add(new Categories("GAMING", "GAMING"));
categories.add(new Categories("GAMBLING", "GAMBLING"));
categories.add(new Categories("TECH", "TECH"));
categories.add(new Categories("NATURE", "NATURE"));
categories.add(new Categories("RANDOM", "RANDOM"));
categories.add(new Categories("COUSINE", "COUSINE"));
categories.add(new Categories("HISTORY", "HISTORY"));
categories.add(new Categories("MUSIC", "MUSIC"));
categories.add(new Categories("STUDIES", "STUDIES"));
}
}
});
//check the current state before we display the screen
if(categoriesSortingSwitch.isChecked()){
switchStatus.setText("Sorting alphabetically");
categories.add(new Categories("CARS", "CARS"));
categories.add(new Categories("COUSINE", "COUSINE"));
categories.add(new Categories("GAMBLING", "GAMBLING"));
categories.add(new Categories("GAMING", "GAMING"));
categories.add(new Categories("HISTORY", "HISTORY"));
categories.add(new Categories("MUSIC", "MUSIC"));
categories.add(new Categories("NATURE", "NATURE"));
categories.add(new Categories("RANDOM", "RANDOM"));
categories.add(new Categories("SPORTS", "SPORTS"));
categories.add(new Categories("STUDIES", "STUDIES"));
categories.add(new Categories("TECH", "TECH"));
}
else {
switchStatus.setText("Sorting by popularity");
categories.add(new Categories("CARS", "CARS"));
categories.add(new Categories("SPORTS", "SPORTS"));
categories.add(new Categories("GAMING", "GAMING"));
categories.add(new Categories("GAMBLING", "GAMBLING"));
categories.add(new Categories("TECH", "TECH"));
categories.add(new Categories("NATURE", "NATURE"));
categories.add(new Categories("RANDOM", "RANDOM"));
categories.add(new Categories("COUSINE", "COUSINE"));
categories.add(new Categories("HISTORY", "HISTORY"));
categories.add(new Categories("MUSIC", "MUSIC"));
categories.add(new Categories("STUDIES", "STUDIES"));
}
}
//HERE IS THE initializeData() METHOD THAT I CALLED BEFORE THAT HAS A NEW LIST THAT SUPPOSEDLY WAS FILLED ALPHABETICALLY OR BY POPULARITY BASED ON THE SWITCH STATUS.
private void initializeData() {
categories = new ArrayList<>();
}
}
此时它编译并运行,当开关状态改变时,它会在我为此创建的 TextView 上正确地通知它,但是 RecyclerView 的元素没有任何反应,它们只是保持相同的顺序一些原因。
基本上想要做的是直接按照开关指定的顺序将元素添加到在方法 initializeData() 中创建的列表中。
我怎样才能按照我想要的方式完成这项工作?
我没有使用排序算法,因为数据很小。
您可能想使用 Comparator 对 Categories
进行排序,然后将排序后的列表设置为为 CardView
供电的适配器,然后使用 [=12] 通知适配器=].
This tutorial 将有助于了解如何使用 Comparator
s。
我想按字母顺序或受欢迎程度排序一个 RecyclerView,其中包含基于开关的 cardView,这是我目前所做的:
public class CategoriesRecyclerView extends Activity {
private List<Categories> categories;
private RecyclerView rv;
private Switch categoriesSortingSwitch;
private TextView switchStatus;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.categories_recycler_view);
rv = (RecyclerView) findViewById(R.id.cardList);
rv.setHasFixedSize(true);
LinearLayoutManager llm = new LinearLayoutManager(this);
llm.setOrientation(LinearLayoutManager.VERTICAL);
rv.setLayoutManager(llm);
//HERE I CALL THE METHOD THAT INITIALIZE ALL THE CARDVIEWS WITH IT'S ELEMENTS ISIDE THE RECYCLERVIEW
initializeData();
CategoriesAdapter ca = new CategoriesAdapter(categories);
rv.setAdapter(ca);
//SWITCH IMPLEMENTATION FOR THE SORTING
categoriesSortingSwitch = (Switch) findViewById(R.id.switchsortcategories);
switchStatus = (TextView) findViewById(R.id.testswitch);
//set the switch to OFF
categoriesSortingSwitch.setChecked(false);
//attach a listener to check for changes in state
categoriesSortingSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
//HERE I SHOW A TEXT VIEW WITH THE SWITCH STATUS (WORKS FINE)
switchStatus.setText("Sorting alphabetically");
//HERE I TRY TO ADD ELEMENTS TO THE LIST categories ORGANIZED BY THEIR NAME
categories.add(new Categories("CARS", "CARS"));
categories.add(new Categories("COUSINE", "COUSINE"));
categories.add(new Categories("GAMBLING", "GAMBLING"));
categories.add(new Categories("GAMING", "GAMING"));
categories.add(new Categories("HISTORY", "HISTORY"));
categories.add(new Categories("MUSIC", "MUSIC"));
categories.add(new Categories("NATURE", "NATURE"));
categories.add(new Categories("RANDOM", "RANDOM"));
categories.add(new Categories("SPORTS", "SPORTS"));
categories.add(new Categories("STUDIES", "STUDIES"));
categories.add(new Categories("TECH", "TECH"));
}else{
switchStatus.setText("Sorting by popularity");
//HERE I TRY TO ADD ELEMENTS TO THE LIST categories ORGANIZED BY THEIR POPULARITY
categories.add(new Categories("CARS", "CARS"));
categories.add(new Categories("SPORTS", "SPORTS"));
categories.add(new Categories("GAMING", "GAMING"));
categories.add(new Categories("GAMBLING", "GAMBLING"));
categories.add(new Categories("TECH", "TECH"));
categories.add(new Categories("NATURE", "NATURE"));
categories.add(new Categories("RANDOM", "RANDOM"));
categories.add(new Categories("COUSINE", "COUSINE"));
categories.add(new Categories("HISTORY", "HISTORY"));
categories.add(new Categories("MUSIC", "MUSIC"));
categories.add(new Categories("STUDIES", "STUDIES"));
}
}
});
//check the current state before we display the screen
if(categoriesSortingSwitch.isChecked()){
switchStatus.setText("Sorting alphabetically");
categories.add(new Categories("CARS", "CARS"));
categories.add(new Categories("COUSINE", "COUSINE"));
categories.add(new Categories("GAMBLING", "GAMBLING"));
categories.add(new Categories("GAMING", "GAMING"));
categories.add(new Categories("HISTORY", "HISTORY"));
categories.add(new Categories("MUSIC", "MUSIC"));
categories.add(new Categories("NATURE", "NATURE"));
categories.add(new Categories("RANDOM", "RANDOM"));
categories.add(new Categories("SPORTS", "SPORTS"));
categories.add(new Categories("STUDIES", "STUDIES"));
categories.add(new Categories("TECH", "TECH"));
}
else {
switchStatus.setText("Sorting by popularity");
categories.add(new Categories("CARS", "CARS"));
categories.add(new Categories("SPORTS", "SPORTS"));
categories.add(new Categories("GAMING", "GAMING"));
categories.add(new Categories("GAMBLING", "GAMBLING"));
categories.add(new Categories("TECH", "TECH"));
categories.add(new Categories("NATURE", "NATURE"));
categories.add(new Categories("RANDOM", "RANDOM"));
categories.add(new Categories("COUSINE", "COUSINE"));
categories.add(new Categories("HISTORY", "HISTORY"));
categories.add(new Categories("MUSIC", "MUSIC"));
categories.add(new Categories("STUDIES", "STUDIES"));
}
}
//HERE IS THE initializeData() METHOD THAT I CALLED BEFORE THAT HAS A NEW LIST THAT SUPPOSEDLY WAS FILLED ALPHABETICALLY OR BY POPULARITY BASED ON THE SWITCH STATUS.
private void initializeData() {
categories = new ArrayList<>();
}
}
此时它编译并运行,当开关状态改变时,它会在我为此创建的 TextView 上正确地通知它,但是 RecyclerView 的元素没有任何反应,它们只是保持相同的顺序一些原因。
基本上想要做的是直接按照开关指定的顺序将元素添加到在方法 initializeData() 中创建的列表中。
我怎样才能按照我想要的方式完成这项工作? 我没有使用排序算法,因为数据很小。
您可能想使用 Comparator 对 Categories
进行排序,然后将排序后的列表设置为为 CardView
供电的适配器,然后使用 [=12] 通知适配器=].
This tutorial 将有助于了解如何使用 Comparator
s。