enter image description hereI have almost 250 patent item which i want to show in a parent recyclerview. After item click of parent recyclerview it will show over hundreds of data under each parent item in another recycler view. How can i do it like this picture.
I would do it this way. Create an interface:
interface AdapterContract{
fun onListItemClick(id: String) //whatever the parameter is
}
When you instantiate the Adapter in your Fragment1, you can implement this interface:
class Fragment1 : Fragment(), AdapterContract{
...
//when instantiating the adapter:
private val adapter: MyAdapter by lazy{
MyAdapter(this //for the interface)
}
//override the method of Adapter contract
}
Your adapters constructor should look like this: MyAdapter(adapterContract: AdapterContract) : ListAdapter<MyAdapter.MyViewHolder>(Diff_Util_SOME_OBJECT)
Than in the adapters ViewHolder:
itemHolder?.setOnClickListener{
adapterContract.onListItemClick(someId)
}
Now in your override method in Fragment1:
override method onListItemClick(id: String){
//pass this id to the next opening fragment (like Bundles or navargs)
//init `Fragment2` which is going to have all items, that belong to `Fragment1` selected item
}
Load the data :)
I hope you are using a database or something. Should work with hard coded data structures as well, but just to be safe :).
Related
I have a ListView which works with Custom-Item-Templates.
Inside the List-Item Template are ImageViews (used as Buttons) and several Strings. The Data is bound with CustomAdapter.
I want to to do specific things inside the ListView with each Item on clicking different ImageViews.
I come from .NET and WebDevelopment and very new in Android.
How can I achieve to check which Button or Image in which ListItem was clicked.
Normally i would give each element in ListItem custom attribute whith unique_id including a key from the DataItem and get that Key in a Click-Event of Buttom and so on.
Is there any opinion to give a ImageView a Custom Attribute like 'MyCustomID' and get that in the Click-Event from it? Or how is the usual workflow for a situation like this: ListItems with repeating Elements and checking what ListItem belongs to the clicked Element inside.
Please help!
Try with this
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// position is the index of clicked item.
}
});
First of all, I'd recommend to change to RecyclerView. It is more performant, flexible and easier to use. Here you can check how to do it.
About your question: I don't remember exactly how to implement ListView, but I believe that you have an adapter. In that adapter, you are creating the view of your items. So, what you can do is create a listener interface that receives the info of your items. Something like:
interface OnImageItemClickListener {
fun onImageClicked(imageInfo: ImageInfo)
}
data class ImageInfo(
val id: Long,
val uri: URI,
)
Then you can pass an implementation of that interface to your adapter, and then use it in the View.setOnClickListener method:
class CustomAdapter(
private val listener: OnImageItemClickListener,
private val info: List<ImageInfo>,
) : ... {
override getView(position: Int, convertView: View, parent: ViewGroup): View {
// Inflate your view...
view.setOnClickListener {
listener.onImageClicked(info[position])
}
}
}
Again, I highly recommend to migrate to RecyclerView. If you implement it, you'll have to follow the same steps.
I've a recyclerview and I want to delete a recyclerview item.
fun deleteItem(position: Int){
ModelList.removeAt(position)
notifyItemRemoved(position)
notifyDataSetChanged()
}
override fun onBindViewHolder(holder: myRecyclerViewAdapter.ViewHolder, position: Int) {
holder.bind(ModelList[position])
holder.itemView.listRowDelete.setOnClickListener {
deleteItem(position)
}
I deleted items with this method but because of these items are in sqlite database they all come back when I restard the app. I can't reach positions from my mainactivity and I cant reach database inside of adapter. How can I delete specific item from sqlite database which I want to. Please help thank you
For onClickListener , Don't rely on position from the onBindViewHolder
Instead get the position from holder like this
holder.itemView.listRowDelete.setOnClickListener {
deleteItem(holder.adapterPosition)
}
InClickListeners and things that happen afterwards , get the accurate position with adapterPosition , hover over the adapter position to see the quick documentation !
If you want to call database method , I would suggest you to not call it in Adapter & rather have methods to set custom listeners to adapters which can be called with the victim (item supposed to be deleted)
have a function like this in your adapter
Define the listener property like this
private val myListener:MyCustomListenerInterface = MyCustomListenerInterface{}
Have a setter function to set listener
fun setListener(l:MyCustomListenerInterface){
myListener = l
}
interface MyCustomListenerInterface{
fun onDelete(model)
}
Then in your onClickListener call the onDelete of the custom listener interface like this
holder.itemView.listRowDelete.setOnClickListener {
myListener.onDelete(modelItem)
}
Now When Initializing the adapter for recycler view in fragment/activity , set the listener !!
I'm using paging 3 to paginate my retrieved data.
I want to show a badge on the bottom navigation bar when a new item has been added to the recycler view.
imagine the user is watching recycler view in the situation below:
item A
item B
item C
now the user refresh the page using a swipe refreshing button which calls this function:
binding.refresh.setOnRefreshListener {
adapter.refresh()
}
and a new item is added, so the situation would be like this:
item D
item A
item B
item C
the new data would be propagated using this line of code:
viewModel.tickets.observe(viewLifecycleOwner, {
viewLifecycleOwner.lifecycleScope.launch {
adapter.submitData(it)
}
})
I want to show a badge at this moment. so I added an interface into my recycler paged list adapter like this:
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
ticketUtils.hasNewItem(getItem(0)?.ticket_id)
holder.bind(getItem(position))
}
which checks what is the first item of the recycler view.
so I have implemented the body of this interface inside of my fragment like this:
override fun hasNewItem(itemID: String?) {
if (itemID.isNullOrEmpty())
return
if (itemID != firstTicketID)
MainActivity.mainUtils.newTicket(showBadge = true)
}
and MainActivity.mainUtils.newTicket is an interface that shows a badge on the desired icon.
but I have faced to the problem that it seems the recycler view won't be notified for new data till the user start scrolling. I mean the code written in onBindedViewHolder would be called just when the user scrolls the page.
how can I solve that?
viewModel.tickets.observe(viewLifecycleOwner, {
//Here you get to know that there's update in your Datalist
adapter.submitData(it)
})
When you insert a new data into the database, observe the data inserted by returning the id of the data. For example if you are using Room, it can return a long value, which is the new rowId for the inserted item.
Compare with existing ones and then you can show a badge if there's new data in the list.
Can someone explain the logic on how to handle this matter:
I have a fragment that after a WebSocket call inflates 2 Recyclerviews.
Child Recyclerview is nested to Parent Recyclerview and the parent adapter calls the child adapter.
I want to put an Interface for a click listener which handles the click in the Child Items in the Fragment.
Where should I put the interface and which class should implement it?
What you're trying to do has been done multiple times.
There are various approaches you can try, but in general, responsibilities would look something like this:
YourContext (Fragment/Activity)
Inflates a layout with a RecyclerView.
Instantiates YourAdapter
Subscribes, Requests, Waits, for your data and passes it onto YourAdapter.
Maintains an interface for click handling, like:
interface YourThingClickHandler {
fun onThingWasClicked(thing: Thing) // and any other thing you may need.
}
Can be YourContext: YourThingClickHandler or if you want, you can keep an anonymous/local instance of that. I usually do the former and then implement the fun onThingWasClicked(...) in the fragment/activity, it depends what you need to do when the item was clicked.
YourAdapter
Expects a list of Things and one YourThingClickHandler instance. So in your Fragment/Activity you'd do, something like (pseudo code):
// This is called once your ViewModel/Presenter/Repository/etc. makes the data available.
fun onThingsLoaded(things: List<Thing>) {
adapter.setClickHandler(this) // this can be passed when you construct your adapter too via constructor like adapter = YourAdapter(this)
adapter.submitList(things) // if the adapter extends a `ListAdapter` this is all you need.
}
Now that you've passed an outer click handler, you need to deal with the inner list. Now you have a few choices:
1. pass the same click handler all the way in and let the innerAdapter directly talk to this.
2. Have the outerAdapter act as an intermediate between the clicks happening in the innerAdapter and bubble them up via this click handler you just supplied.
Which one you chose, will depend largely on what you want to do with it, and how you want to handle it. There's no right or wrong in my opinion.
Regardless of what you do, you still need to get from the view holder to this click handler...
So in YourAdapter you should have another Interface:
interface InternalClickDelegate {
fun onItemTappedAt(position: Int)
}
This internal handler, will be used to talk from the viewHolder, back to your Adapter, and to bubble the tap up to the external click handler.
Now you can have a single instance of this, defined like so in your adapter class (remember this is Pseudo-Code):
private val internalClickHandler: InternalClickDelegate? = object : InternalClickDelegate {
override fun onItemTappedAt(position: Int) {
externalClickHandler?.run {
onThingWasClicked(getItem(position))
}
}
}
So if the external click handler (the YourThingClickHandler you supplied) is not null, then fetch the item from the adapter's data source, and pass it along.
How do you wire this internal handler with each view holder?
When you do onCreateViewHolder, have a ViewHolder that takes... you guessed, a InternalClickDelegate instance and so...
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
val inflater = LayoutInflater.from(parent.context)
// decide which viewHolder you're inflating... and...
return YourViewHolder(whateverViewYouInflate, internalClickHandler)
Now your ViewHolder(s) have a reference to this internal click handler...
so when you do onBindViewHolder(...) you probably call a common ViewHolder method of your choice, for example if your View holder can be of different types, you probably have an Abstract viewHolder with a fun bind(thing: Thing) method or similar that each concrete viewHolder subType will have to implement... in there, you'd do something like this:
override fun bind(thing: Thing) {
if (clickHandler != null) {
someViewYourViewHolderInflated.setOnClickListener(this) // this assumes your ViewHolder implements View.OnClickListener from the framework
}
}
Because your ViewHolder implements View.OnClickListener, you must implement the onClick method in it:
override fun onClick(v: View?) {
clickHandler?.onItemTappedAt(adapterPosition)
}
And this is how your ViewHolder, will receive the tap/click event from Android in the onClick method, if you supplied a click Handler (you did in the adapter onCreateViewHolder when you passed the internalClickHandler), it will simply bubble the tap, passing the position. adapterPosition is the Kotlin equivalent of calling getAdapterPosition() in a RecyclerView adapter.
TOO LONG, DIDN'T READ GRAPH
Fragment: ExternalClickListener -> passes an instance of it to the Adapter.
Adapter: Receives the ExternalClickListener, passes an InternalClickListener to each ViewHolder.
ViewHolder: Receives the internal Click Listener, sets itself as Clickable (either the entire itemView or just any widgets you want to make clickable, if you want the whole cell to be clickable, simply use itemView which is the "whole" view of the ViewHolder.
When the viewHolder's view is tapped, android calls the click listener's onClick method. In there, and because you are in a ViewHolder, you can do getAdapterPosition() and pass this to the internal click handler you received.
The Adapter then can transform that position back into data, and because you supplied an External clickListener, it can pass the actual item back to the external click listener.
Wait, but how about a NESTED RecyclerView.
There's nothing special about that, you simply need to provide the same mechanism, and keep passing things around. What you do or how many of these interfaces you have, depends entirely on what you're trying to achieve; like I said at the beginning, each solution is different and other factors must be taken into account when making architectural decisions.
In general, keep this thing in mind: Separation of Concerns: keep things small and to the point. For E.g.: it may seem crazy to have this double interface, but it's very clear what each does. The internal one, is simply concerned about a "tap" in a "view", and to provide the position in a list where said tap occurred.
This is "all" the adapter needs to fetch the data and make an informed guess at what item was truly tapped.
The fragment doesn't know (or care) about "positions", that's an Adapter's implementation detail; the fact that positions exist, is oblivious to the Fragment; but the Fragment is happy, because it receives the Thing in the callback, which is what most likely needs to know (if you needed to know the position for whatever reason, tailor and modify the externalCallback to have the signature of your choice.
Now replicate the "passing hands" from your OuterAdapter to your InnerAdapter, and you have done what you wanted to do.
Good luck!
1) You should put interface in child adapter and implement that in parent and then pass another one interface (long peocess)
2) Use local broadcast manager
you will add ClickListener in parent adapter and also add it in constructor of adapter
public interface HandleClickListener
{
void onItemClicked(int position, SurveysListModel surveysListModel);
}
Make an instance of your clicklistener and then on holderclick listner get the position of item and its value from your model list
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
handleClickListener.onItemClicked(position, yourModelList.get(position));
});
and get in to your activity like this making an instance of you adapter
adapter = new InstrumentsSearchAdapter(yourModelsList, activity.this, new SearchAdapter.HandleClickListener() {
#Override
public void onItemClicked(int position, Model listModel) {
instumentChild = listModel.getInstrument_title();
Intent intent = new Intent(Activity.this, Listing.class);
intent.putExtra("selectedQuestions", listModel.getQuestions());
startActivityForResult(intent, 5);
}
});
And if you want to go to parent recyclerview class implement onActivityResutlt method and get data back from child through intent and get that intent in onActivityResutlt method
I am having trouble debugging a recycler view issue. Here I am trying to delete an item from recycler view using the following method:
when I click on the card (one that is to be deleted), it will store the title of a card in shared preference. (so that in future when I open the app I will know what cards are not to be displayed)
Then I am calling a method which checks if the title in shared preference matches the title of the card and make ArrayList consisting of cards which are not present in shared preference.
Now I am using this ArrayList to fill cards using notifyDataSetChanged
My code is as Follows.
Adapter.
class LocalAdapter(var localCardsInfo : ArrayList<LocalModel>?,var fragment: LocalListingFragment) : RecyclerView.Adapter<LocalHolder>() {
fun refreshDataOnOrientationChange(mLocalCards : ArrayList<LocalModel>?){
if (localCardsInfo!!.size>0)
localCardsInfo!!.clear()
localCardsInfo = mLocalCards
notifyDataSetChanged()
}
override fun getItemCount(): Int {
return localCardsInfo!!.size
}
override fun onBindViewHolder(holder: LocalHolder, position: Int) {
if (localCardsInfo!=null)
holder.updateUI(holder.adapterPosition,localCardsInfo!![holder.adapterPosition])
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): LocalHolder {
val card_a : View = LayoutInflater.from(parent.context).inflate(R.layout.card_local_a,parent,false)
return LocalHolder(card_a,fragment)
}
}
ViewHolder
class LocalHolder(itemView : View,val fragment: LocalListingFragment) : RecyclerView.ViewHolder(itemView),OpenedLayoutManagerLocal{
fun updateUI(position : Int , localModel: LocalModel){
moveButton.setOnClickListener({
archived()
})
private fun archived(){
ALL_STATIC_CONSTANTS_AND_METHODS.addToArchive(fragment.activity!!,model!!.roomName,model!!.deviceName)
itemView.archiveLayout.visibility = View.GONE
itemView.elevateLayoutLocal.visibility = View.GONE
fragment.mAdapter!!.refreshDataOnOrientationChange(ArrayList(LocalDataService.ourInstance.getNonArchivedItems(fragment.activity!!)))
}
For example, if I have six item in recycler view and when I delete one I can see 5 items on the list while debugging, even onbind is called 5 times but only 4 items are displayed.
I tried my best to debug it yet failed to find a solution and I can't think about what to do next. I tried notifyItemRemoved and notifyItemRangeInserted also but still, I am facing the same issue. It would be a great help if someone can suggest a possible cause of such issue.
Thank you.
Edits:
I just experimented by adding dummy button in fragment where recycler view is placed. An to my surprise if I delete element here just element at that position gets deleted and there is no case of disappearing items. So it seems like problem happens when I delete an item from view holder. So I guess I got the cause of the error but can't think of a way to implement it because the original button is in the card so I am forced to use delete procedure inside view holder. Please do suggest if you have any suggestions. Code that I added in fragment is as follow:
v!!.findViewById<Button>(R.id.delete_button).setOnClickListener({
try {
val model = LocalDataService.ourInstance.getNonArchivedItems(activity!!)[0]
ALL_STATIC_CONSTANTS_AND_METHODS.addToArchive(activity!!,model.roomName,model.deviceName)
mAdapter!!.refreshDataOnOrientationChange(ArrayList(LocalDataService.ourInstance.getNonArchivedItems(activity!!)))
}catch (e : Throwable){}
})
Here on this new dummy button click, it deletes the first item in the list gets deleted while on clicking the button on RecyclerView card it deletes one item and then one more item gets disappeared.