I will try to be as clear as possible:
I have an application with a recyclerview, when the user clicks on a recyclerview item, he opens a new activity that starts playing a video directly.
In this new activity (the one that contains videoview) I have inserted three imagebutton to allow the user to share, like or download the video while watching it.
My question is how to define the listener of each imagebutton in order to perform a precise action on the selected recyclerview item.
So I would like the user to download the video he is looking at, ie the one that corresponds to the selected item.
Here is my recyclerview Adapter:
public class Adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context context;
private LayoutInflater inflater;
private List<Data> data= Collections.emptyList();
Data current;
private EditText editText;
private String GetName;
private int currentPos=0;
// create constructor to innitilize context and data sent from MainActivity
public Adapter(Context context, List<Data> data){
this.context=context;
inflater= LayoutInflater.from(context);
this.data=data;
}
// Inflate the layout when viewholder created
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view=inflater.inflate(R.layout.container, parent,false);
MyHolder holder=new MyHolder(view);
return holder;
}
// Bind data
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
// Obtenir la position actuelle de l'élément dans recyclerview pour lier les données et affecter des valeurs à partir de la liste
MyHolder myHolder= (MyHolder) holder;
Data current = data.get(position);
myHolder.textFishName.setText(current.catName);
myHolder.textSize.setText(current.sizeName);
myHolder.textSize.setVisibility(View.GONE);
myHolder.textType.setText(current.fishName);
myHolder.textPrice.setText("" + current.price + "");
myHolder.textPrice.setVisibility(View.GONE);
myHolder.textPrice.setTextColor(ContextCompat.getColor(context, R.color.colorAccent));
// load image into imageview using glide
Glide.with(context).load("http://192.168.43.196/vibe2/images/" + current.fishImage)
.placeholder(R.drawable.ic_menu_camera)
.error(R.drawable.ic_menu_camera)
.into(myHolder.ivFish);
}
// return total item from List
@Override
public int getItemCount() {
return data.size();
}
class MyHolder extends RecyclerView.ViewHolder {
TextView textFishName;
ImageView ivFish;
TextView textSize;
TextView textType;
TextView textPrice;
TextView counter;
TextView id;
ImageButton imageButton;
// create constructor to get widget reference
public MyHolder(View itemView) {
super(itemView);
editText = itemView.findViewById(R.id.editText);
textFishName = itemView.findViewById(R.id.textFishName);
ivFish = itemView.findViewById(R.id.ivFish);
textSize = itemView.findViewById(R.id.textSize);
textType = itemView.findViewById(R.id.textType);
textPrice = itemView.findViewById(R.id.textPrice);
counter = itemView.findViewById(R.id.counter);
}
}
}
My RecyclerView:
RecycleClick.addTo(recyclerView).setOnItemClickListener(new RecycleClick.OnItemClickListener() {
@Override
public void onItemClicked(RecyclerView recyclerView, int position, View v) {
Intent intent = new Intent(MainActivity.this, PlayVideo.class);
intent.putExtra("url", data.get(position).sizeName);
intent.putExtra("name", data.get(position).fishName);
intent.putExtra("titre", data.get(position).catName);
startActivity(intent);
overridePendingTransition(R.anim.activity_back_in, R.anim.activity_back_out);
finish();
}
});
Here is how I get and use the position in my second activity but it does not seem to work. no matter what element I click toast the condition is not true.
final int position = getIntent().getIntExtra("a",-1);
sparkButton1.setEventListener(new SparkEventListener(){
@Override
public void onEvent(ImageView button, boolean buttonState) {
if (position == 0) {
Toast.makeText(PlayVideo.this, "welcome", Toast.LENGTH_LONG).show();
}else if (position ==1){
Toast.makeText(PlayVideo.this, "exit", Toast.LENGTH_LONG).show();
}
}