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