安卓。获取 ListView 项目 onClick 内部按钮到新的子 fragment

标签 android listview android-fragments

我有一个带有 项的 ListView 的 fragment 。 row 只包含在 ImageView

fragment _1.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/fragment_1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Fragment1">

    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:divider="#b5b5b5"
        android:dividerHeight="1dp"
        android:listSelector="@drawable/list_selector"/>

</RelativeLayout>

行.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/row"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/list_selector"
    android:orientation="horizontal"
    android:padding="5dip">

    <!-- Rightend Play Button. -->
    <ImageView
        android:id="@+id/play_button"
        android:layout_width="50dip"
        android:layout_height="50dip"
        android:src="@drawable/ic_play"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"/>

</RelativeLayout>

我有一个 ResourceCursorAdapter 来填充这个 ListView。我只是为每个 ImageView 分配一个点击监听器。它播放保存在提供的路径中的记录。

private abstract class MyAdapter extends ResourceCursorAadpter {

    @Override
    public void bindView(View view, Context context, final Cursor cursor) {
        // Play button.
        ImageView playButton = (ImageView) view.findViewById(R.id.play_button);
        playButton.setOnClickListener(new View.OnClickListener() {
            String record = cursor.getString(cursor.getColumnIndex(DbAdapter.RECORD));
            public void onClick(View v) {
                // Play record in path [record]. Not the problem.
            }
        });
    }
}

现在我想在 row 单击中打开一个新 fragment Fragment_2。此 fragment 具有相同的播放按钮,并且必须播放相同的记录。

fragment _2.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/fragment_audio"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Fragment2">

    <!-- Play Button. -->
    <ImageView
        android:id="@+id/play_button"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/ic_play"/>

</RelativeLayout>

我如何管理此按钮播放与 Fragment_1 中相同的记录? 如果我有一个隐藏的 TextView 和记录的路径,我想我可以做到,但确保您有更智能的解决方案。

Fragment_1onCreateView 中。

mList = (ListView) root.findViewById(R.id.list);
// Click event for single row.
mList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view,
                            int position, long id) {
        // TODO: Go to fragment_2 and has the same view [view.findViewById(R.id.play_button)]
    }
});

最佳答案

fragment 之间的通信应该通过关联的 Activity 来完成。您可以为此使用接口(interface)。它将作为他们之间的契约和桥梁。

让我们有以下组件:

An activity hosts fragments and allow fragments communication

FragmentA first fragment which will send data

FragmentB second fragment which will receive datas from FragmentA

FragmentA 的实现是:

public class FragmentA extends Fragment {
  DataPassListener mCallback;
  public interface DataPassListener{
    public void passData(String data);
  }
  @Override
  public void onAttach(Activity activity) {
    super.onAttach(activity);
    // Make sure that container activity implement the callback interface
    try {
      mCallback = (DataPassListener)activity;
    } catch (ClassCastException e) {
      throw new ClassCastException(activity.toString()
            + " must implement DataPassListener");
    }
  }
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
    // Suppose that when a button clicked second FragmentB will be inflated
    // some data on FragmentA will pass FragmentB
    // Button passDataButton = (Button).........
    passDataButton.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        if (view.getId() == R.id.passDataButton) {
          mCallback.passData("Text to pass FragmentB");
        }
      }
    });
  }
}

MainActivity 实现是:

public class MainActivity extends ActionBarActivity implements DataPassListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    if (findViewById(R.id.container) != null) {
        if (savedInstanceState != null) {
            return;
        }
        getFragmentManager().beginTransaction()
                .add(R.id.container, new FragmentA()).commit();
    }
}
@Override
public void passData(String data) {
    FragmentB fragmentB = new FragmentB ();
    Bundle args = new Bundle();
    args.putString(FragmentB.DATA_RECEIVE, data);
    fragmentB .setArguments(args);
    getFragmentManager().beginTransaction()
        .replace(R.id.container, fragmentB )
        .commit();
}
}

FragmentB 的实现是:

public class FragmentB extends Fragment{
final static String DATA_RECEIVE = "data_receive";
TextView showReceivedData;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_B, container, false);
    showReceivedData = (TextView) view.findViewById(R.id.showReceivedData);
}
@Override
public void onStart() {
    super.onStart();
    Bundle args = getArguments();
    if (args != null) {
        showReceivedData.setText(args.getString(DATA_RECEIVE));
    }
}

https://developer.android.com/training/basics/fragments/communicating.html 这份文件已经给出了详细的信息,你可以去看看。

在您的情况下,您可以应用这种方式在两个 fragment 之间进行通信。

关于安卓。获取 ListView 项目 onClick 内部按钮到新的子 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39126987/

相关文章:

android - 删除android中的动态表行

android - ListView 和图像

c# - 如何按比例调整WPF Listview的大小?

android - 获取 fragment 中的当前位置

android - 根据行为在不同的 fragment 上使用相同的工具栏折叠工具栏布局

java - 使用 Parcelable 对象将数据发送到 Fragment 而不是 Activity

java - Android为动态创建的列表设置点击事件

java - 与 MVVM 的套接字连接

android - Android 数据库上的多个表 - 没有这样的表

ios - 想要将数据传递到其他组件-ListView