Not able to remove image view from from last item clicked in ArrayAdapter TextView

点点圈 提交于 2019-12-13 03:46:08

问题


So I have a Song List which plays a song when clicked on text view and displays a gif (audio spectrum) next to the song name. What I want is when I click on another song from Song List the previous clicked song should stop (which it was able to do). But along with this I also want the gif of the previous song to disappear which I am not able to do. Here is my code -

package com.shaikhsakib.sounddemo;

import android.app.Activity;
import android.content.Context;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.google.firebase.database.DatabaseReference;

import java.util.List;

import pl.droidsonroids.gif.GifImageView;

public class SongList extends ArrayAdapter {

    private Activity context;

    private List<Song> songList;

    DatabaseReference databaseReference;

    MediaPlayer mediaPlayer;

    View listViewItem;

    boolean unclicked = true;


    public SongList(@NonNull Activity context, List<Song> songList, DatabaseReference databaseReference){

        super(context, R.layout.playlist_textview, songList);

        this.context = context;

        this.songList = songList;

        this.databaseReference = databaseReference;

    }

    @Override
    public View getView(final int pos, View view, ViewGroup viewGroup){

        LayoutInflater inflater = context.getLayoutInflater();

        listViewItem = inflater.inflate(R.layout.songlist_textview, null, true);

        final TextView songListTextView = (TextView) listViewItem.findViewById(R.id.songListTextView);

        songListTextView.setSelected(true);

        final GifImageView bass = (GifImageView) listViewItem.findViewById(R.id.bass);

        final Song list = songList.get(pos);

        songListTextView.setText(list.getSongName());

        songListTextView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                if (mediaPlayer != null && mediaPlayer.isPlaying() && null!=bass.getDrawable()) {

                    mediaPlayer.stop();
                    //trying to remove gif from last song
                    bass.setVisibility(View.GONE);

                }else {
                    //trying to put back gif over here on current song
                    bass.setVisibility(View.VISIBLE);

                }



                try {

                        String f = Environment.getExternalStorageDirectory().getAbsolutePath() + "/music/" + list.getSongName().toString() + ".mp3";

                        Uri uri = Uri.parse(f);

                        mediaPlayer = MediaPlayer.create(context.getApplicationContext(), uri);

                        bass.setImageResource(R.drawable.bass);

                        mediaPlayer.start();

                    } catch (Exception e) {

                        e.printStackTrace();

                    }

            }

        });




        bass.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                if(mediaPlayer.isPlaying()) {

                    mediaPlayer.pause();

                    bass.setImageResource(R.drawable.bass1);

                }

                else {

                    bass.setImageResource(R.drawable.bass);

                    mediaPlayer.start();

                }

            }
        });

        return listViewItem;

    }
}

Here is video link of demo -

Demo Link


回答1:


Declare a GifImageView lastBass; in SongList class as global variable. Now modify your songListTextView's onClickListener code like:

songListTextView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                if (mediaPlayer != null && mediaPlayer.isPlaying() && null!=bass.getDrawable()) {

                    mediaPlayer.stop();
                    //trying to remove gif from last song
                    if(lastBass!=null)
                        lastBass.setVisibility(View.GONE);
                    bass.setVisibility(View.GONE);

                }else {
                     if(lastBass!=null)
                        lastBass.setVisibility(View.GONE);
                    //trying to put back gif over here on current song
                    bass.setVisibility(View.VISIBLE);

                }



                try {

                        String f = Environment.getExternalStorageDirectory().getAbsolutePath() + "/music/" + list.getSongName().toString() + ".mp3";

                        Uri uri = Uri.parse(f);

                        mediaPlayer = MediaPlayer.create(context.getApplicationContext(), uri);

                        bass.setImageResource(R.drawable.bass);
                        bass.setVisibility(View.VISIBLE); // add this line
                        mediaPlayer.start();
                        lastBass=bass;  // initialize lastBass here

                    } catch (Exception e) {

                        e.printStackTrace();

                    }

            }

        });

Update add this: bass.setVisibility(View.VISIBLE); after bass.setImageResource(R.drawable.bass); as in above code




回答2:


Try to save id of previous pressed song:

create some value in your SongList adapter and initialize it with song id on pressed

songListTextView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            someValue = pos //storing clicked song id
            if(someValue != -1){
                //stop your gif here using someValue and start pressed song gif using pos value
            }
            ...
        }

    });

Also you should to pass this someValue to activity or fragment and back to adapter for that you can write your function, which returns this someValue

I think this is enough, inform if something goes wrong



来源:https://stackoverflow.com/questions/54900674/not-able-to-remove-image-view-from-from-last-item-clicked-in-arrayadapter-textvi

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