1
我正在使用一个RecyclerView,它具有一个开关,可以让用户在开关打开时组织从A到Z的列表,并且从Z到A时关闭,以实现我需要的OnClick方法现在是每个元素的位置,但元素可能位于两个不同的位置(取决于用户选择的排序),所以为了知道元素的位置,我需要从实现的类中知道Switch的状态OnClick方法。如何从另一个类获得Android开关状态?
这是我的开关排序实现:
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
switchStatus.setText("Sorting alphabetically");
Collections.sort(categories, new Comparator<Categories>() {
@Override
public int compare(Categories lhs, Categories rhs) {
return lhs.title.compareTo(rhs.title);
}
});
ca.notifyDataSetChanged();
}else{
switchStatus.setText("Sorting by popularity");
Collections.sort(categories, new Comparator<Categories>() {
@Override
public int compare(Categories rhs, Categories lhs) {
return lhs.title.compareTo(rhs.title);
}
});
ca.notifyDataSetChanged();
}
}
});
//check the current state before we display the screen
if(categoriesSortingSwitch.isChecked()){
switchStatus.setText("Sorting alphabetically");
Collections.sort(categories, new Comparator<Categories>() {
@Override
public int compare(Categories lhs, Categories rhs) {
return lhs.title.compareTo(rhs.title);
}
});
}
else {
switchStatus.setText("Sorting by popularity");
Collections.sort(categories, new Comparator<Categories>() {
@Override
public int compare(Categories rhs, Categories lhs) {
return lhs.title.compareTo(rhs.title);
}
});
}
}
//I CREATED THIS METHOD THINKING IN USING IT IN THE OTHER CLASS TO GET THE STATUS
public boolean getSwitchstatus(){
if(categoriesSortingSwitch.isChecked()){
return true;
}
else return false;
}
而这是其他类的,我需要获得状态的方法:
@Override
public void onClick(View v) {
final Intent intent;
int position = getAdapterPosition();
if (position==0){
if(/** find a way to see if it is on or off and if it is on do this**/){
intent = new Intent(context, nose.class);
context.startActivity(intent);
}
else/** if it is off**/{
}
}
}
在恢复,我需要找到一种获取开关状态的方法,以便我可以知道元素的位置。
非常感谢。
使用SharedPreference。在开关打开时保存/创建数据(共享),并在关闭时将其删除。如果打开,请检查SharedPreference(您可以使用标签),如果匹配,则无论您想要的位置如何。 –