安卓。获取listview项目onClick内部按钮以创建新的子片段

圣地亚哥·吉尔

我有一个带有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>

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

现在我想开一个新片段Fragment_2row点击。该片段具有相同的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及其记录路径,就可以了,但是请确保您有一个更聪明的解决方案。

在我onCreateViewFragment_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)]
    }
});
阿杰·P·普拉亚帕蒂(Ajay P.

片段之间的通信应通过关联的活动来完成。您可以为此使用界面。它将作为合同和它们之间的桥梁。

让我们具有以下组件:

活动托管片段并允许片段通信

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

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本文档详细介绍了信息,您可以仔细阅读。

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

TOP 榜单

热门标签

归档