how to apply onTouchEvent in recyclerview child inside the child? - android

I had made a navigation drawer which is having header footer and list. I wonder how to apply touch event in specific navigation drawer header image and footer. I'm having navigation drawer with Header which contain Image and name of the user then in between list of items and then footer.
All the xml file(Header, CustomRow and footer) are in recyclerView,
so i want basically functionality to click header image,customrom and
footer as well.

If you are using recyclerview you must have ViewHolder as well. Yes? Your problem is fixed.
You can define clickListeners within ViewHolder
Taking an example -
class AddedProductHolder extends RecyclerView.ViewHolder {
View mCardContainer;
AddedProductHolder(View itemView) {
super(itemView);
mContainer = itemView.findViewById(R.id.conatiner);
// Define clickListener here...
mCardContainer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Do your stuff.. If you need position of item clicked.. user getAdapterPosition()
}
});
}
}

Related

recycler view, not scrolling when using an on click listener onto the whole itemview

I want to use an on click listener onto the whole recycler view item. well, I want to use the on click listener when clicking on the item "anywhere" (the whole item). but when the whole item is using an on click listener, as you can see, the click listener is preventing the scrolling. how can i use an on click listener onto the whole item in the recycler, but scrolling should still be possible. i used the systems itemview for the click listener, also a button over the whole item layout, anyway the click listener prevents from scrolling. how to solve this? Thanks! Here the code:
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView tvBezeichnung;
public ImageButton buttonSelect;
public ImageButton buttonUpDown;
public ViewHolder(View itemView) {
super(itemView);
tvBezeichnung = itemView.findViewById(R.id.risftvbezeichnung);
itemview.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
...
});
//also tried buttons/other views over the whole item, on click listener prevents scrolling
}
}

Recyclerview item postion NOT_FOUND after scrolling

So i have a parent recyclerview with vertical orientation and a child recyclerview nested with parent with horizontal orientation.
Onclick function goes with child recylerview
I have created an interface which gives the adapter position of child item back to parent adapter class.
TEST CASE
Display the recyclerview and click on any child item.
result - working fine
Scroll down the recyclerview and click on any child item.
result - working fine
Manually Scroll back to 1st position and now click on any child item.
result - parent recylerview position is -1 (FAILED TEST CASE)
What can be the possibilities of this?
If you need any code i can provide it by editing
Parent ViewHolder class
class ViewHolder extends RecyclerView.ViewHolder{
RecyclerView reClassList;
ViewHolder(View itemView, final Context context) {
super(itemView);
reClassList = itemView.findViewById(R.id.recycle_garage_list_class);
garageListEvents = new GarageListEvents() {
#Override
public void getChildPositon(int childPosition) {
// TODO Remove
Log.e("GARAGE ADAPTER","Garage List Position :"+String.valueOf(getLayoutPosition()));
}
};
}
Child ViewHolder Class
public class ClassViewHolder extends RecyclerView.ViewHolder {
public ClassViewHolder(#NonNull View itemView) {
super(itemView);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
garageListEvents.getChildPositon(getAdapterPosition());
}
});
}
}
INTERFACE
public interface GarageListEvents {
public void getChildPositon(int childPosition);
}
FYI - GarageAdapter is the parent adapter
I guess it has to do something with recycling viewholders. Try to set this to parent viewholder holder.setIsRecyclable(false);. It will have little impact on performace, but it should solve problem.

like unlike button recyclerView image

I have an android app in which users can like and unlike an image.I'm using recyclerView.I Just disable the button(Like/Unlike) once user clicked. Problem, when I click on button like , the apps go to main activity and the button Like doesn't change to unlike What I have done :
1 ) layout that holds the each recycler view layout item
2 ) A view holder for creating each layout
3 ) A Model Class to holds the data
4 ) Recycler Adaptor which deals with the data for the Each Layout item
Hier ist my view holder
//Initializing Views
public ViewHolder(View itemView) {
super(itemView);
imageView = (NetworkImageView) itemView.findViewById(R.id.imageViewHero);
textViewName = (TextView) itemView.findViewById(R.id.textViewName);
//textViewPublisher = (TextView) itemView.findViewById(R.id.textViewPublisher);
likeImageView = (ImageView) itemView.findViewById(R.id.likeImageView);
likeImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int id = (int)likeImageView.getTag();
if( id == R.drawable.ic_like){
likeImageView.setTag(R.drawable.ic_liked);
likeImageView.setImageResource(R.drawable.ic_liked);
}else{
likeImageView.setTag(R.drawable.ic_like);
likeImageView.setImageResource(R.drawable.ic_like);
}
}
});
I answered this type of questions over and over. You didn't search enough and you didn't understand how ListView or RecycleView works. Changing current state of views ( such as changing text of TextView or changing resource of ImageView) is the wrong thing. You need data set (a list related to items in ListView) and you need to change corresponding data of the list and call notifyDataSetChanged() method of your adapter.
Don't forget. getView() method of your adapter is called every time any view of your list become on the screen and if you update only the view (instead of change data) your view will show the past value of item because your data didn't changed.
Look link below and search much more about how ListView and RecycleView works.
How ListView's recycling mechanism works

Create CardView with dynamic custom list inside

I create a RecyclerView with CardViews in my app. Now I want to add inside each CardView a new List of custom Views, with a custom layout (see example Image). When the add button is pressed, there should be insert a new row of this layout inside the card.
How do I realize that? In the internet I readed, that a ListView inside the CardView is not possible, because a ListView is scrollable, and there should not be two scrollable Views on the activity (thats what I found..)
the red marked row is the custom row.
for the RecylcerView I used the ViewHolder, CustomAdapter, etc.
Do I need this for the rows inside to? or is there a simpler way?
What I would do is attached a click handler to the Views is your ViewHolder. In this event handler you can delegate to a presenter of some sort that will add/remove/edit the View in the card holder.
private class MyHolder extends ViewHolder
implements View.OnClickListener {
...
public MyHolder(View itemView, MyPresenter presenter) {
super(itemView);
itemView.setOnClickListener(this);
...
}
#Override
public void onClick(View v) {
presenter.addRow(v);
}
}

Recyler view listitem image onclick

I am working on xamarin android apps. I used recycler view for displaying the images in each row. Now for each item image there is some link which I need to redirect. How can I achieve this onclick action for each image in recycler view.
Please help me in this regard.
Thanks
When creating a recycler view, you need to create a RecyclerView adapter which (among other things) implements methods for creating and binding a viewholder to the item in the recycler view. Somewhere in your code (oftentimes within this recycler view adapter class), you need to define the viewholder that you will use for your recyclerview items. This is where you should assign the onClickListener to your imageView.
Here is an example of a viewholder definition that I think may help you:
public class YourViewHolder extends RecyclerView.ViewHolder {
protected ImageView yourImage;
public YourViewHolder(View v) {
super(v);
final View theView = v;
yourImage = (ImageView) v.findViewById(R.id.yourImage);
yourImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// image clicked... do something
}
});
}
}
Let me know if you need more information on how to set up the recycler view adapter class (I have assumed that you already have done this, but I could be mistaken).

Categories

Resources