This might have been asked plenty of times already but I can't seem to get an actual answer to this. I have an adapter that takes data from an API and converts it into a listview. The row has its own Play button in it. When clicking on the play button, inside the row, it should change the button to a pause button. When I click on a different row's play button, the last button clicked should go back to a play button. I know it's setimageresource on the buttons but I can't seem to track my last button clicked. Here are my codes:
This is part of the adapter and
#Override
public View getView(final int pos, View ConvertView, ViewGroup parent) {
final MyViewHolder holder;
if (ConvertView == null){
ConvertView = layoutinflater.inflate(R.layout.row, parent, false);
holder = new MyViewHolder();
holder.plays = (ImageButton) ConvertView.findViewById(R.id.plays);
holder.plays.setFocusable(false);
play = playdata.get(pos);
holder.posturl= play.getposturl();
mStartPlaying = true;
player = new MediaPlayer();
// player.setOnCompletionListener(onCompletion);
// player.setOnErrorListener(onError);
holder.plays.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String url = holder.posturl;
if (!mStartPlaying){
holder.plays.setImageResource(R.drawable.ic_action_stop);
startPlaying(url);
mStartPlaying = true;
} else{
holder.plays.setImageResource(R.drawable.ic_action_play);
player.stop();
mStartPlaying = false;
}
}
private void startPlaying(String url) {
// TODO Auto-generated method stub
try {
player.reset();
player.setDataSource(url);
// mPlayer.setDataSource(mFileName);
player.prepareAsync();
player.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
player.start();
}
});
} catch (IOException e) {
Log.e("preparefailed", "prepare() failed");
}
}
});
if (!player.isPlaying()){
holder.plays.setImageResource(R.drawable.ic_action_play);
}
ConvertView.setTag(holder);
ConvertView.setOnClickListener(new OnClickListener() {
public void onClick(View v){
Log.d("hi", "hi from "+ pos);
}
});
}
else{
holder = (MyViewHolder)ConvertView.getTag();
}
play = playdata.get(pos);
holder.play = play;
return ConvertView;
}
This is my main activity:
mLastClickedPosition = -1;
this.PlayList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick (AdapterView<?> parent, View view, int position, long id) {
if(mLastClickedPosition != -1){
// do something to pause the item in your list at this position
}
// next, update mLastClickedPosition
mLastClickedPosition = position;
if(position==0){
ImageView imageView = (ImageView) view.findViewById(R.id.plays);
imageView.setImageResource(R.drawable.ic_action_stop);
}
// play audio
}
});
the mediaplayer is initialized in the adapter, I'm not really sure how I'd go about initializing it in the main activity instead. Any help is appreciated! Thanks in advanced!
If each row in the ListView know what state it is in(butten), You could control each row in the ListView using listeners or LocalBroadcastManager. Try this. That is a ListView skeleton to start thinking in OOP where each row really becomes an object that could know what state it is in . A ListView abstraction example that makes it easier to understand/see the java code as objects.
From the example above, let say that every row(EventItem) that are created are a listener or register for LocalBroadcastManager Then it would be easy to address each row. Talk to each row and ask it to stop having the button in a pause state, or whatever
...I'm updating this answer since the link above doesn't work. Here's a new link to a gethub project
listview-with-multiple-layouts
You should follow what Erik suggested and delegate the handling to each row.
BTW, could the problem be because of two listeners you have? You are maintaining the state in main activity which may not even be called. If this is not happening, you may need to move the maintaining state to the list listener (saving the last position).
But, follow what Erik suggested and that should help you to easy debugging and maintenance.
Good Luck.
Related
The ViewHolder I have contains views that are needed for each item of my RecycleView, which are two TextViews, PlayPauseView, and AppCompatSeekBar. In the constructor I have initialized them to their ids of my layout, and is ready for my Adapter class to extend it. The problem is I have this PlayPauseView that needs to be toggled at the correct position for it to function right, because if I press the view at another position, the previous position is still toggled. Here's an image for better understanding.
The yellow means that it is currently playing, but two cannot play at the same time so I was hoping to get the position of the view inside the RecyclerView item.
This is from the Holder class
TextView mInstrumentalName, mProducer;
PlayPauseView mPlayButton; // Need to add pause/stop
AppCompatSeekBar mMusicSeekbar;
ItemClickListener itemClickListener;
public TrackHolder(View itemView) {
super(itemView);
mInstrumentalName = (TextView) itemView.findViewById(R.id.instrumental_name);
mProducer = (TextView) itemView.findViewById(R.id.producer);
mPlayButton = (PlayPauseView) itemView.findViewById(R.id.play_pause);
mMusicSeekbar = (AppCompatSeekBar) itemView.findViewById(R.id.music_seekbar);
itemView.setOnClickListener(this);
}
This is from the Adapter class
#Override
public void onBindViewHolder(#NonNull final TrackHolder holder, final int position) {
holder.mInstrumentalName.setText(tracks.get(position).getInstrumentalName());
holder.mProducer.setText(tracks.get(position).getProducer());
holder.mPlayButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
holder.mPlayButton.toggle();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
if (mediaPlayer.isPlaying()) {
mediaPlayer.reset();
try {
mediaPlayer.setDataSource(tracks.get(position).getMusicLink());
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
mediaPlayer.start();
}
});
mediaPlayer.prepareAsync();
} else {
try {
mediaPlayer.setDataSource(tracks.get(position).getMusicLink());
previous_link = tracks.get(position).getMusicLink();
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
mediaPlayer.start();
}
});
mediaPlayer.prepareAsync();
}
}
});
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
mediaPlayer.release();
}
});
holder.setItemClickListener(new ItemClickListener() {
#Override
public void onItemClick(View v, int pos) {
Toast.makeText(context, Integer.toString(tracks.size()), Toast.LENGTH_SHORT).show();
}
});
}
If you want to get position in your view holder class then you can call getAdapterPosition() inside your class. this will return you the clicked position in view holder class
I finally found a solution. So what I did instead of focusing on the views inside of the ViewHolder, I refreshed/updated the whole item in the RecyclerView, meaning it will refresh the toggle state of the previous item. Here's a code snipped if anyone in the future has any trouble on the same question.
private int start_notifying = 0;
private int previous_pos = -1;
So start notifying is at 0 because you don't want to refresh the item until you toggled the play button at least once, and set the current position to become the previous position when you want to press a different list item. You then increment so you can start refreshing items on the next item pressed. Using notifyItemChanged(previous_pos) refreshed the item of the previous position that was pressed, making the play button go back to its normal state.
if (start_notifying > 0) {
notifyItemChanged(previous_pos);
previous_pos = position;
} else {
previous_pos = holder.getAdapterPosition();
Log.d("previous_pos", Integer.toString(holder.getAdapterPosition()));
start_notifying++;
}
Hi i have recyclerView row item consists of a play Button(Which change to stop and Play as toggle) and a play seek bar and TextView.
The each row plays different audios .
When i click on play button of respective row i am able to stop the other row audio ..
Addressing to the problem ..
When i am playing a audio of a respective row, the button of earlier row has to stow a play symbol (in the sense it has to change the state ) please help me how to update the other rows of recyclerView when the other item is clicked
#Override
public void onBindViewHolder(final ViewHolder holder, final int i) {
if (holder.viewType == ROW_TYPE){
// holder.play_button.setImageResource(R.drawable.play_button);
if(i == selected_item){
}
else {
// holder.play_button.setImageResource();
}
JSONObject obj;
String mp3_url = null, caption = null, thumbnail_url = null;
try {
obj = voicesArray.getJSONObject(i);
mp3_url = obj.getString("mp3_url");
caption = obj.getString("caption");
thumbnail_url = obj.getString("thumbnail_url");
} catch (JSONException e) {
e.printStackTrace();
}
holder.list_text.setText(caption);
imageLoader.DisplayImage(thumbnail_url, holder.list_image);
final String finalMp3_url = mp3_url;
holder.play_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
selected_item = i;
int ii = holder.getOldPosition();
// holder.play_button.setImageResource();
// updateItem(ii);
if(player != null){
player.reset();
holder.play_button.setImageResource(R.drawable.play_button);
myAudioPlay(holder, finalMp3_url);
}
else {
myAudioPlay(holder,finalMp3_url);
/*if (player == null) {
player = new MediaPlayer();
playAudiFile(finalMp3_url);
holder.play_button.setImageResource(R.drawable.pause_button);
} else if (player.isPlaying()) {
holder.play_button.setImageResource(R.drawable.play_button);
player.stop();
Log.d("player", "" + player);
player.reset();
} else {
holder.play_button.setImageResource(R.drawable.pause_button);
playAudiFile(finalMp3_url);
}*/
}
}
});
}
}
Apparently the purpose of a recyclerView is to recycle the views to minimize memory usage required for rendering view so setting an image in the view is not an apt solution,Their are many solutions for your problem the One i would pick(Pseudo code) is given below
Step 1. Create a Boolean value in your model class
bool isPlay=true;
Step 2. When you are playing an audio loop through your Model for the recycler adapter and set the value for isPlay accourdingly
for(object obj in listOfModels)
if(obj.Id!=idOfthePlayingView)
obj.isPlay=false;
Then notify your adapter
yourRecyclerAdapterObject.notifyDatasetChanged()
I have created a listview which consists of list of tracks and play and pause image.When clicked on play image, pause image becomes visible and clicking on pause ,play image will become visible.Issue is that when i click on play and scrolls down i see another list item showing pause image and when i scroll down more another list item showing pause image.It is because list recylces an shows duplicated images.On studying the issue i think i need to place if-else statement in the getView method but i failed to implement correct code to resolve this issue..plzz help me with code in tht..
Create a new boolean variable in your SoundCloud class by the name isPlaying. Create getter setters for it, like you have done for other variables.
Get the object from SoundCloudList like this:
SoundCloug soundCloud = (SoundCloud) getItem(position);
This object can be used everywhere you are using soundcloudList.get(position), so that makes it easy to because we don't have to fetch object everytime.
Then in your getView use isPlaying to show play/pause button on every position like below:
if(soundCloud.isPlaying()){
holder.img1.setVisibility(View.VISIBLE);
holder.img2.setVisibility(View.GONE);
}
else{
holder.img1.setVisibility(View.GONE);
holder.img2.setVisibility(View.VISIBLE);
}
and then in your onClickListeners, set values for isPlaying like this:
holder.img1.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(final View v) {
soundCloud.setPlaying(true);
try {
notifyDataSetChanged();
holder.img1.setVisibility(View.INVISIBLE);
holder.img2.setVisibility(View.VISIBLE);
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setDataSource("http://api.soundcloud.com/tracks/" + soundcloudList.get(position).getId() + "/stream?client_id=e13865f9debacb5f96375fdd96b7fa1b");
mMediaPlayer.prepareAsync();
mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener()
{
#Override
public void onPrepared(MediaPlayer mp)
{
mp.start();
}
});
mMediaPlayer.setOnCompletionListener(
new MediaPlayer.OnCompletionListener()
{
#Override
public void onCompletion(MediaPlayer mp)
{
mMediaPlayer.release();
mMediaPlayer = null;
holder.img1.setVisibility(View.VISIBLE);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
});
holder.img2.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(final View v)
{
soundCloud.setPlaying(false);
notifyDataSetChanged();
holder.img1.setVisibility(View.VISIBLE);
holder.img2.setVisibility(View.INVISIBLE);
if(mMediaPlayer!=null)
{
mMediaPlayer.release();
mMediaPlayer = null;
}
}
});
This code will not show the play/pause button duplicated on multiple rows, because now, we are checking the play/pause of audio for every row in getView and only then setting the buttons visibility.
I am trying to implement a select list. Each item is an imageview.
When the user clicks a view, a custom dialog opens up showing the 56 ImageViews in a list.
The user can then click on one to select it.
The imageviews have images named like this items_r1_c1 ... items_r56_c1.
I have to implement onClickListeners to each of the imageviews.
Instead I did this:
private int i; // This is int the outer class.
...
private ImageView [] spec = new ImageView[56];
myView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//set up dialog
try {
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.myCustomList);
dialog.setTitle("Select one of 56");
dialog.setCancelable(true);
dialog.show();
String s = null;
//This is where I automate the ImageView object creation
for (i=2; i<=56; i++) {
s = "items_r"+Integer.toString(i)+"_c1";
spec[i] = (ImageView) findViewById(getResources().getIdentifier(s,"drawable",getPackageName()));
spec[i].setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
myItem.setItem(Integer.toString(i));
if(i == 0) myItem.setItem("invalid");
Log.e(tag, myItem.getItem());
dialog.dismiss();
}
});
}
} catch (Exception e) {
Log.e(tag, e.toString());
}
}
However I am not getting the behavior I expected.
What am I doing wrong? What is the efficient way of doing this instead of writing 56 onClick listeners.
Thank you.
For starters I'd put the for( ... ) loop before the call to dialog.show().
To answer your more general question, look at the method ListView.setOnItemClickListener() http://developer.android.com/reference/android/widget/AdapterView.html#setOnItemClickListener(android.widget.AdapterView.OnItemClickListener).
You'd only have to register one listener on your ListView. When an item in your ListView is clicked you'd end up in the code under void onItemClick(AdapterView<?> parent, View view, int position, long id). The position would be the index of the row clicked (corresponding to i in your loop).
I have a phrasebook, with the ability to save the sample to SD. I use a Gridview set up with the following code in place for the button adapter:
public View getView(int position, View convertView, ViewGroup parent) {
try {
final Sample sample = board.getSamples().get(position);
if (sample != null) {
Button button = new Button(context);
button.setText(sample.getName());
button.setTextColor(Color.WHITE);
button.setTextSize(12);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
context.play(sample);
}
});
// TODO Implement this correctly.
button.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View v) {
return context.saveToSD(sample);
}
});
return button;
}
} catch (IndexOutOfBoundsException e) {
Log.e(getClass().getCanonicalName(), "No sample at position "
+ position);
}
return null;
}
I am looking to integrate a context menu here on a Long press, to give the option of where to save the sample. I don't seem to be able to register the button for the context menu within this method (ie registerForContextMenu(button), as it gives me errors.
I am a bit stumped here, any pointers would be a great help.
Thanks
I take it that this is an old post but I came across it today as I was looking for an answer on the same topic. Like the question here I have a grid of items and wanted to show a context menu on long click.
I am not using a contextmenu but instead I am using an AlertDialog.
gridview.setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long id)
{
showOptionsMenu(position);
return true;
}
});
public void showOptionsMenu(int position)
{
new AlertDialog.Builder(this)
.setTitle("test").setCancelable(true).setItems(R.array.myOptions,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialoginterface, int i) {
//take actions here according to what the user has selected
}
}
)
.show();
}
Hope this helps.