Recycler view adapter remove items - java

I have two activities: HomePage and MyFavorites. Both have recycler views with items (similar to facebook posts) each item has a button, when you click on it, the item will be shown in the recycler view for MyFav activity. The code for when the button is clicked is in the Adapter class for the recycler view (both activities use the same adapter). Now when a user click on the button for the second time (while the user is in MyFav activity) it means that this specific item should be removed from the recycler view in MyFav activity, but when the button is clicked for a second time while the user is using/opening the HomePage activity the item shouldn’t be removed, the button will turn from red to white ONLY . How can i remove the item only from the recycler view in MyFav activity without removing it from the HomePage activity (since both use the same adapter for their recycler view) without making a new adapter?
If i create separate adapter class for each activity, is it good practice? Since the same code will be duplicated twice with minor changes.
Note: the button (Fav button) is a checkbox with a heart shaped icon, the color is initially white, it will turn red when clicked for the first time, and white when clicked again and so on..

Related

Android. How to show 2 items in recycler view and then add one item on every button click

I want to show 2 items by default and add items in recycler view when we click button in main activity. Those item contains edittext so I have to get the data from edittext in my main activity.
Basically you have to create two different ViewType/Layouts to implement this.
Sharing related links below..
https://www.geeksforgeeks.org/how-to-create-recyclerview-with-multiple-viewtype-in-android/
How to create RecyclerView with multiple view types
For adding items to Recycler view when you click in a button you should add the item in the list and notify adapter that new item added at the end of the recyclerview using notifyItemInserted()
and for the other question for getting the edittext value from the adapter to the activity you can create a callback interface that implemented in the activity and pass the reference to the adapter and fire it from adapter when Edittext value is changed

How do I save the state of fragments in android?

I'm using a bottom navigation view which has 2 menu items. Each menu item upon clicking loads a fragment onto the MainActivity.
The first fragment is the home fragment where a RecyclerView shows a list of items and the second fragment contains a simple form.
now whenever I switch back and forth between these fragments, their previous states are lost and the fragment is recreated. This is a big issue for me because suppose I scroll to, let's say the 15th item of the list present in the home fragment. After scrolling, I navigate to the second fragment and then I navigate back. What happens is that the home fragment gets recreated and to see the 15th item I have to scroll again.
What should I do to handle this?

Android Cardview Menu Item Click to opens Different Activity

I am having a hard time figuring this one out. I am creating an Android app where the user (once logged in) sees an activity with a cardview list inside a recycler view.
I want the user to be able to click item on a card and go to a different activity. If you click menu item on card 1 you must go to the activity for game 1. If you click menu item on card 2 you must go to the activity for game 2. Etc...
In your adapter's ViewHolder class add following:
public ViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(new View.OnClickListener(){
// Start your activity
});
}
Create switch case to open different activities and then get recycler view position on the basis of that and provide different URL for each cards.

How to handle Swipe and Click event in the same cardview - Android

I am working on a android app, which has card swiping functionality like Tinder. In each cards, I'm having a imageview, which on click, opens in full screen. My problem is when I try to swipe, the image view considers it as a click event and opens the image in full screen.
I'm having my code for imageview onclick in the card swipe adapter and the code for swipe in the main activity that uses the card swipe adapter. How can I restrict the child view from consuming the parent's event?

changing listview item elements

I'm new to Android development.
I need to make a listview with text and icon audio player. The icon has three States, "download", "downloading", "play" and "pause". The idea is that when populating the listview, set the "download" icon if the track is not downloaded and "play" if the track is downloaded". If a person clicks the "download" button the icon should be updated to "downloading" to start the download, then when complete, you should see the icon "play". If the user presses the "play" icon for this element should be updated to "pause".
I can't change the icon . I tried to change them in onItemClickListener and make the onClick in the getView method in adapter, but when scrolling the icons changed to the old. And if you select multiple items in the list, then change all the icons, and I need to change only one, for playing of the track.
I didn't show the code because there is nothing that can be corrected. - all wrong
if you have your own adapter try using a method notifyDataSetChanged();
That is happening due to recycling of views. If you click on an item and you will see multiple items clicked.
Use RecyclerView instead of listView, which will handle recycling by its own and its ViewHolder class takes care of the issue you are having.
And don't forget to notify the recyclerView adapter whenever you make some change in data source (i.e. arraylist)
Check out this example...
Hope it helps
http://hmkcode.com/android-simple-recyclerview-widget-example/

Categories

Resources