问题
Assume That an app Firstly List all post in a ListView in Fragment ( 1st ) with a custom adapter. and that custom adapter data is ArrayList<Hashmap<String,String>>
Then users can go to another fragments like category fragment ( 2nd ), that we have another listview with different data. We using back stack for fragments and user can back to prev fragment by clicking back button.
Issue:
in these fragments some post can be common! Like image attached, in first image, we can see a post in all post List, and when click on category and goes to category fragment, we can see that post again.
Then if user click on this post and goes to single post activity ( 3rd ), user can see like button and can like this post ( 4th ).
Until now every thing is OK! But trouble is happen when user click back button! first user goes to category fragment ( 5th ) and we should show like button on List View as liked button. And if user goes further and back to all post we should like button in that list view as liked too!! ( 6th )
what's the best strategy to handle this issue? :)
回答1:
You can create the model of the data bind to your list view and populate the list view with that data.
To have post execution of something like making like button you can use broadcast receiver(local) or can use tinyBus or otto to have call backs for any kind of event.
After getting the callback just notify the particular list in fragments.
Trick lies between model formation and callbacks.
回答2:
You can explicitly force the back button state to not be added to the back stack with transaction.addToBackStack(null). You can then override the back button (inside onBackPressed()) and insert whatever behavior you need. I'm not sure I'm following everything above, but I think that this combo will take care of the fundamental issue you address.
回答3:
The first (single post activity) problem can be easily solved by adding the like to the category list set of data (ArrayList.get(i).addLike or whatever you use to add likes) and then call
adapter.notifyDatasetChanged();
Which will make your second list update it self with the new content (that you've just changed)
So now the problem is the first list. as far as I understand we have 2 lists that contains an identical item, that (at least I wanna believe since it's post kind of element) has it's own unique ID, perhaps try to loop thru the first list searching for that element?
for example if the 2nd list (category?) is now showing a post in single post activity and its ID is 5. When changing the likes amount try something like this:
//First we change the second list item which is the first one on the backstack & we know it's position on the list
CategoryList.get(currentPost).addLike();
//Then we loop the first list to find the same post from the second list on it
for(int i = 0; i < MainList.size(); i++)
{
if(MainList.get(i).getID == CategoryList.get(currentPost).getID)
{
//Add the like on the first list
MainList.get(i).addLike();
break;
}
}
//Call update on the adapters
MainListAdapter.notifyDatasetChanged();
SecondListAdapter.notifyDatasetChanged();
来源:https://stackoverflow.com/questions/27374445/update-common-post-in-different-listview-with-different-data-in-adapter