我想在后退时刷新listview fragmentA
抱歉,我的英语不好。示例:
我在FragmentA的listview中点击onitem到FragmentB
当我在FragmentB中单击返回(在Activity中)时,我想要刷新 listview FragmentA。
如何在FragmentA中设置onViewCreated OR onResume ???
和我的活动
@Override
public void onBackPressed() {
super.onBackPressed();
MyFragmentA
@Override
public void setItems(ArrayList<NotificationListModel> items,String last_page) {
Log.d("NotificationFragment", "setItems ");
this.notificationListModels=items;
this.last_page=last_page;
for (NotificationListModel notificationListModel: notificationListModels){
notificationListModelArrayList.add(notificationListModel);
}
getData();
if (getActivity()!=null) {
int currentPosition = getListView().getFirstVisiblePosition();
adapNotificationList = new AdapNotificationList(getActivity(), notificationListModelArrayList);
setListAdapter(adapNotificationList);
adapNotificationList.notifyDataSetChanged();
getListView().setSelection(currentShowList);
getListView().setSelectionFromTop(currentPosition + 1, 0);
}
}
答案 0 :(得分:1)
覆盖片段A中的onResume并调用adapter.notifyDataSetChanged()或处理listview数据刷新的函数。
@Override
public void onResume() {
super.onResume();
adapter.notifyDataSetChanged();
//OR
callDataRefreshFunction();
}
您无需在Activity中处理onBackPressed。
答案 1 :(得分:0)
首先检查fragmentB是否可见,如果可见则
@Override
public void onBackPressed() {
if(fragmentb.isVisible())
{
//update listview of fragment a
adapter.notifyDataSetChanged()
}
else
super.onBackPressed();
}
答案 2 :(得分:0)
我猜您正在将数据从服务器加载到listview中。如果是这样,并且您想要在后面按下刷新列表视图,那么就不要覆盖后退方法。只需从片段B中删除此代码即可。
@Override
public void onBackPressed() {
super.onBackPressed();
}
答案 3 :(得分:0)
在片段中,您需要在返回后刷新尝试添加:
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
if (FragmentCreated) { //FragmentCreated = True in procedure onCreateView
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.detach(this).attach(this).commit();
//This reloads current fragment
}
}else{
// fragment is no longer visible
}
}