How to update fragment in Swipe View with Tabs

时间秒杀一切 提交于 2020-02-15 12:33:06

问题


the following: I'm using Swipe View with Tabs. Works quite smooth so far.

The thing is: I have two fragments/tabs. Each contains a ListView. I can remove an item from the left list. When I swipe to the right I want to update the list adapter so the left-deleted item is shown.

I tried onSwipeListerner, TabListener, onPageChangeListener (and on Resume() in the fragment itself). Nothing worked... Either the function is not called or I don't get the fragment object.

Does anybody know how I can call a function in my Fragment class when I swipe to this tab/fragment?

Thanks!


回答1:


http://developer.android.com/training/basics/fragments/communicating.html#Deliver

I believe this is what you are looking to accomplish. But I don't think your plan of action is the best.

I would create an interface in my fragment where items will be deleted from such as

 public class FragmentDeleteItemSharer extends Fragment{
     // Interface in Fragment
     public interface ShareDeletedItem{
          // Interface method you will call from this fragment
          public void shareItem(Object deletedItem);
     }// end interface


     // Instantiate the new Interface Callback
     ShareDeletedItem mCallback = null;

     // Override the onAttach Method
     @Override
     public void onAttach(Activity activity){
       super.onAttach(activity);

       try{ 
           // Attaches the Interface to the Activity
           // must add "implements ShareDeletedItem" in your 
           // Activity or this Exception is thrown
           mCallback = (ShareDeletedItem) activity;

       }catch(Exception ex){
           ex.printStackTrace();
       }
     }// end onAttach()


     // the method which you use to 
     // remove an item from the current fragment's listview
     // where position is from yourlistViewAdapter.getSelectedItemPosition();
     public void removeListItem(int position){
            // using the item position, get the item in your object array
            Object objectToDelete = myObjects[position];
            // pass this information to removeItemFromArray
            // a method that creates a new object array from the data
            Object [] newObjectList = removeItemFromArray(myObjects, objectToDelete);

            // Then use the interface callback to tell activity item was deleted
            mCallback.shareItem(objectToDelete);

            // Call to the method where you update the UI with the Objects
            // Are you using an arrayList? Not sure but probably have 
            // an ArrayList<Objects> myObjects, as reference above
            updateUiWithData(newObjectList);
     }

 }// end this fragment

Then in your activity create an interface

 public class MyActivity extends FragmentActivity implements ShareDeletedItem{

    // Interface Object , must attach this interface to Fragment2, when its created
    UpdateFragment2 mCallback = null;

    // You must attach this interface to your Fragment2, when its created
    // you could do so with your view pager, create a method that adds each
    // fragment to the view pager, then call a new method like                   
    // addinterface(fragmentReference)

    public interface UpdateFragment2{
           // method to call in your Fragment that shows queue of deletes
           public void addItemtoList(Object newObject);
    }

   // Interface Callback from the Fragment that deletes an item
   public void shareItem(Object deletedItem){

         // call the interface method to share the item with the Fragment2
         mCallback.addItemToList(deletedItem);
   }

 }

Finally, Implement this interface in your Fragment2

  public class Fragment2 extends Fragment implements UpdateFragment2{

     // Interface Method in charge of giving this fragment the deleted item
     public void addItemToList(Object deletedItem){
         // TODO: Add the Item to the list you currently have
         // If the mObjects array is an array list
         mObjects.add(mObjects[mObjects.length + 1], deletedItem);
     }
  }

Depending on how you create your fragments with your view pager call

try{ 
   // or however you hold reference to the fragment
  mCallBack = (UpdateFragment2) Fragment2.class; 
}catch(Exception ex){ex.printStackTrace();}

This is the Full of It. Hope this helps you understand that the interface way is the way to go. This is kind of difficult to help with no code from you but this is how it is done. The hardest part is adding the interface to the fragment when you create the fragments with you view pager. Good Luck



来源:https://stackoverflow.com/questions/20050519/how-to-update-fragment-in-swipe-view-with-tabs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!