Handling click on image button of listview from activity - android

I have a list view of songs with a play button
..
Now I need to click on this play buttons based on their positions and after click change the selected one into pause button .. but i have to do this inside my activity not in the adapter(getView).. Any suggestions !?
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//productList is my ArrayList<Product> and
//Product is my Serializable class that holds
// items that is in mysql database
Product selectedProduct = productList.get(position);
}
});

Firstly, use a recyclerview instead of a listview. Afterwards, inside the adapter in onBindViewHolder method, handle clicks by addind a click listener to holder :
holder.setOnClickListener(...);
interface:
public interface OnItemClicked{
void onItemClicked(/*HERE YOUR VARIABLES TO PASS FROM ADAPTER TO MAINACTIVITY);
}
in adapter:
OnItemClicked listener;
public void setOnItemClickedListener(OnItemClicked listener){
this.listener = listener;
}
downwards, on onBindViewHolder method :
#Override
public ViewHolder onBindViewHolder(... params){
...your code
holder.setOnClickListener(new View.OnClickListener{
#Override
public void onClick(View v){
if(listener != null)
listener.onItemClicked()
}
});
}
and, finally, in your MainActivity:
adapter.setOnItemClickedListener(new OnItemClicked ... ) <- here will pop up autocomplete

use this in your activity
Declare RecyclerView and set Layout Manager
set Adapter
view.setAdapter(mAdapter);
perform on click
v
view.addOnitemTouchListener(
new RecyclerItemClickListener(getApplicationContext(), new RecyclerItemClickListener.OnItemClickListener() {
#Override
public void onItemClick(View view, int position) {
//do something
}
})
);
RecyclerItemClickListener :
public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener {
private OnItemClickListener mListener;
public interface OnItemClickListener {
public void onItemClick(View view, int position);
}
GestureDetector mGestureDetector;
public RecyclerItemClickListener(Context context, OnItemClickListener listener) {
mListener = listener;
mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
#Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}
});
}
#Override
public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) {
View childView = view.findChildViewUnder(e.getX(), e.getY());
if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) {
mListener.onItemClick(childView, view.getChildAdapterPosition(childView));
}
return false;
}
#Override
public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) {
}
#Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
}

Related

Need suggestion for handling click events on RecyclerView

I have a RecyclerView which spawns a CardView containing another RecyclerView.
The parent RecyclerView Adapter sets the child RecyclerView Adapter in order to render the following image. (The red text are added to show which position):
My question is how do I get the positions of what was clicked such as Parent 0 and Child 0 to signify the first parent RecyclerView with the first child RecyclerView was clicked?
ViewHolder item is your list item so implement on click listener on it
and you can get clicked position on item click as:
class ViewHolder extends RecyclerView.ViewHolder implements
View.OnClickListener {
ViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View view) {
System.out.println("clicked position "+getAdapterPosition());
}
Create a custom RecyclerView Listener
public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener {
private OnItemClickListener mListener;
public interface OnItemClickListener {
public void onItemClick(View view, int position);
}
GestureDetector mGestureDetector;
public RecyclerItemClickListener(Context context, OnItemClickListener listener) {
mListener = listener;
mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
#Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}
});
}
#Override
public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) {
View childView = view.findChildViewUnder(e.getX(), e.getY());
if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) {
mListener.onItemClick(childView, view.getChildAdapterPosition(childView));
}
return false;
}
#Override
public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) {
}
#Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
}
then in your activity implement above class
recyclerview.addOnItemTouchListener(new RecyclerItemClickListener(getActivity(), new RecyclerItemClickListener.OnItemClickListener() {
#Override
public void onItemClick(View view1, int position) {
CardView item_card = (CardView) view1.findViewById(R.id.item_card);
item_card.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view2) {
//you code here
}
});
}
}));

how to handle both RecyclerView on item click listener for its rows and the views inside it

I have recyclerView which contains some text view and 2 image view.
I would like to do a task when user clicks on each row and another task when user clicks on each of image views .. but I can only handle one of them at the same time.
Now if I set recylerview.addOnItemTouchListener I can't handle images views onclicks with setting on clicklistener for them in the adapter because when user clicks on image view the row's click listener will trigger.
Below code is my class for handling recycler view items
public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener {
private OnItemClickListener mListener;
public interface OnItemClickListener {
public void onItemClick(View view, int position);
public void onLongItemClick(View view, int position);
}
GestureDetector mGestureDetector;
public RecyclerItemClickListener(Context context, final RecyclerView recyclerView, OnItemClickListener listener) {
mListener = listener;
mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
#Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}
#Override
public void onLongPress(MotionEvent e) {
View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
if (child != null && mListener != null) {
mListener.onLongItemClick(child, recyclerView.getChildAdapterPosition(child));
}
}
});
}
#Override public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) {
View childView = view.findChildViewUnder(e.getX(), e.getY());
if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) {
mListener.onItemClick(childView, view.getChildAdapterPosition(childView));
return true;
}
return false;
}
#Override public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) { }
#Override
public void onRequestDisallowInterceptTouchEvent (boolean disallowIntercept){}
and this is my holder inside adapter
public MyViewHolder(View view) {
super(view);
tv_word = (TextView) view.findViewById(R.id.tv_word_show);
tv_english = (TextView) view.findViewById(R.id.tv_english_show);
tv_translation = (TextView) view.findViewById(R.id.tv_tranlation_show);
tv_numbershow = (TextView) view.findViewById(R.id.tv_numershow);
fav = (ImageView) view.findViewById(R.id.iv_fav);
more = (ImageView) view.findViewById(R.id.iv_more);
fav.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(mContext,"show some text for test",Toast.LENGTH_LONG).show();
}
});
I appreciate any help for my problem
Create Interface in you adapter class.
public void setClickListener(ClickListener clickListener) {
this.clickListener = clickListener;
}
public interface ClickListener {
void ItemClicked(View v, int position);
}
Declare the object on top like this:
ClickListener clickListener;
holder.view.setOnclickListener(new OnclickListener(
#Override
public void onClick(View v) {
if (clickListener != null) {
clickListener.ItemClicked(v, getAdapterPosition());
}
}
);
Implement this interface in class and intialize the clicklistener where you are setting adapter to recyclerview.
adapter.setClickListener(this);
When you will implement this in class you will get following method:
#Override
public void ItemClicked(View v, int position) {
}
Apply switch case from v.getId() you can handle as many click listeners you need to implement.

itemclick listener for recyclerview in android

I am using android recyclerview with cardview, which populates with the data from server. I want to set itemclick event for each item and need to display a dialog with a text. this text also getting from server. it will be different for diferent items.
when I am trying like given,
mItem=lists.getRemarks();
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new SweetAlertDialog(v.getContext())
.setTitleText("Remark")
.setContentText(mItem)
.show();
}
});
it only gives the last details
somehow i solved it by setting setContentText(mItem) to setContentText(lists.getRemarks())
Recyclerview does not have its item click listener so I have one custom class to detect it
RecyclerItemClickListner
public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener
{
public static interface OnItemClickListener
{
public void onItemClick(View view, int position);
public void onItemLongClick(View view, int position);
}
private OnItemClickListener mListener;
private GestureDetector mGestureDetector;
public RecyclerItemClickListener(Context context, final RecyclerView recyclerView, OnItemClickListener listener)
{
mListener = listener;
mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener()
{
#Override
public boolean onSingleTapUp(MotionEvent e)
{
return true;
}
#Override
public void onLongPress(MotionEvent e)
{
View childView = recyclerView.findChildViewUnder(e.getX(), e.getY());
if(childView != null && mListener != null)
{
mListener.onItemLongClick(childView, recyclerView.getChildPosition(childView));
}
}
});
}
#Override
public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e)
{
View childView = view.findChildViewUnder(e.getX(), e.getY());
if(childView != null && mListener != null && mGestureDetector.onTouchEvent(e))
{
mListener.onItemClick(childView, view.getChildPosition(childView));
}
return false;
}
#Override
public void onTouchEvent(RecyclerView view, MotionEvent motionEvent){}
#Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
}
Use it
mRecyclerView.addOnItemTouchListener(new RecyclerItemClickListener(getActivity(), mRecyclerView, new RecyclerItemClickListener.OnItemClickListener() {
#Override
public void onItemClick(View view, int position) {
// do whatever you want on position clicked
}
#Override
public void onItemLongClick(View view, int position) {
// do whatever you want on position long press
}
}));

RecyclerView with button onItemClick

I'm developping an app with a RecyclerView to show different "posts", each item has a like button which works perfectly fine but I'm now having a hard time because I want to show the details of a post in a seperate activity if the user clicks on it.
How can I start a new activity if the user clicks on anything except the like button ?
Try this,
recycleview.addOnItemTouchListener(
new RecyclerItemClickListener(Activity, new RecyclerItemClickListener.OnItemClickListener() {
#Override
public void onItemClick(View v, int position) {
startActivity(new Intent(Activity,otherclass.class));
}
})
);
ADD this Class,
public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener {
private OnItemClickListener mListener;
public interface OnItemClickListener {
public void onItemClick(View view, int position);
}
GestureDetector mGestureDetector;
public RecyclerItemClickListener(Context context, OnItemClickListener listener) {
mListener = listener;
mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
#Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}
});
}
#Override
public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) {
View childView = view.findChildViewUnder(e.getX(), e.getY());
if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) {
mListener.onItemClick(childView, view.getChildAdapterPosition(childView));
}
return false;
}
#Override
public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) {
}
#Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
}

get the text of an Item recyclerView

I'm working in the application of Android approached to finish it but I found the problem in recyclerView, and i didn't know how to get the text of an Item of the recyclerView ? (when i click in the item)
First you need to create a listener that gives as argument the view and the position. It dosent exist directly like for the listView. For that you can create a class like this :
public class RecyclerClickListener implements RecyclerView.OnItemTouchListener {
private OnItemClickListener mListener;
GestureDetector mGestureDetector;
public interface OnItemClickListener {
void onItemClick(View view, int position);
}
public RecyclerClickListener(Context context, OnItemClickListener listener) {
mListener = listener;
mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
#Override public boolean onSingleTapUp(MotionEvent e) {
return true;
}
});
}
#Override
public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) {
View childView = view.findChildViewUnder(e.getX(), e.getY());
if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) {
mListener.onItemClick(childView, view.getChildLayoutPosition(childView));
return true;
}
return false;
}
#Override
public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) { }
#Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
}
Then within your activity :
recyclerView.addOnItemTouchListener( // and the click is handled
new RecyclerClickListener(getActivity(), new RecyclerClickListener.OnItemClickListener() {
#Override public void onItemClick(View view, int position) {
Log.d("click item", String.valueOf(position));
TextView textView = (TextView) view.findViewById(R.id.YourTextViewID);
Log.d("test",textView.getText().toString());
}
}
}));

Categories

Resources