有什么方法可以在更改回收器视图之前对其进行操作

时间:2019-04-25 06:39:47

标签: android android-recyclerview

我有一个recyclerview,它在cardview中有2个imageview用于播放和停止按钮。我正在使用onclick方法来更改播放图像以通过setImageSrouce暂停。我的问题是,单击任何其他行的图像按钮(播放图像)后,我无法将上一行的imageview更改回播放按钮。如果有人可以解决这个问题,那就太好了。

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder>{
private static final String TAG = "RecyclerViewAdapter";
    private ArrayList<String> songnames=new ArrayList<>();
    private ArrayList<Integer> mFlag=new ArrayList<>();
    private ArrayList<Integer> song=new ArrayList<>();
    private ArrayList<Integer> mplay=new ArrayList<>();
    private ArrayList<Integer> mstop=new ArrayList<>();
    private ArrayList<Integer> mpause=new ArrayList<>();
    MediaPlayer player;
public static int path;
ArrayList<Integer> msong, ArrayList<Integer> mplay,ArrayList<Integer> mstop
private Context mcontext;
int i=0;
private ArrayList<ItemData> itemData;
    private int position;
    private Object payload;
    public RecyclerViewAdapter(Context mcontext, ArrayList<String> msongnamesnames, ArrayList<Integer> flag, ArrayList<Integer> msong, ArrayList<Integer> mplay, ArrayList<Integer> mstop, ArrayList<Integer> pause) {
        this.mcontext = mcontext;
        this.songnames = msongnamesnames;
        this.mFlag = flag;
        this.song=msong;
        this.mplay=mplay;
        this.mstop=mstop;
        this.mpause=pause;
    }
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
       View view= LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_list_item,viewGroup,false);
       ViewHolder holder = new ViewHolder(view);
       return holder;
    }                           
    @Override
    public void onBindViewHolder(@NonNull final ViewHolder viewHolder, final int position) {
        viewHolder.songname.setText(songnames.get(position));
        viewHolder.flag.setImageResource(mFlag.get(position));
        viewHolder.play.setImageResource(mplay.get(position));
        viewHolder.stop.setImageResource(mstop.get(position));
        viewHolder.play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                path=song.get(position);
                play(v, viewHolder, position);
                Log.d(TAG, "onClick: position is " + mpause);
            }
        });

        viewHolder.stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                stopPlayer(viewHolder, position);
            }
        });
    }
public boolean checkimage(ViewHolder viewHolder) {
        String imagename= viewHolder.play.getResources().getResourceEntryName(R.id.playbutton);
return  true;
}
    @Override
    public int getItemCount() {
        return songnames.size() ;
    }
    public class ViewHolder extends RecyclerView.ViewHolder {
TextView songname;
ImageView flag;
ImageView play;
ImageView stop;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            songname=itemView.findViewById(R.id.songname);
            flag=itemView.findViewById(R.id.FlagHolder);
         play=itemView.findViewById(R.id.playbutton);
              stop=itemView.findViewById(R.id.stopbutton);
        }
  }
    public void play(View v, final ViewHolder viewHolder, final int position) {
        if(player!=null) {
            Log.d(TAG, "Here position is" + position );
            player.release();
            player = null;
            for(int i=0; i<songnames.size(); i++) {
                viewHolder.play.setImageResource(mplay.get(i));

            }
        }
        if (player == null) {
            player = (MediaPlayer) MediaPlayer.create(mcontext, path);
                    viewHolder.play.setImageResource(mpause.get(position));
            player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    stopPlayer(viewHolder, position);
                }
            });
        }
        player.start();
    }
    public void pause(View v) {
        if (player != null) //is called to check a user is pressing the pause button which started the song
        {
            player.pause();
        }
    }
    public void stop(ViewHolder viewHolder, int position) {
        stopPlayer(viewHolder, position);
    }
    private void stopPlayer(ViewHolder viewHolder, int position) {
            viewHolder.play.setImageResource(mplay.get(position));
        if (player != null) {
            player.release();
            player = null;
            Toast.makeText(mcontext, "MediaPlayer released", Toast.LENGTH_SHORT).show();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

在模型中添加一个布尔值,

boolean isPlaying = false; //with getter setter
onBindViewHolder()中的

这样做,

if(songnames.get(position).isPlaying())
    viewHolder.play.setImageResource(R.drawable.play);
else
    viewHolder.play.setImageResource(R.drawable.stop);

viewHolder.play.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                  for(int i = 0; i<songnames.size(); i++)
                     songnames.get(i).setIsPlaying(false);
                  songnames.get(viewHolder.getAdapterPosition()).setIsPlaying(true);
                  notifyDataSetChanged();
                }
            });

建议

在适配器中不要使用那么多的数组列表

 private ArrayList<String> songnames=new ArrayList<>();
    private ArrayList<Integer> mFlag=new ArrayList<>();
    private ArrayList<Integer> song=new ArrayList<>();
    private ArrayList<Integer> mplay=new ArrayList<>();
    private ArrayList<Integer> mstop=new ArrayList<>();
    private ArrayList<Integer> mpause=new ArrayList<>();

避免这样做,为所有必填字段创建一个model,并在arraylist中使用此模型。