【发布时间】:2015-09-07 15:04:32
【问题描述】:
我正在使用一个 RecyclerView,它有一个 Switch,它允许用户在开关打开时从 A 到 Z 组织列表,在开关关闭时从 Z 到 A 组织列表,以实现我需要的 OnClick 方法到现在每个元素的位置,但元素可能在两个不同的位置(取决于用户选择的排序)所以为了知道元素在哪里,我需要从类中知道 Switch 的状态实现 OnClick 方法。
这是我的 Switch-sorting 实现:
@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(您可以为此使用标签),如果匹配,请随心所欲。
标签: java android get android-widget