How to get row of particular position of recycler view in android? - android

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);

Related

best way to get RecycleView detail in another view

I'm implementing onClick on my main layout that contain all my views, in order to pass data to another view e.g:
holder.wholeView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(mContext, String.valueOf(position), Toast.LENGTH_SHORT).show();
//pass data with something like eventBus and transition to another view
}
});
I think there is a better way to do that?
Use this:
public class RecyclerTouchListener implements RecyclerView.OnItemTouchListener {
public interface ClickListener {
void onClick(View view, int position);
void onLongClick(View view, int position);
}
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) {
}
}
and from your activity where you're using your Recycler view use it like this:
mRecyclerView.addOnItemTouchListener(new RecyclerTouchListener(this, mRecyclerView,
new RecyclerTouchListener.ClickListener() {
#Override
public void onClick(View view, int position) {
// use this position to fetch the data from your list which you used to populate your recylcer view
}
#Override
public void onLongClick(View view, int position) {
}
}));
You should have the list of items from which the recycler view items are made. Now simply use this
myList.get(holder.getAdapterPosition());
This will return you the object of the clicked position.
Hope this helps.

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 make click listener in recyclerview in android?

I have a recycler view which contain some items in list and in individual items contain a button. i want to perform some action on button click on particular item i implemented Recycler view adapter but don't want to implement code in button click in adapter i want to perform action in fragmenr which contain recylcer view impelementation. How do i do that
code for adapter
public class ViewHolder extends RecyclerView.ViewHolder {
TextView m_premiumText, m_getDealText;
public ViewHolder(View itemView) {
super(itemView);
m_premiumText = (TextView) itemView.findViewById(R.id.tv_premiun_deals);
m_getDealText = (TextView) itemView.findViewById(R.id.tv_get_deals);
m_getDealText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(mContext,"clicked"+getAdapterPosition(),Toast.LENGTH_LONG).show();
}
});
}
}
code for recycler view
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
mContext = getActivity();
main = inflater.inflate(R.layout.reward_premium_deals, container, false);
findViewById();
initControls();
return main;
}
public void findViewById() {
m_premiumList = (RecyclerView) main.findViewById(R.id.premium_list);
m_layoutManager = new LinearLayoutManager(mContext);
m_premiumList.setLayoutManager(m_layoutManager);
}
public void initControls() {
premiumNames = getResources().getStringArray(R.array.premium_deals_title);
m_premiumDealsAdapter = new CPremiumDealsAdapter(mContext, premiumNames);
m_premiumList.setAdapter(m_premiumDealsAdapter);
/*here i want button click action*/
}
in these cases you must use an listener or callback and send it to your recycler view adapter through constructor.
firstly create a new interface to use as the callback. then implement that interface in your fragment and then send your fragment (this) as the listener to your adapter. then you can call that action from your recycler view instance.
From this Reference, I have implemented in recycler view item in my activity class not in fragment.
Interface Class:
public interface RecyclerViewClickListener {
void onClick(View view, int position);
void onLongClick(View view, int position);
}
RecyclerViewTouchListener Class:
public class RecyclerViewTouchListener implements RecyclerView.OnItemTouchListener {
private GestureDetector gestureDetector;
private RecyclerViewClickListener recyclerViewClickListener;
public RecyclerViewTouchListener(Context context, final RecyclerView recyclerView, final RecyclerViewClickListener recyclerViewClickListener1) {
recyclerViewClickListener = recyclerViewClickListener1;
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 && recyclerViewClickListener1 != null){
recyclerViewClickListener.onLongClick(child, recyclerView.getChildLayoutPosition(child));
}
}
});
}
#Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
View child = rv.findChildViewUnder(e.getX(), e.getY());
if (child != null && recyclerViewClickListener != null && gestureDetector.onTouchEvent(e)){
recyclerViewClickListener.onClick(child, rv.getChildLayoutPosition(child));
}
return false;
}
#Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
}
#Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
}
Now, inside fragment or activity class where you have to setup you recycler view activties:
recyclerView.setAdapter(your_adapter_class_object);
recyclerView.addOnItemTouchListener(new RecyclerViewTouchListener(this, recyclerView, new RecyclerViewClickListener() {
#Override
public void onClick(View view, int position) {
Loge("Item","Click");
}
#Override
public void onLongClick(View view, int position) {
Loge("Item","Long Click");
}
}));

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.

How to get specific view checkbox from RecyclerView?

I have a recyclerview filled with relativelayouts each have a textview and a checkbox. In my recyclerview filter fragment I'm Implementing android.widget.CompoundButton.OnCheckedChangeListener to try and differentiate between which textview of the recyclerview item is clicked, then perform an action and the same applies to the unchecking phase, how can this be accomplished?
Thanks
In onCheckedChanged, you can get your relativelayout from your checkbox by checkbox.getParent().
Then you can apply any change to your view.
If you want coresspone position in adapter, you can get by your_recyler_view.getChildAdapterPosition(get_relative_layout)
Hope this helps.
You can not direct use onclick. I have found a solution and it's works for me. Try it.
Step 1: Fist add this RecyclerItemClickListener class to your package.
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.getChildPosition(childView));
}
return false;
}
#Override
public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) {
}
}
Step 2: Now use onclick method. this gives you position of item clicked. Once you get position than get view at this position now you can use it as per your requirement.
recyclerview.addOnItemTouchListener(new RecyclerItemClickListener(
context, new RecyclerItemClickListener.OnItemClickListener() {
#Override
public void onItemClick(View view, int position) {
// TODO Auto-generated method stub
//find your view at clicked position here.
CheckBox c = (CheckBox) view.findViewById(R.id.btn_tag);
if (c.isChecked()) {
//add your code
} else {
//add your code
}
}
}));

Categories

Resources