如何刷卡查看与标签更新片段片段、标签

2023-09-08 09:28:46 作者:心凉不过一瞬间 #

以下内容:我使用的是刷卡查看与标签。工作比较顺利,到目前为止。

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

的事情是:我有两个片段/标签。每个包含的ListView 。我可以从左边的列表中删除项目。当我向右滑动我想更新列表适配器,使左删除的项显示。

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.

我试过 onSwipeListerner TabListener onPageChangeListener (和的简历()在片段本身)。毫无效果......无论是功能不叫或我没有得到该片段的对象。

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?

谢谢!

推荐答案

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

 }

最后,实现此接口在Fragment2

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

这是它的全部。希望这可以帮助您了解该接口的方法是要走的路。这是一种很难帮助您不需code,但这是它是如何做。最难的是接口加入该片段在创建片段,您可以查看传呼机。好运

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