如何使用选项卡更新滑动视图中的片段

时间:2013-11-18 14:43:46

标签: android android-fragments swipe event-listener

以下内容:我正在使用Swipe View with Tabs。到目前为止工作非常顺利。

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

我在片段中尝试了onSwipeListernerTabListeneronPageChangeListener(以及on Resume())。什么都没有用......要么没有调用函数,要么我没有得到片段对象。

当我滑动到此标签/片段时,有人知道如何在我的Fragment课程中调用某个函数吗?

谢谢!

1 个答案:

答案 0 :(得分:2)

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

我相信这是你想要完成的。但我不认为你的行动计划是最好的。

我会在我的片段中创建一个界面,其中的项目将从

中删除
 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

然后在您的活动中创建一个界面

 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中实现此接口

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

取决于您使用查看寻呼机调用创建片段的方式

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

这就是它的全部。希望这有助于您了解接口方式是可行的方法。没有你的代码,这有点难以帮助,但这就是它的完成方式。最难的部分是在使用查看分页器创建片段时将接口添加到片段。祝你好运