Fragment
保留状态让我感到不舒服。我不知道我犯了什么错误。请帮助我们。
public class FragmentNotification extends Fragment implements OnAruguments {
public static final String LIST_ITEMS = "list_items";
public static final String TEMP_ITEMS = "temp_items";
List<DBNotifications> notificationModels = new ArrayList<>();
List<DBNotifications> tempModelList = new ArrayList<>();
NotificationAdapter notificationAdapter;
void setAdapter() {
if (notificationAdapter == null) {
notificationAdapter = new NotificationAdapter(getActivity(), tempModelList);
listView.setAdapter(notificationAdapter);
} else {
notificationAdapter.notifyDataSetChanged();
}
if (tempModelList.size() > 0) {
listView.setVisibility(View.VISIBLE);
cstmTxtNoItems.setVisibility(View.GONE);
} else {
listView.setVisibility(View.GONE);
cstmTxtNoItems.setVisibility(View.VISIBLE);
}
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (savedInstanceState != null) {
notificationModels = savedInstanceState.getParcelableArrayList(LIST_ITEMS);
tempModelList = savedInstanceState.getParcelableArrayList(TEMP_ITEMS);
}
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.notification, container, false);
init();
createItemsList();
return view;
}
void init() {
listview= (ListView) view.findViewById(R.id.listView);
}
void createItemsList() {
new NotificationModelCreation().execute();
}
void createNotificationModelList() {
String query = "SELECT * FROM notifications WHERE id='" + tempModel.getId() + "' AND code='" + Constants.CODE + "' ORDER BY priority DESC ";
List<Notifications> notificationsList = Notifications.findWithQuery(Notifications.class, query);
if (notificationsList != null && notificationsList .size() > 0) {
notificationModels.addAll(notificationsList );
tempModelList.addAll(notificationsList );
}
}
@Override
public void onResume() {
super.onResume();
setAdapter();// I have tried to set adapter onResume. Because, onCreateview will not be called on setRetainInstance =true
}
@Override
public void setOnArguments(Bundle bundle) {}
class NotificationModelCreation extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
createNotificationModelList();
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
setAdapter();
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putParcelableArrayList(LIST_ITEMS, (ArrayList<? extends Parcelable>) notificationModels);
outState.putParcelableArrayList(TEMP_ITEMS, (ArrayList<? extends Parcelable>) tempModelList);
}
}
第一次加载时,效果很好。但是,当配置更改时,设置Adapter
,但不显示视图。 ListView
也已初始化。因为,我正在通过这样的Fragment
标签获取。
getSupportManager().findFragmentByTag("tag");
获得Fragment
后,我使用了以下代码。
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.setCustomAnimations(R.anim.enter_from_right,
R.anim.exit_to_left,
R.anim.enter_from_right,
R.anim.exit_to_left)
.add(R.id.fragment_container, fragment, tag)
.addToBackStack(fragment.getClass().getName())
.commit();
我也管理过后台堆。没问题。唯一的问题是视图不可见。即使列表中有项目,ListView
也会与适配器设置一起完美初始化。
已编辑:
我删除了notifiyDataSetChanged()。我每次都设置适配器。现在工作正常。
答案 0 :(得分:0)
将此行放在清单的活动标记中,您可以在其中加载此片段
android:configChanges="keyboardHidden|orientation"
它不会影响任何数据,但方向会发生变化
快乐的Codding !!