how to get position of recylerview databinding - android-databinding

i am using data binding recycler view .i want to get that clicked position and show that position id(image) in view pager.i created data binding XML for image.

Position of clicked view does not matter, because views reused again and again while you scrolling content. You can directly set EventLisener to ViewHolder at onBindViewHolder(final ViewHolder holder, int position) method of RecyclerView.Adapter class

Related

Adapter with Picasso

I'm trying to use Picasso to get images from Url and put inside an Adapter. I have to send the image as a parameter to the funcion imageSlider inside the my SliderViewHolder. Can someone explain me how?
Picasso get:
Picasso.get()
.load(sliderImages[position])
.transform( RoundedTransformation(30, 0))
.placeholder(context.resources.getDrawable(R.drawable.ic_launcher_foreground))//it will show placeholder image when url is not valid.
.networkPolicy(NetworkPolicy.OFFLINE) //for caching the image url in case phone is offline
.into(holder.imageSlider(?))
Viewholder:
class SliderViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val imageView: RoundedImageView = itemView.findViewById(R.id.image_slide)
fun imageSlider(sliderItem: SliderItem) {
imageView.setImageResource(sliderItem.image)
}
}
The Function
fun imageSlider(sliderItem: SliderItem) {
imageView.setImageResource(sliderItem.image)
}
is not necessary, because you declare the imageView with a public visibility.
In the Adapter of your RecyclerView, there is a Method called
override fun onBindViewHolder(holder: SliderViewHolder, position: Int)
This one will get invoked, when the Adapter wants to bind your Item Layout (ViewHolder) with a "backing data item" of the List...
Then you could simply do this:
Picasso.get()
.load(sliderImages[position])
.transform( RoundedTransformation(30, 0))
.placeholder(context.resources.getDrawable(R.drawable.ic_launcher_foreground))//it will show placeholder image when url is not valid.
.networkPolicy(NetworkPolicy.OFFLINE) //for caching the image url in case phone is offline
.into(holder.imageView)
Which will make Picasso, load the Image into the ImageView of your ViewHolder (which actually simply holds the Item Layout) for the current Item in the List.
The correct implementation would actually be, that you give your Adapter a List of Image Objects (for example, containing Properties like Image Name and a Path/URL, that should get "rendered") and when the Adapter is invoking "onBindViewHolder" with the corresponding Position, it is your Job to implement the loading of the image from the given path for the given position.

Kotlin: How can inflate Room data into a RecyclerView using Fragments?

I trying use Room to inflate the Database in an RecyclerView, but all this using Fragments. I have the screen where the user enters the data and the screen where the RecyclerView is inflated, but I don't know how to make the Room data inflate in the RecyclerView and I still can't understand the viewModel.
For Getting data from Room this might get helpful
and for view modal this helped me.
after getting data from Room you can simply notify the adapter.
example :
var myList = ArrayList<YourModel>()
roomDataList.forEach { item ->
//You can parse or convert your data according to your use
myList.add(item)
}
adapter.updateList(myList)

android recyclerView's view item's onConfigurationChanged is not called

Has
android:configChanges="orientation|screenSize"
and there are some custom view (root from RelativeLayout), which has
override fun onConfigurationChanged(newConfig: Configuration?) {
super.onConfigurationChanged(newConfig)
// do something to react
}
the same custom view is used in either scrollView or recyclerView.
in same activity/fragment when place the custom view in the scrollerView its onConfigurationChanged is called when rotate, but place it in the recyclerView the onConfigurationChanged is not called.
The code has no difference except one use scrollView, one use recyclerView.
Is it a known behavior? Or how to make the view item's onConfigurationChanged to be called when place it in the recyclerView?
Turns out there is different how the custom view is placed in the parent container.
When add to recyclerview view holder, it is wrapped in a FrameLayout first, so one more layer view which seems prevents the custom view's onConfigurationChange getting called (since the custome view is not the direct view in the viewholder).

Android scroll to position in Recylerview not working

I want to scroll the RecyclerView to a position, and I use LinearLayoutManager.scrollToPositionWithOffset() to scroll.
But it does not work. Could any one help me?
RecyclerView rv = findViewById(R.id.recycler_view);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
rv.setLayoutLayoutManager(layoutManager);
layoutManager.smoothScrollToPosition(position);
Scrolling RecyclerView to certain item position can be achieved with LayoutManager which is used with that particular RecyclerView. Plz refer to above piece of code
If you don't have a lot of items in your list, you can use LinearSmoothScroller
val smoothScroller = object : LinearSmoothScroller(activity) {
override fun getVerticalSnapPreference(): Int {
return SNAP_TO_START
}
}
and then when you want to scroll:
smoothScroller.targetPosition = position // position on which item you want to scroll recycler view
binding.cardsRecycler.layoutManager?.startSmoothScroll(smoothScroller)

Arraylist and ListView

I have a ListView, I created a custom adapter because in every row there must be two ImageView.
My aim is to set on clicklistener one of them, and when I click it a new row must be created in a certain position in the list.
I know that to achieve this I have to set onclickListener the imageView inside the adapter class, but how can I get access to the ArrayList of the ListView so that I can create new rows?
This is the onClick() method in the onClickListener that you need:
public void onClick(View v){
mArrayList.add(<some new data>, <the index of the row> + 1);
mBaseAdapter.notifyDatasetCahged();
}