Accessing image of the last clicked item in listview

一世执手 提交于 2019-12-12 04:53:09

问题


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 variable lastPosition in onClick 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!