我正在使用一个带有Switch的RecyclerView,它允许用户在开关打开时将列表从A组织到Z,在关闭时从Z到A组织列表,以便实现我需要的OnClick方法到现在每个元素的位置,但元素可以在两个不同的位置(取决于用户选择的排序),所以为了知道元素的位置,我需要从类中知道Switch的状态实现OnClick方法。
这是我的Switch排序实现:
@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**/{
}
}
}
在简历中,我需要找到一种方法来获取开关的状态,以便我可以告诉元素的位置。
非常感谢。
答案 0 :(得分:0)
感谢MkiiSoft的建议,这就是我所做的和完美的工作:
@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();
//JUST ADDED THIS SHARED PREFERENCE WITH THE STRINGS ("it's","on") WHEN THE SWITCH IS ON
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = preferences.edit();
editor.putString("it's","on");
editor.apply();
}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();
//JUST ADDED THIS SHARED PREFERENCE WITH THE STRINGS ("it's","off") WHEN THE SWITCH IS OFF
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = preferences.edit();
editor.putString("it's", "off");
editor.apply();
}
}
});
然后在Class中,我现在需要切换的状态我只是比较字符串,并根据字符串的匹配或不匹配操作不同的Activity,这样:
public void onClick(View v) {
final Intent intent;
int position = getAdapterPosition();
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
String onoff = preferences.getString("it's", "");
//IF IT IS ON DO THIS
if(onoff.equalsIgnoreCase("on"))
{
if (position==0){
intent = new Intent(context, nose.class);
context.startActivity(intent);
}
//IF IT IS OFF DO THIS
} else if (onoff.equalsIgnoreCase("off")){
if (position==0){
intent = new Intent(context, nose2.class);
context.startActivity(intent);
}
}
}