在我的项目中,我在其中一个片段中的android片段之间航行,有一个列表从json接收信息。 有必要每隔1分钟更新一次这个列表,我正在使用
postDelayed (this, 60000);
调用一个使用的线程
changeActivity (ActivitiesListFragment.class, null);
刷新列表。
正在运行,列表会在新片段中更新。
问题是,当我按下后退按钮时,它会返回上一个列表,因为它没有“被破坏”。
如何启动新片段并删除以前的片段? 谢谢!!
public class ActivitiesListFragment extends AbstractListFragment {
private AbstractNetworkHandler handler;
private ListView listView;
private ActivitiesListAdapter adapter;
private long currentPage = 0L;
private int pageSize = 50;
private int previousTotal = 0;
private boolean loading = true;
private boolean isPaused;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable final Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.fragment_activities_list, container, false);
Handler handler;
handler = new Handler();
super.setupListUI(NetworkAction.ACTIVITIES_LIST, rootView, true);
this.handler = AbstractNetworkFactory.build(super.getContext(), super.networkAction);
this.listView = (ListView) rootView.findViewById(R.id.list_view_activities);
this.listView.setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if(totalItemCount < pageSize) {
return;
}
if(loading) {
if(totalItemCount > previousTotal) {
loading = false;
previousTotal = totalItemCount;
currentPage++;
}
}
if(!loading && (totalItemCount == (firstVisibleItem + visibleItemCount))) {
loading = true;
Editable text = ActivitiesListFragment.super.txtFilter.getText();
if(text != null && text.toString() != null && !text.toString().isEmpty()) {
refreshGrid(text.toString(), null);
}
else {
refreshGrid(null, null);
}
}
}
});
if(this.isPaused) {
this.listView.setAdapter(adapter);
Bundle arguments = getArguments();
if(arguments != null) {
String mode = arguments.getString("mode");
if("search".equals(mode)) {
Editable text = this.txtFilter.getText();
String toFilter = text.toString();
this.adapter.clearItems();
this.adapter.notifyDataSetInvalidated();
this.currentPage = 0;
this.previousTotal = 0;
if(toFilter != null && !toFilter.isEmpty()) {
refreshGrid(toFilter, null);
}
else {
refreshGrid(null, null);
}
}
}
return rootView;
}
if(!ApplicationOptionsManager.hasOptionsCache(super.getActivity(), networkAction)) {
final Count c = new Count();
// this.refreshOptions(true);// false para adicionar "Listar Todos" no
final HomeActivity home = (HomeActivity) getActivity();
final Runnable myRunnable= new Runnable() {
@Override
public void run()
{
refreshOptions(true);
adapter.notifyDataSetChanged();
handler.postDelayed(myRunnable, 60 * 1000);
}
};
return rootView;
}
private void refreshOptions(final boolean isToLoad) {
final ProgressDialog dialog = MobileUtils.getBasicProgressDialog(super.getContext());
handler.getOptions(new HttpOptionsBaseListener() {
@Override
public void onRequestCompleted(OptionsVO options, Integer httpStatus, CharSequence response) {
ApplicationOptionsManager.storeOptions(ActivitiesListFragment.super.getActivity(), networkAction, options);
if(isToLoad) {
String txtToFilter = txtFilter.getText().toString();
if(txtToFilter != null && !txtToFilter.isEmpty()) {
refreshGrid(txtToFilter, dialog);
}
else {
refreshGrid(null, dialog);
}
}
else {
dialog.dismiss();
// addOne(); "Listar todos" no começo do fragment
}
}
}, new HttpFailListener() {
@Override
public void onRequestCompleted(Exception e, Integer httpStatus, CharSequence msg) {
String txtToFilter = txtFilter.getText().toString();
if(txtToFilter != null && !txtToFilter.isEmpty()) {
refreshGrid(txtToFilter, null);
}
else {
refreshGrid(null, null);
}
dialog.dismiss();
}
});
}
private void refreshGrid(final CharSequence textToFilter, final ProgressDialog currentDialog) {
super.runRunnableOnUIThread(new Runnable() {
@Override
public void run() {
final ProgressDialog dialog = currentDialog != null ? currentDialog : MobileUtils.getBasicProgressDialog(getContext());
JSONObject where = new JSONObject();
if(textToFilter != null && !textToFilter.toString().isEmpty()) {
try {
where.put("nmStep", textToFilter);
}
catch(JSONException e) {
}
}
handler.get(currentPage, new HttpJsonObjectListener() {
@Override
public void onRequestCompleted(final JSONObject object, Integer httpStatus, CharSequence msg) {
final JSONArray array = object.optJSONArray("searchResultsCollection");
ActivitiesListFragment.super.runRunnableOnUIThread(new Runnable() {
@Override
public void run() {
dialog.dismiss();
if(adapter != null) {
int count = adapter.getCount();
if(count > 0) {
if(array.length() > 0) {
adapter.addAll(array);
}
else {
currentPage--;
if(currentPage < 0) {
currentPage = 0;
}
}
}
else {
String[] columns = { "nmStep" };
if(array.length() == 0) {
JSONObject value = new JSONObject();
try {
value.put("id", -1);
value.put("nmStep", "Nenhum resultado encontrado.");
}
catch(Exception e) {
}
array.put(value);
}
adapter = new ActivitiesListAdapter(array, columns, ActivitiesListFragment.this);
listView.setAdapter(adapter);
}
}
else {
if(array.length() == 0) {
JSONObject value = new JSONObject();
try {
value.put("id", -1);
value.put("nmStep", "Nenhum resultado encontrado.");
}
catch(Exception e) {
}
array.put(value);
}
String[] columns = { "nmStep", "dtStartFmt" };
adapter = new ActivitiesListAdapter(array, columns, ActivitiesListFragment.this);
listView.setAdapter(adapter);
}
}
});
}
}, new HttpFailListener() {
@Override
public void onRequestCompleted(Exception e, Integer httpStatus, CharSequence msg) {
dialog.dismiss();
ActivitiesListFragment.super.runToastOnUiThread(R.string.application_generic_error, Toast.LENGTH_LONG);
}
});
}
});
}
protected int getContentViewId() {
return(R.layout.fragment_activities_list);
}
@覆盖 protected void onFilterForText(CharSequence textToFilter){ if(adapter!= null){ adapter.clearItems(); currentPage = 0; previousTotal = 0; } refreshGrid(textToFilter.toString(),null); }
@Override
protected int getTitleResource() {
return R.string.title_activity_activities_list;
}
@Override
public void onPause() {
handler.removeCallbacks(myRunnable);
this.isPaused = true;
super.onPause();
}
@Override
public void onResume() {
handler.postDelayed(myRunnable, 60 * 1000);
super.onResume();
}
public class Count {
private int count;
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}
public View getChildAt(int position) {
return(this.listView.getChildAt(position));
}
}
答案 0 :(得分:1)
tiny sunlight's发帖回答你的问题。但是他提到当你只想更新ListView时不应该创建一个新的Fragment。实际上,实现此目的的方法是更新处理程序内的列表内容,而不是创建新的Fragment。实现这一目标的片段如下:
if($results["foo"] && $results["email"]){
您获得的是避免更换碎片的内存,时间和代码开销。
答案 1 :(得分:0)
为什么要创建新的Fragment
来更新?
如果你想这样做;像这样添加:
Fragment a = YourFragment();
getSupportFragmentManager().beginTransition().add(a,"a").commitAllowStateloss();
像这样删除:
Fragment a = getSupportFragmentManager().findFragmentWithTag("a");
if(a!=null){
getSupportFragmentManager().beginTransition().remove(a,"a").commitAllowStateloss();
}