【问题标题】:How to access to a position of a Fragment listview item from another Fragment如何从另一个片段访问片段列表视图项的位置
【发布时间】:2017-04-25 07:49:36
【问题描述】:

我有一个片段,其中包含一些音调的列表视图。我还有另一个片段,其中包含第一个片段中最喜欢的项目的列表视图。 如果主片段中的用户想要取消将某个项目标记为收藏,我必须检查该特定项目是否正在播放。因此,我需要从我的主 Fragment 访问一个名为 position 的 int 变量。

Favorite Fragment 中的一个方法:

public void stopSong(int position){
  int selectedSong = soundList.get(position).getRes();
}

Main Fragment 访问该方法:

  if (MainFragment.isPlaying==true){
  MainFragment z = (MainFragment) MainFragment.mainFragment;
  z.getFragmentManager().findFragmentByTag(MainFragment.ARG_ITEM_ID);
  if (z!=null){
    z.stopSong(position);//position that I need to get from Favorite Fragment method
  }
}

你能帮我解决这个问题吗?

【问题讨论】:

  • 您可以为此使用共享偏好或使用捆绑发送位置

标签: android listview android-fragments fragment


【解决方案1】:

使用 FragmentTransaction 切换片段,并添加参数 z.setArguments(getIntent().getExtras());

稍后,执行方法stopSong

我建议进入服务停止音乐,不应该从 UI[Fragment] 控制

【讨论】:

    猜你喜欢
    • 2020-01-04
    • 2016-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-29
    • 2020-10-30
    • 1970-01-01
    相关资源
    最近更新 更多