update recycleview row from item onclick - java

I have a recycleview with 3 texts, is there a way to update specific item onclick that position, (i have quantity of each item, and i want to rise the quantity of that item onclick that position, 0,1,2,3...)
here is my receycleview:
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.RecyclerViewHolder> {
String [] Item_names,Price_items,Item_quantity;
public RecyclerAdapter(String [] Item_names,String [] Price_items,String [] Item_quantity)
{
this.Item_names=Item_names;
this.Price_items=Price_items;
this.Item_quantity=Item_quantity;
}
#Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_layout,parent,false);
RecyclerViewHolder recyclerViewHolder=new RecyclerViewHolder(view);
return recyclerViewHolder;
}
#Override
public void onBindViewHolder(final RecyclerViewHolder holder, final int position) {
holder.ItemNameRecycler.setText(Item_names[position]);
holder.ItemPriceRecycler.setText(Price_items[position]);
holder.ItemQuantityRecycler.setText(Item_quantity[position]);
}
#Override
public int getItemCount() {
return Item_names.length;
}
public static class RecyclerViewHolder extends RecyclerView.ViewHolder{
TextView ItemPriceRecycler,ItemNameRecycler,ItemQuantityRecycler;
public RecyclerViewHolder(View view)
{
super(view);
ItemNameRecycler=(TextView)view.findViewById(R.id.RowItemName);
ItemPriceRecycler=(TextView)view.findViewById(R.id.RowItemPrice);
ItemQuantityRecycler=(TextView)view.findViewById(R.id.RowItemQuantity);
ItemQuantityRecycler.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
}
}

You can use getAdapterPosition
From Documentation:
Returns the Adapter position of the item represented by this
ViewHolder.
in your code:
public static class RecyclerViewHolder extends RecyclerView.ViewHolder{
TextView ItemPriceRecycler,ItemNameRecycler,ItemQuantityRecycler;
public RecyclerViewHolder(View view)
{
super(view);
ItemNameRecycler=(TextView)view.findViewById(R.id.RowItemName);
ItemPriceRecycler=(TextView)view.findViewById(R.id.RowItemPrice);
ItemQuantityRecycler=(TextView)view.findViewById(R.id.RowItemQuantity);
ItemQuantityRecycler.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// get position
int pos = getAdapterPosition(); //get position here
}
});
}
EDIT
for updating the recylerview Item Textview in onClick()
public static class RecyclerViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView ItemPriceRecycler, ItemNameRecycler, ItemQuantityRecycler;
public RecyclerViewHolder(final View itemView) {
super(itemView);
ItemNameRecycler = (TextView) itemView.findViewById(R.id.RowItemName);
ItemPriceRecycler = (TextView) itemView.findViewById(R.id.RowItemPrice);
ItemQuantityRecycler = (TextView) itemView.findViewById(R.id.RowItemQuantity);
ItemQuantityRecycler.setOnClickListener(this);
}
// onClick Listener for view
#Override
public void onClick(View v) {
if (v.getId() == ItemQuantityRecycler.getId()) {
int pos = getAdapterPosition();
int value = pos + 1;
Toast.makeText(v.getContext(), "ITEM PRESSED = " + String.valueOf(getAdapterPosition()), Toast.LENGTH_SHORT).show();
ItemQuantityRecycler.setText(String.valueOf(value));
} else {
}
}
}

Related

Moving a recyclerviewer to different tab

