我想按字母顺序或按受欢迎程度来订购一个基于开关的充满RecyclerView的cardViews,这是我迄今为止所做的:
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()中创建的列表中。
我怎么才能让这一切按我想要的方式运作呢?我没有使用排序算法,因为数据非常小。
发布于 2015-09-06 18:34:44
您可能需要使用比较器对Categories进行排序,然后将排序列表设置为启用CardView的适配器,然后使用notifyDataSetChanged()通知适配器。
本教程将有助于如何使用Comparator的。
https://stackoverflow.com/questions/32426755
复制相似问题