简体   繁体   English

onbackpressed() 时如何刷新片段中的列表视图

[英]How to refresh listview in fragment when onbackpressed()

I want to refresh listview fragmentA when back pressed我想在按下时刷新 listview fragmentA

example:例子:

I click onitem in listview in FragmentA to FragmentB我在 FragmentA 到 FragmentB 的列表视图中单击 onitem

and when I click back (in Activity) in FragmentB ,I want to refresh listview FragmentA.当我在 FragmentB 中单击返回(在 Activity 中)时,我想刷新列表视图 FragmentA。

sorry My english is not good.对不起,我的英语不好。

How to set onViewCreated OR onResume in FragmentA ???如何在 FragmentA 中设置 onViewCreated 或 onResume ???

and My activity和我的活动

@Override
    public void onBackPressed() {
        super.onBackPressed();

MyFragmentA我的片段A

 @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);
        }
    }

Override onResume in Fragment A and call adapter.notifyDataSetChanged() or the function which is handling data refresh of your listview.覆盖片段 A 中的 onResume 并调用 adapter.notifyDataSetChanged() 或处理列表视图数据刷新的函数。

@Override
public void onResume() {
    super.onResume();
    adapter.notifyDataSetChanged();
    //OR
    callDataRefreshFunction();

}

You do not need to handle onBackPressed in Activity.您不需要在 Activity 中处理 onBackPressed。

First check fragmentB is visible or not if visible then首先检查fragmentB是否可见,如果可见然后

@Override
    public void onBackPressed() {
        if(fragmentb.isVisible())
         {
           //update listview of fragment a
           adapter.notifyDataSetChanged()
         }
        else
          super.onBackPressed();
    }

I guess you are loading data from server into the listview.我猜您正在将数据从服务器加载到列表视图中。 If so and you want to refresh the listview on back press then just don't override the backpressed method.如果是这样,并且您想在按下后刷新列表视图,则不要覆盖 backpressed 方法。 Just remove this code from fragment B.只需从片段 B 中删除此代码即可。

@Override
    public void onBackPressed() {
        super.onBackPressed();
    }

In fragment you need to refresh after back pressed try add this:在片段中,您需要在按下后刷新尝试添加以下内容:

 @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 } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM