【问题标题】:Android. Get listview item onClick inner button to new child fragment安卓。获取列表视图项 onClick 内部按钮到新的子片段
【发布时间】:2016-08-24 15:05:40
【问题描述】:

我有一个带有 row 项目的 ListView 的片段。 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_2。该片段具有相同的 play_button,并且必须播放相同的记录。

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,我可以做到,但确定你有更智能的解决方案。

在我的onCreateView 中Fragment_1。

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

【问题讨论】:

  • 我建议您编辑您的问题,并清楚地说明您的问题。还要发布代码而不是图像。
  • @AjayP.Prajapati 抱歉,我在上传代码时遇到了问题,但现在一切正常:) 有什么不清楚的地方吗?

标签: android listview android-fragments


【解决方案1】:

Fragment之间的通信应该通过关联的Activity来完成。您可以为此使用界面。它将充当他们之间的契约和桥梁。

让我们有以下组件:

活动承载片段并允许片段通信

FragmentA第一个将发送数据的片段

FragmentB 第二个片段,它将接收来自 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 这个文档已经给出了详细的信息,你可以去看看。

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

【讨论】:

  • 在我的情况下,我如何提取Text to pass FragmentB。我想将我的记录所在的路径传递给它,但我只有 onClick 来播放此记录。如何保存每行列表视图的路径字符串。在我的问题中,这个字符串是record。
  • 如您在示例中所见,您可以使用 bundle 传递值,并将其提取到您需要的地方。
  • 在示例中,我已将文本从 FragmentA 传递到 FragmentB。
  • 但我的问题是我不知道在哪里为每个 ListView 行保存这个字符串。也许在行位置访问的列表中?或者我也可以为每个行视图设置一个隐藏的 TextView。
  • 是的,您可以传递位置 id(假设您在行中附加了字符串 - 这可能/可以来自 API/Internet/本地存储到您的 listView)来跟踪它,并将其传递给 fragmentB。
猜你喜欢
  • 1970-01-01
  • 2018-05-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多