【发布时间】: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