I have multiple songs in recyclerview. I have to implement autoplay feature like normal media player when one song completes the next must play on next position in recyclerview.
Thanks for any help.
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
focusedItem = getLayoutPosition();
Log.d("TAG_LIST", "Element " + focusedItem + " clicked.");
}
});
Related
I have created a Custom Adapter and applied it to listView, but when I click a CheckTextView item the .setChecked() method won't change the state of the radio button on the UI.
int getPosition = position;
checkedTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (checkedTextView.isChecked()) {
Log.i("Username", Title.get(getPosition) + " is no longer checked");
checkedTextView.setChecked(false);
} else {
Log.i("Username", Title.get(getPosition) + " is checked");
checkedTextView.setChecked(true);
}
}
});
However the logs are showing that the view is being checked and unchecked.
What am I missing?
Thanks
I dont know why its happening but i have an idea to implement it in another way
Assuming your are using custom adapter to load list items
set OnClickListener for CheckedTextView at the time of view creation in your custom adapter
final CheckedTextView ct = (CheckedTextView) view
.findViewById(R.id.checkedTextView);
ct.setText("text");
ct.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// do what ever you want to perform here
if(ct.isChecked()){
} else {
}
}
});
I want the focus to move automatically to next row position in Recyclerview. My Recyclerview contains a videoview. On media completion, I want the next videoview to be played.Videoview is playing next video but only in first position videoview
private static int position= 0;
public static class ViewHolder extends RecyclerView.ViewHolder {
private VideoView videoView;
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
position= getAdapterPosition();
//recyclerView.smoothScrollToPosition(position);
Log.d("TAG_LIST", "Element " + position+ " clicked.");
}
});
}
holder.videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
//recyclerView.smoothScrollToPosition(position);
Video(holder);
}
});
private void Video(ViewHolder holder) {
if (++position>= items.size()) {
// Last song, just reset position
position= 0;
} else {
// Play next song
recyclerView.smoothScrollToPosition(position);
AsyncTask task = new AsyncTask (items.get(position).getUrl(), holder,position);
task.execute(items.get(position).getUrl());
}
}
You didnt post any code related to your problem but I can suggest you some methods for this.
Find first visible item position on RecyclerView and start video at your VideoView
Listen scroll changes on your RecyclerView and when first visible item index changed, stop your video, and start next RecyclerView item VideoView
I have recently started implementing RecyclerView. I have implemented horizontal recyclerview with image and imagebutton. I want to know position of which image or imagebutton is clicked. I could able to see six images horizontally in the view.
App crashes in the following line:
Toast.makeText(context, "You Clicked Image Button " + position, Toast.LENGTH_LONG).show();
Implementation:
#Override
public void onBindViewHolder(ViewHolder viewHolder, int pos) {
final int position = pos;
viewHolder.imgViewIcon.setImageResource(itemsData[position].getImageUrl());
viewHolder.imgBtn.setImageResource(R.drawable.ic_close_black_24dp);
viewHolder.imgBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Crashes in the following line
Toast.makeText(context, "You Clicked Image Button " + position, Toast.LENGTH_LONG).show();
((FrameLayout) v.getParent()).removeAllViews();
}
});
}
As you saying no logcat showing for ERROR I am only assuming that your context is null.
try this solution
if(context != null)
Toast.makeText(context, "You Clicked Image Button " + position, Toast.LENGTH_LONG).show();
else
Log.d("You Clicked Image Button", "Position : " + position);
Try the following Code, Hope it works
Toast.makeText(context,"item Clicked at "+getPosition(), Toast.LENGTH_LONG).show();
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.
Im new to android and Im wondering if there is a standard method which is used to accomplish the retrieval of all the views' positions with in a GridLayout.
I'm dynamically adding and removing views from a GridLayout like this
addButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
addButton.setVisibility(View.INVISIBLE);
Button newButton = new Button(LayoutAnimationsByDefault.this);
gridContainer.getColumnCount();
newButton.setText(String.valueOf("position#"+"huh"));
newButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
gridContainer.removeView(v);
addButton.setVisibility(View.VISIBLE);
}
});
gridContainer.addView(newButton, Math.min(-1, gridContainer.getChildCount()));
}
});
I've also tried this..
addButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
addButton.setVisibility(View.INVISIBLE);
Button newButton = new Button(LayoutAnimationsByDefault.this);
Object tag =v.getTag();
if(tag!=null)
{
int position = (Integer)tag;
Log.v("onClick", "Position: " + position);
newButton.setTag(position);
newButton.setText(String.valueOf("position#"+position));
TextView text1 = (TextView) newButton;
String message = "positionis" + position + text1.getText().toString();
Toast.makeText(LayoutAnimationsByDefault.this, message, Toast.LENGTH_LONG).show();
}
newButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
gridContainer.removeView(v);
addButton.setVisibility(View.VISIBLE);
}
});
gridContainer.addView(newButton, Math.min(-1, gridContainer.getChildCount()));
gridContainer.getColumnCount();
}
This creates a new button,but with nothing inside, not text or anything
Im trying to figure out how to update and retrieve the updated position of each view in the gridlayout, each time an item is added, removed re-arranged in the GridLayout.
By position I don't mean x,y coordinates, but the 0,1,2,3,4 position as seen with in listviews.
I've tried getColumnCount(); getChildAt(); ActionListener() ActionPerformed() etc.
Any help, pointers, hints?Thanks!!