Есть обработчик:
private int previousPosition = -1;
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
ImageView image;
if (previousPosition == position) {
image = v.findViewById(R.id.btnPlay);
image.setImageResource(R.drawable.ic_btn_play);
previousPosition = -1;
} else {
image = v.findViewById(R.id.btnPlay);
image.setImageResource(R.drawable.ic_btn_pause);
previousPosition = position;
}
}
Сам лист
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:listitem="@layout/fragment_item"
android:choiceMode="singleChoice"
android:background="@color/bg_listView"
android:listSelector="@drawable/item_bg"/>
<TextView
android:id="@id/android:empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/no_data" />
</LinearLayout>
И его элемент
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/btnPlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/ic_btn_play"
android:background="@null"
android:contentDescription="@string/contentDescriptionPlay"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="12dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/tvSong"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/song"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="5dp"
android:textSize="17sp"/>
<TextView
android:id="@+id/tvSinger"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/singer"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="3dp"
android:layout_marginBottom="8dp"/>
</LinearLayout>
</TableRow>
<TextView
android:id="@+id/tvDuration"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:text="@string/duration"
android:textAlignment="viewEnd"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="17dp"/>
</FrameLayout>
Выглядит так:
То есть при клике по элементу изображение меняется на "паузу", при повторном клике на "плей".
Проблема: когда выбираешь, например, первый элемент, изображение меняется не только у него, но и у элемента ниже (который не виден на текущем экране - немного скролла вниз и видно)
UPD: Код с адаптером:
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Handler mHandler = new Handler(hc);
fileRead = new FileRead(mHandler);
}
Handler.Callback hc = new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
switch (msg.what) {
case MESSAGE_READ:
playlist = fileRead.getPlaylist();
ArrayList<HashMap<String, String>> myArrList = new ArrayList<>();
HashMap<String, String> map;
for (int i = 0; i < playlist.size(); i++){
map = new HashMap<>();
Track t = playlist.get(i);
map.put("song", t.getSong());
map.put("singer", t.getSinger());
map.put("duration", getDurationOfMin(t.getDuration()));
myArrList.add(map);
}
SimpleAdapter adapter = new SimpleAdapter(getActivity(), myArrList,
R.layout.fragment_item,
new String[] {"song", "singer", "duration"},
new int[] {R.id.tvSong, R.id.tvSinger, R.id.tvDuration});
setListAdapter(adapter);
break;
}
return false;
}
};