无法从ArrayAdapter TextView中单击的最后一项中删除图像视图 [英] Not able to remove image view from from last item clicked in ArrayAdapter TextView

查看:78
本文介绍了无法从ArrayAdapter TextView中单击的最后一项中删除图像视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我有一个歌曲列表",当单击文本视图时会播放一首歌曲,并在歌曲名称旁边显示一个gif(音频频谱).我想要的是,当我单击歌曲列表"中的另一首歌曲时,上次单击的歌曲应停止(它能够执行此操作).但是与此同时,我也希望上一首歌曲的gif消失,这是我无法做到的.这是我的代码-

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;

    }
}

这是演示的视频链接-

演示链接

推荐答案

SongList类中将GifImageView lastBass;声明为全局变量. 现在,如下修改您的songListTextView的onClickListener代码:

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();

                    }

            }

        });

更新 在上面的代码中,在bass.setImageResource(R.drawable.bass);之后添加bass.setVisibility(View.VISIBLE);

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

这篇关于无法从ArrayAdapter TextView中单击的最后一项中删除图像视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