I have 3 fragment(Todo, Ongoing , Finished)
A recyclerviewer populates the first fragment (ToDO). I want to move the recyclerview to the next tab (ongoing). Could not find appropriate documentation on this. Below is a screenshot of the UI
Code for adapter
public class recyclerKanbanAdapter extends RecyclerView.Adapter<recyclerKanbanAdapter.MyViewHolder> {
ArrayList<kanbanItems> kanlist;
Context context;
public recyclerKanbanAdapter(Context context,ArrayList<kanbanItems>kanbanList)
{
this.context=context;
this.kanlist=kanbanList;
}
public recyclerKanbanAdapter.MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(context).inflate(R.layout.fragment_itemkanban,parent,false);
return new recyclerKanbanAdapter.MyViewHolder(v);
}
#Override
public void onBindViewHolder(#NonNull recyclerKanbanAdapter.MyViewHolder holder, int position) {
kanbanItems kanban = kanlist.get(position);
holder.contractorName.setText(kanban.getContractorName());
holder.Duration.setText(kanban.getDuration());
holder.taskName.setText(kanban.getTaskName());
holder.btn_Delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
kanlist.remove(holder.getAdapterPosition());
notifyItemRemoved(holder.getAdapterPosition());
notifyItemRangeChanged(holder.getAdapterPosition(), kanlist.size());
}
});
holder.btn_Move.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//move to next tab
}
});
}
#Override
public int getItemCount() {
return kanlist.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder{
EditText taskName,Duration,contractorName;
ImageButton btn_Delete;
ImageButton btn_Move;
public MyViewHolder(#NonNull View itemView) {
super(itemView);
btn_Move=itemView.findViewById(R.id.btn_move);
btn_Delete=itemView.findViewById(R.id.btn_delete);
taskName= itemView.findViewById(R.id.txtTaskName);
Duration=itemView.findViewById(R.id.txtDuration);
contractorName=itemView.findViewById(R.id.txtContractor);
}
}
}

On Clicking one button, it Triggers another button in recycle view

I'm currently working on a attandance management Android app. In my recyclerview there are list of student detail being populated dynamically from database. When i click the first button in the list, it also change the button colour of the 11th button of the list Like wise to 2nd button click changes the colour of 12th View button
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
Context context;`enter code here`
List<StudentDetails> MainImageUploadInfoList;
public RecyclerViewAdapter(Context context, List<StudentDetails> TempList) {
this.MainImageUploadInfoList = TempList;
this.context = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_items, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
StudentDetails studentDetails = MainImageUploadInfoList.get(position);
holder.StudentNameTextView.setText(studentDetails.getName());
holder.StudentNumberTextView.setText(studentDetails.getPhoneNumber());
holder.st.setText(studentDetails.getRollno());
holder.cardView.setCardBackgroundColor(getcolor(position));
}
private int getcolor(int position) {
return Color.parseColor("#" + Integer.toHexString(ContextCompat.getColor(context, R.color.normal)));
}
#Override
public int getItemCount() {
return MainImageUploadInfoList.size();
}
class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView StudentNameTextView;
public TextView StudentNumberTextView;
public TextView st;
DatabaseReference databaseReference;
String se,wh,su,d;
Button b;
CardView cardView;
public ViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
StudentNameTextView = (TextView) itemView.findViewById(R.id.ShowStudentNameTextView);
StudentNumberTextView = (TextView) itemView.findViewById(R.id.ShowStudentNumberTextView);
st = (TextView) itemView.findViewById(R.id.studentrollno);
cardView=itemView.findViewById(R.id.cardview1);
se=Teacher.getInstance().getSe();
wh=Teacher.getInstance().getWh();
su=Teacher.getInstance().getSu();
d=Teacher.getInstance().getD();
b=itemView.findViewById(R.id.button3);
itemView.findViewById(R.id.button3).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View view ) {
int position=getAdapterPosition();
String message = String.valueOf(position) ;
databaseReference= FirebaseDatabase.getInstance().getReference("attandance").child(wh).child(se).child(su).child(d).child(message);
databaseReference.setValue("p");
itemView.findViewById(R.id.button3).setBackgroundColor(Color.GREEN);
itemView.findViewById(R.id.button4).setEnabled(false);
itemView.findViewById(R.id.button3).setEnabled(false);
Toast.makeText( context,"position is "+String.valueOf(position), Toast.LENGTH_SHORT).show();
}
});
itemView.findViewById(R.id.button4).setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(final View view ) {
int position=getAdapterPosition();
String message = String.valueOf(position) ;
databaseReference= FirebaseDatabase.getInstance().getReference("attandance").child(wh).child(se).child(su).child(d).child(message);
databaseReference.setValue("A");
itemView.findViewById(R.id.button4).setBackgroundColor(Color.RED);
itemView.findViewById(R.id.button3).setEnabled(false);
itemView.findViewById(R.id.button4).setEnabled(false);
}
});
}
#Override
public void onClick(View view) {
// int position=this.getAdapterPosition();
// Intent intent=new Intent(context,mark.class);
// String message = String.valueOf(position);
// intent.putExtra("attan",message);
// context.startActivity(intent);
// Intent intent = new Intent(context,mark.class);
//String message = String.valueOf(position) ;
//intent.putExtra("attan",message);
//context.startActivity(intent);
// Toast.makeText( context,"position is "+String.valueOf(position), Toast.LENGTH_SHORT).show();
}
}
}
}

