问题
Below is the code for the listview adapter which displays list of tracks.Each list item has play and stop icons to play and stop track.When user clicks on play image button it changes to pause button image and track starts playing..When I click on pause button image it changes to play button image and track stops playing.While playing a track if I click on another track play image button previous track stops and the current clicked track start playing but the issue is that previous track still show the pause icon image button while it should change to play image button since currently its not playing.It is because holder.img2 visiblity is still true since we never changed the visiblity of image( holder.img1 is for play icon and holder.img2 is for stop icon).For that i need to access the image icon of the last clicked item in listview..What should i do so that the pause image button should change to play image button if i clicked on another track play button while playing current track..
回答1:
You can use something like this:
- Save
position
in a global variablelastPosition
inonClick
every time. Check if position is not default value, then get its related Row, its images, and set their visibility as per need.
int lastPosition = -1; holder.img1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(final View v) { if (lastPosition != -1) { View lastRow = listView.getChildAt(lastPosition); ImageView play = (ImageView) lastRow.findViewById(R.id.play2); ImageView pause = (ImageView) lastRow.findViewById(R.id.pause); play.setVisibility(View.GONE); pause.setVisibility(View.VISIBLE); } .. //all other code of onClick for media player lastPosition = position; } }
Hope it helps.
回答2:
One another way could be by saving last clicked row's ImageView
directly to variable
of SoundCloudAdapter
.
Simple declare a variable global to SoundCloudAdapter
ImageView imgRowLastClicked;
and
holder.img1.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(final View v)
{
if(imgRowLastClicked!= null)
{
lastClickedimg= imgRowLastClicked;
lastClickedimg.setVisibility(View.GONE);
}
//your other code
imgRowLastClicked= (ImageView) v.findViewById(R.id.play2);
}
}
Do same for pause ImageView
来源:https://stackoverflow.com/questions/26959345/accessing-image-of-the-last-clicked-item-in-listview