How to get specific view checkbox from RecyclerView? - android

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

Related

Recycleview updates its layout

My question is asked before in many threads such as
How do I make RecyclerView update its layout?.
But unfortunately none of them fixed my issue.
I am using recycleview in my android project. for each item I have hidden checkbox and I am trying to make it visible for all items when long pressing. I am trying to call: adapter.notifyItemChanged() in such case but for nothing. sometimes It updates and sometimes does not and in case of updating it updadtes some items and left others.
Here is my code:
recyclerView.addOnItemTouchListener(new RecyclerItemClickListener(this, recyclerView, new RecyclerItemClickListener.OnItemClickListener() {
#Override
public void onItemLongClick(View view, int position) {
CheckBox checkBox = findViewById(R.id.checkBox);
checkBox.setVisibility(View.VISIBLE);
adapter.notifyItemChanged(position);
}
}));
and my listener class is:
public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener {
public interface OnItemClickListener {
void onItemClick(View view, int position);
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.getChildAdapterPosition(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.getChildAdapterPosition(childView));
}
return false;
}
#Override
public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) {
}
#Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
}
I have solved my issue by adding a boolean flag in the adapter. Then, In the method onBindViewHolder I check this flag and depending on it I show or hide Items' checkboxes as following:
if(flag){
holder.itemView.findViewById(R.id.checkBox).setVisibility(View.VISIBLE);
}
Finally in the long click method I set that flag and call notifyDataSetChanged method to update RecycleView's layout as following:
adapter.flag = true;
adapter.notifyDataSetChanged();

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

Handling click on image button of listview from activity

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) {
}
}

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

How to get row of particular position of recycler view in 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);

Categories

Resources