How to implement mini equalizer on selected item of RecyclerView?

I have a recyclerview whose listitem is designed as follows
LinearLayout(Horizontal)
ImageView
TextView
ImageButton(PlayButton)
EqualizerView(Invisible)
Now when I click on PlayButton, it should hide and equalizer show be displayed and if I scroll the list, equalizer should stay on selected item only.
However, I am facing problem that EqualizerView is being displayed at interval of 10 items. That is if I click on 3rd item then equalizer will be displayed on 3rd,13th and so on..
I am using this https://github.com/claucookie/mini-equalizer-library-android library for implementing mini equalizer.
public class StationViewAdapter extends RecyclerView.Adapter<StationViewAdapter.StationViewHolder> {
List<Station> statons;
final private ListItemClickListener mOnClickListener;
public StationViewAdapter (List<Station> s, ListItemClickListener l)
{
statons = s;
mOnClickListener = l;
}
#NonNull
#Override
public StationViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item,parent,false);
return new StationViewHolder(v);
}
#Override
public void onBindViewHolder(#NonNull final StationViewHolder holder, int position) {
Station station = statons.get(position);
if(!station.getArtwork().isEmpty() && station.getArtwork() != null)
{
try{
Picasso.get().load(Uri.parse(station.getArtwork()))
// .placeholder(R.mipmap.ic_launcher)
.into(holder.stationArtwork);
} catch (Exception e) {
e.printStackTrace();
holder.stationArtwork.setImageResource(R.mipmap.ic_launcher);
}
}
else
holder.stationArtwork.setImageResource(R.mipmap.ic_launcher);
holder.stationName.setText(station.getName());
}
#Override
public int getItemCount() {
return statons.size();
}
public class StationViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
ImageView stationArtwork;
TextView stationName;
ImageButton stationPlayButton;
EqualizerView equalizerView;
int selectedItem;
public StationViewHolder(#NonNull View itemView) {
super(itemView);
stationArtwork = itemView.findViewById(R.id.favicon);
stationName = itemView.findViewById(R.id.text);
stationPlayButton = itemView.findViewById(R.id.play_button);
equalizerView = itemView.findViewById(R.id.equalizer_view);
itemView.setOnClickListener(this);
stationPlayButton.setOnClickListener(this);
selectedItem = getAdapterPosition();
}
#Override
public void onClick(View v) {
int clickPosition = getAdapterPosition();
Log.d(TAG, "onClick: "+getAdapterPosition());
Log.d(TAG, "onClick: "+getLayoutPosition());
Log.d(TAG, "onClick: "+getItemId());
if(v == itemView) {
mOnClickListener.onListItemClick(clickPosition);
}else if(v == stationPlayButton)
{
mOnClickListener.onListItemPlay(clickPosition,stationPlayButton,equalizerView);
}
}
}
public interface ListItemClickListener {
void onListItemClick(int clickedItemIndex);
void onListItemPlay(int clickItemIndex,ImageButton i,EqualizerView eq);
}
}
These are ClickListeners in Main Activity
#Override
public void onListItemClick(int clickedItemIndex) {
if(mToast !=null)
{
mToast.cancel();
}
mToast = Toast.makeText(this,"Main Activity item clicked at position"+clickedItemIndex,Toast.LENGTH_SHORT);
mToast.show();
}
#Override
public void onListItemPlay(int clickItemIndex,ImageButton i , EqualizerView eq) {
i.setVisibility(View.GONE);
eq.setVisibility(View.VISIBLE);
eq.animateBars();
}
RecyclerView reuses ViewHolder instances. You need to invalidate ViewHolder state inside onBindViewHolder. In your case you should hide EqulizerView at least:
#Override
public void onBindViewHolder(#NonNull final StationViewHolder holder, int position) {
equalizerView.setVisibility(View.INVISIBLE);
....
}
Also, you should save which songs are playing to initialize ViewHolder state later.
More info at: https://developer.android.com/guide/topics/ui/layout/recyclerview#structure

