android - 如何使用选项卡更新滑动 View 中的 fragment

标签 android android-fragments swipe event-listener

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

问题是:我有两个 fragment/选项卡。每个都包含一个ListView。我可以从左侧列表中删除一个项目。当我向右滑动时,我想更新列表适配器,以便显示左侧删除的项目。

我尝试了onSwipeListernerTabListeneronPageChangeListener(以及 fragment 本身的on Resume())。没有任何效果...要么该函数没有被调用,要么我没有得到 fragment 对象。

有人知道当我滑动到此选项卡/fragment 时如何调用 Fragment 类中的函数吗?

谢谢!

最佳答案

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

我相信这就是您想要实现的目标。但我不认为你的行动计划是最好的。

我会在我的 fragment 中创建一个界面,其中的项目将从中删除

 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

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

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

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

取决于您如何通过 View 寻呼机调用创建 fragment

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

这就是它的全部。希望这可以帮助您理解接口(interface)方式是正确的方式。如果没有您的代码,这有点难以提供帮助,但这就是它的完成方式。最难的部分是当您使用 View 分页器创建 fragment 时将界面添加到 fragment 中。祝你好运

关于android - 如何使用选项卡更新滑动 View 中的 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20050519/

相关文章:

ios - 如何使用滑动 Action 移动带有动画的 tableViewCell 中的单元格

java - 使用代码使用相对布局时出错

android - 更改 ListView 中的 Textview 大小运行时

java - 如何在MainActivity中获取ViewPager创建的fragment的元素?

java - 在 fragment OnCreate() 方法中获取 Textview 的 Id

Android:如何在家庭应用程序中切换桌面等 Activity ?

android - panic : Missing arch-specific emulator program: C:\Program Files (x86)\Android\android-sdk\tools/emulator-mips. exe

android - 如何在 Android 中停止 gif 动画?

安卓错误 : "commit already called" when remove a fragment

jquery - 使用 Phonegap + jQuery Mobile 滚动