提问人:ayna khan 提问时间:8/11/2023 最后编辑:Dmitriy Popovayna khan 更新时间:8/12/2023 访问量:15
即使在保存后,Switch 在滚动时也会丢失其状态
switch loses its state while scrolling even after getting saved
问:
我正在使用“ListView”来显示所有系统应用程序及其名称、图标和开关,并希望保存开关的状态,如果我选中它并关闭应用程序,它会保存状态,但是如果我向下滚动列表,开关会丢失其状态。
我希望它在任何情况下都能保存开关状态,但在向下滚动列表后它会丢失状态 这是我的代码:
public class system_apps extends Fragment {
private static final String PREFERENCE_KEY = "AppSwitchStates";
private List<AppInfo> systemAppInfos = new ArrayList<>();
private AppListAdapter adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_system_apps, container, false);
ListView listView = view.findViewById(R.id.system_app_list);
adapter = new AppListAdapter(requireContext(), systemAppInfos);
listView.setAdapter(adapter);
// Populate the list of system apps
systemAppInfos.clear();
systemAppInfos.addAll(getSystemAppInfos());
adapter.notifyDataSetChanged();
return view;
}
private List<AppInfo> getSystemAppInfos() {
List<AppInfo> appInfos = new ArrayList<>();
PackageManager packageManager = requireContext().getPackageManager();
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> allAppInfos = packageManager.queryIntentActivities(mainIntent, 0);
SharedPreferences preferences = requireContext().getSharedPreferences(PREFERENCE_KEY, Context.MODE_PRIVATE);
for (ResolveInfo resolveInfo : allAppInfos) {
if ((resolveInfo.activityInfo.applicationInfo.flags & android.content.pm.ApplicationInfo.FLAG_SYSTEM) != 0) {
String appName = resolveInfo.loadLabel(getContext().getPackageManager()).toString();
boolean isSelected = preferences.getBoolean(appName, false);
Drawable appIcon = resolveInfo.loadIcon(packageManager);
AppInfo appInfo = new AppInfo(appName, appIcon, isSelected);
appInfos.add(appInfo);
}
}
return appInfos;
}
private class AppListAdapter extends ArrayAdapter<AppInfo> {
AppListAdapter(Context context, List<AppInfo> appInfos) {
super(context, R.layout.list_item_app, appInfos);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item_app, parent, false);
}
AppInfo appInfo = getItem(position);
if (appInfo != null) {
TextView appNameTextView = convertView.findViewById(R.id.app_name);
ImageView appIconImageView = convertView.findViewById(R.id.app_icon);
Switch appSwitch = convertView.findViewById(R.id.app_switch);
appNameTextView.setText(appInfo.appName);
appIconImageView.setImageDrawable(appInfo.appIcon);
appSwitch.setChecked(appInfo.isSelected);
// Set a listener to update the switch state
appSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> {
// Update the switch state in the AppInfo object
appInfo.isSelected = isChecked;
// Save the switch state
saveSwitchState(appInfo.appName, isChecked);
// Handle any additional logic here
});
// Show the switch for system apps
appSwitch.setVisibility(View.VISIBLE);
}
return convertView;
}
}
private void saveSwitchState(String appName, boolean isChecked) {
SharedPreferences preferences = requireContext().getSharedPreferences(PREFERENCE_KEY, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean(appName, isChecked);
editor.apply();
}
private static class AppInfo {
String appName;
Drawable appIcon;
boolean isSelected;
AppInfo(String name, Drawable icon, boolean selected) {
appName = name;
appIcon = icon;
isSelected = selected;
}
}
}
答: 暂无答案
评论