How to add a onItemclicklistener method to recycleview of buttons

I want to make a onItemclicklistener method to recycleview of buttons and every button intent to Activity
but the Activity has Textview (every button has different Textview )
public class RecAdapter extends RecyclerView.Adapter<RecAdapter.ViewHolder> {
public class ViewHolder extends RecyclerView.ViewHolder {
Button button;
public ViewHolder(View itemView) {
super(itemView);
button = itemView.findViewById(R.id.buttonn);
}
}
private Context context;
private List<items> items;
public RecAdapter(Context c, List<items> itemList) {
this.context = c;
items = itemList;
}
#Override
public RecAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.buttonss, parent, false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(RecAdapter.ViewHolder holder, int position) {
items p = items.get(position);
holder.button.setText(p.getName());
}
#Override
public int getItemCount() {
return items.size();
}
}`
Add OnClickListener on itemView of holder like below:
#Override
public void onBindViewHolder(RecAdapter.ViewHolder holder, int position) {
items p = items.get(position);
holder.button.setText(p.getName());
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Do here
}
});
}

How to set onClickListener on RecyclerView

I have a simple problem that I can't set onClickListener on recyclerview , i don't want to set onClickListener on items of recyclerview , just the recyclerview itself , I tried the ordinary way and it didn't work
( i tried to set an onClickListener on the parent Layout but the strange things it doesn't work because of the LinearLayoutManager that i set on recyclerview :| )
here's my code :
mRecyclerView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("onClick","Clicked");
}
});
Adapter :
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
private List<Message> mMessage;
public static class ViewHolder extends RecyclerView.ViewHolder{
private TextView txtSender;
private TextView txtMessage;
public ViewHolder(View v) {
super(v);
txtSender = (TextView) v.findViewById(R.id.txtSender);
txtMessage = (TextView) v.findViewById(R.id.txtText);
}
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View inflatedView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.recyclerview_item_row, parent, false);
return new ViewHolder(inflatedView);
}
public RecyclerAdapter(List<Message> messages) {
mMessage = messages;
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
Message msg = mMessage.get(position);
holder.txtSender.setText(msg.getSender());
holder.txtMessage.setText(msg.getmessage());
}
#Override
public int getItemCount() {
return mMessage.size();
}
As per your need, you need to modify R.layout.recyclerview_item_row. Use a android LinearLayout put both of TextView(R.id.txtSender and R.id.txtText) inside that.
Modify your ViewHolder as below:
public static class ViewHolder extends RecyclerView.ViewHolder
{
private LinearLayout lvContainer;
private TextView txtSender;
private TextView txtMessage;
public ViewHolder(View v) {
super(v);
lvContainer = (LinearLayout) v.findViewById(R.id.lv_container); // R.id.lv_container this should be in your R.layout.recyclerview_item_row
txtSender = (TextView) v.findViewById(R.id.txtSender);
txtMessage = (TextView) v.findViewById(R.id.txtText);
}
}
Modify your onBindViewHolder as below
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
Message msg = mMessage.get(position);
holder.txtSender.setText(msg.getSender());
holder.txtMessage.setText(msg.getmessage());
holder.lvContainer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Handle your click here
}
});
}

Categories