我想按字母顺序或按人气来订购基于交换机的充满cardViews的RecyclerView,这是我迄今为止所做的:
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()中创建的列表中。
我怎样才能以他想要的方式完成这项工作? 我没有使用排序算法,因为数据非常小。
答案 0 :(得分:4)
您可能希望使用Comparator对CardView
进行排序,然后将排序后的列表设置为适用于notifyDataSetChanged()
的适配器,然后通过Comparator
通知适配器
This tutorial将对如何使用{{1}} s。
有所帮助