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
}
});
}
}));
Related
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) {
}
}
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.
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
}
}));
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());
}
}
}));
In my application I need to get a complete row of recycler view (becuase I want to get some view of that row) of a particular position.I searched we can get position by method getAdapterPosition().Now after getting position via this method I want to get complete row of that position.How can I achieve it?
Edit: I have some view on every row and I have set onclick listner on buttons in click of that I want get complete row or top layout (parent layout).What I understand is when on click is called it return parent view on which button is wrapped but not complete row.
I used one class for getting views(and Click and Long clicks too)
public class RecyclerTouchListener implements RecyclerView.OnItemTouchListener {
private GestureDetector gestureDetector;
private ClickListener clickListener;
public RecyclerTouchListener(Context context, final RecyclerView recyclerView, final ClickListener clickListener) {
this.clickListener = clickListener;
gestureDetector = 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 && clickListener != null) {
clickListener.onLongClick(child, recyclerView.getChildPosition(child));
}
}
});
}
#Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
View child = rv.findChildViewUnder(e.getX(), e.getY());
if (child != null && clickListener != null && gestureDetector.onTouchEvent(e)) {
clickListener.onClick(child, rv.getChildPosition(child));
}
return false;
}
#Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
}
#Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
public interface ClickListener {
void onClick(View view, int position);
void onLongClick(View view, int position);
}
}
and to use this
/**
* Recycler view item click handler
*/
rvDraft.addOnItemTouchListener(new RecyclerTouchListener(getActivity(),
rvDraft, new RecyclerTouchListener.ClickListener() {
#Override
public void onClick(View view, final int position) {
// click operation
}
#Override
public void onLongClick(View view, final int position) {
Dialogs dialog = new Dialogs();
dialog.showAlertForAction(getActivity(), getString(R.string.are_u_sure_want_to_delete), new AlertInterface() {
#Override
public void buttonYesClick() {
// long press operation
}
#Override
public void buttonNoClick() {
}
});
}
}));
Hope this will help you .
Use a global variable View selectedView. In your custom ViewHolder class, assuming the row item to be a LinearLayout set add the following lines
rowItemLayout = (LinearLayout) itemView.findViewById(R.id.row_item);
rowItemLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
selectedView = rowItemLayout;
..
}
});
Now you can fetch that view using
public View getSelectedView() {
return selectedView;
}
I have got solution to this problem now. First get the recycler view in adapter using below method in adapter class
#Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
mRecyclerView = recyclerView;
}
and on click event use below code
RecyclerView.ViewHolder holder = mRecyclerView.findViewHolderForLayoutPosition(getAdapterPosition());
and then cast the holder to the actual object as mentioned below
MessageViewHolder messageViewHolder = (MessageViewHolder) holder;
Congrats!! now you have a complete row of that particular position. Happy coding
You can try
getBindingAdapterPosition(view);
getAbsoluteAdapterPosition() ;
getchildAdapterPosition(view);