【发布时间】:2016-10-01 12:03:30
【问题描述】:
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 也与适配器一起完美初始化。
已编辑:
我删除了 notifyDataSetChanged()。我每次都设置适配器。现在它工作正常。
【问题讨论】:
标签: android android-activity fragment orientation