I'm studying MVP style with Android Architecture Blueprints
In this sample, Adapter(RecyclerViewAdapter etc) belongs to View.
Fragment means View in MVP and Adapter placed in the View as inner class.
In this case, a communication between View and Adapter was made by using callback interface.
public TasksAdapter(List<Task> tasks, TaskItemListener itemListener) {
setList(tasks);
mItemListener = itemListener;
}
I often though callback communication is a little confused.
Many reactions in this QA show this matter.
Why doesn't RecyclerView have onItemClickListener()? And how RecyclerView is different from Listview?
public class ReactiveAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
String[] mDataset = { "Data", "In", "Adapter" };
private final PublishSubject<String> onClickSubject = PublishSubject.create();
#Override
public void onBindViewHolder(final ViewHolder holder, int position) {
final String element = mDataset[position];
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onClickSubject.onNext(element);
}
});
}
public Observable<String> getPositionClicks(){
return onClickSubject.asObservable();
}
}
There are some ways on this.
Callbacks (callback, listener, and observer)
Observable with RxJava
PublishSubject with RxJava
Event Bus
Pass View or Presenter to Adapter's constructor
Which way is correct ? How should I do this?
Related
I have a fragment Users which has 3 other fragments in it (tabs). For one tab ( called Friends2Fragment ) I made a recycler View and made an adapter for it. In each item of RecyclerView I have a button "Add friend" and I want to call it from Friends2Fragment, not to call it from the adapter because I can't use Firestore Database properly.
RecyclerViewInterface:
public interface RecyclerViewInterface {
void onItemClick(int position, String button_pressed);
}
Friends2Fragment.java :
public void onStart(){
super.onStart();
recyclerView = (RecyclerView) v.findViewById(R.id.recycler);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
friendslist = new ArrayList<>();
myAdapter = new MyAdapter(friendslist,v.getContext());
recyclerView.setAdapter(myAdapter);
------ Firestore operations ------
}
#Override
public void onItemClick(int position, String button_pressed) {
switch ( button_pressed ){
case "ADD_FRIEND":
Log.d(TAG, "item clicked: " + friendslist.get(position).username);
}
}
MyAdapter.java :
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.myViewHolder> {
Context context;
public ArrayList<User> userArrayList;
public MyAdapter(ArrayList<User> userArrayList, Context context) {
this.userArrayList = userArrayList;
this.context = context;
}
public Context getContext() {
return context;
}
public ArrayList<User> getUserArrayList() {
return userArrayList;
}
#NonNull
#Override
public MyAdapter.myViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);
MyAdapter.myViewHolder myViewHolder = new MyAdapter.myViewHolder(v);
myViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((Friends2Fragment)context).onItemClick(myViewHolder.getAdapterPosition(),"ADD_FRIEND");
}
});
return myViewHolder;
}
#Override
public void onBindViewHolder(#NonNull MyAdapter.myViewHolder holder, int position) {
User user = userArrayList.get(position);
holder.usernamerecycle.setText(user.username);
}
#Override
public int getItemCount() {
return userArrayList.size();
}
public void filterList(List<User> filteredList){
userArrayList = (ArrayList<User>) filteredList;
notifyDataSetChanged();
}
public class myViewHolder extends RecyclerView.ViewHolder{
TextView usernamerecycle;
Button addbutton;
View rootview;
public myViewHolder(#NonNull View itemView) {
super(itemView);
rootview = itemView;
usernamerecycle = itemView.findViewById(R.id.usernamerecycler);
addbutton = itemView.findViewById(R.id.addfriendbutton);
}
}
}
The problem is at this line : ((Friends2Fragment)context).onItemClick(myViewHolder.getAdapterPosition(),"ADD_FRIEND"); in onCreateViewHolder method in MyAdapter.
I have this error : Inconvertible types; cannot cast 'android.content.Context' to 'com.example.birthday.Fragments.Friends2Fragment'
Please help me ..
A Fragment isn't a Context (that's not one of its supertypes) so that cast is impossible, that's why you're getting the error.
I think you should organise it like this: your Adapter holds a bunch of User objects, right? It displays those, and you have a click listener on each ViewHolder that knows which index in the User list it's currently displaying, and it wants to inform some listener when it's clicked. That index is an internal detail really, it would make more sense to look up the actual User, and provide that to the listener.
The simplest way is to just provide your fragment as a listener. First store it in your adapter:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.myViewHolder> {
// store a reference to your fragment
private Friends2Fragment listener;
// add a function to provide that fragment
public void setListener(Friends2Fragment: listener) {
this.listener = listener
}
...
public MyAdapter.myViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
...
myViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (listener != null) {
// look up the actual user
User user = userArrayList.get(myViewHolder.getAdapterPosition());
// call a function on your fragment
listener.onItemClick(user, "ADD_FRIEND");
}
}
});
}
Then add the callback function your adapter uses, and also set your fragment on the adapter as a listener:
// Friends2Fragment
// You should REALLY be doing this in onViewCreated or something, so this setup happens once.
// You're losing all your state by creating a new adapter whenever the user returns to the app
public void onStart(){
...
myAdapter = new MyAdapter(friendslist,v.getContext());
// set the fragment as the listener
myAdapter.setListener(this);
recyclerView.setAdapter(myAdapter);
}
// now add the function the adapter calls
private void onItemClick(User user, String someString) {
// handle the clicked user
}
A better way is to create an interface with all the events that need to be handled, and make your Fragment implement those. It breaks the hard association with the Fragment since you could pass any object that implements those functions, and it's also clearer because the interface kinda documents all the data the adapter produces, and that a listener needs to be able to handle. Something like this:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.myViewHolder> {
// the listener is now something that implements the Callbacks interface
private Callbacks listener;
...
// nesting it inside MyAdapter makes the path MyAdapter.Callbacks, which makes it clear
// exactly what it is and what it relates to, and kinda gives the Adapter "ownership"
interface Callbacks {
void addFriend(User user)
}
And then you just make the Fragment implement that interface
public class Friends2Fragment() extends Fragment implements MyAdapter.Callbacks {
...
// implement all the callbacks you need to handle
override public void addFriend(User user) {
// do the thing
}
// set it in the same way, since this Fragment implements MyAdapter.Callbacks
myAdapter.setListener(this);
Which is a bit neater and cleaner, I think - but slightly more work. Also if you notice, I renamed the callback function from the generic handleItemClick to the more specific addFriend - so instead of having to pass a String saying what kind of click it is, you just have a function for each event you want to handle, and you can name them appropriately
I'm working on an existing app, migrating it towards the MVP architecture. In one of the RecyclerView.Adapters, there's quite a lot of logic in regards to how a header View should look like. According to the MVP architecture, such logic should be moved to the Presenter and the presenter should use the View to display things. Also, I've read quite a lot about how if you have Android classes in your presenter - that basically indicates that something went wrong.
So my thoughts so far are to create a Presenter for the ViewHolder when the ViewHolder (also the View from the MVP standpoint) is created and to move my logic there. Howeverm the problem I'm having is that the logic is based on Android classes such as Paint, Rect, Spannable and so on.
So how do I solve this problem in the cleanest way possible? Do I move all the logic to the View? Or should I keep the logic in my Presenter, but move only the calculations related to the specific Android objects to the View? As an example that would look something like that:
in presenter:
double textLines = mView.getTextLines(text, 0 , text.length());
in view:
Paint paint = mTextView.getPaint();
Rect bounds = new Rect();
paint.getTextBounds(text, start, end, bounds);
double parentWidth = parentView.getWidth() - parentView.getPaddingLeft() - parentView
.getPaddingRight();
return Math.ceil((double) bounds.width() / parentWidth);
Or is there another better way?
Every piece of code that depends on Android classes should be kept on the view implementation (Activiy, Fragment, ViewHolder etc) - the view should be as dumb as possible. I think you don't need a presenter for the ViewHolder, as it usually should not have logic.
If you have unit tests (and you should), the process becomes easier to grasp because you will be forced to move Android dependent classes to somewhere else in order to be able to test it.
For example, a real case scenario I have with RecyclerView is something like this: one view interface for the Activity, one view interface for the ViewHolder, one presenter for the Adapter and one presenter for the Activity. The following is a partial, non compilable example just for demonstration, where you can see the separation of concerns between the layers.
So, in your case, the approach is correct: the view interface just have a method that returns a string "from somewhere", while in the real android view you use the actual classes and methods from the Android API to retrieve the requested data.
My example is below.
// Definitions for the event adapter stuff
public interface EventAdapterContract {
interface View {
void onFetchEventsFailed(String reason);
void notifyDataSetChanged();
}
interface Presenter {
void getItemViewType(int position);
void getItemCount();
void bindEventRow(int position, ViewHolder holder, int backgroundColor);
}
interface ViewHolder {
void setTitle(String value);
void setHour(String value);
void setBackgroundColor(int color);
}
}
// Adapter presenter implementation. Again, partial code for simplicity
public class EventAdapterPresenter implements EventAdapterContract.Presenter {
private final EventAdapterContract.View view;
private final List<Event> events;
static int VIEW_FINISHED = 0;
static int VIEW_pending = 0;
public EventAdapterPresenter(EventAdapterContract.View view, EventAPI api) {
this.view = view;
this.events = new ArrayList<>();
// EventAPI is an interface (Retrofit in this case), which can
// also be tested with plain junit
}
#Override
public int getItemCount() {
return events.size();
}
#Override
public int getItemViewType(int position) {
Event item = events.get(position);
return item.isFinished() ? VIEW_FINISHED : VIEW_PENDING;
}
#Override
public void bindEventRow(int position, EventAdapterContract.ViewHolder holder, int backgroundColor) {
Event event = events.get(position);
holder.setTitle(event.getTitle());
holder.setHour(String.format("Event time: %s", event.getStartTime()));
holder.setBackgroundColor(backgroundColor);
}
}
// The adapter itself. Please mind that this is a partial
// piece of code just for the sake of demonstration,
// I ommited several parts to keep it simple
public class EventAdapter
extends RecyclerView.Adapter<RecyclerView.ViewHolder>
implements EventAdapterContract.View {
private EventAdapterContract.Presenter presenter;
public class EventAdapter(Contex context) {
// Regular adapter contructor
// ...
presenter = new EventAdapterPresenter(this, someImplOfEventApi);
}
#Override
public void onFetchEventsFailed(String reason) {
// Show an AlertDialog
}
#Override
public int getItemViewType(int position) {
return presenter.getItemViewType(position);
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
int backgroundColor = ContextCompat.getColor(context, R.color.colorEventPending);
presenter.bindEventRow(position, holder, backgroundColor);
}
}
public class EventRowViewHolder
extends RecyclerView.ViewHolder
implements View.OnClickListener, EventAdapterContract.ViewHolder.Row {
private TextView title;
private TextView hour;
public EventRowViewHolder(View view) {
super(view);
// init widgets etc...
view.setOnClickListener(this);
}
#Override
public void setTitle(String value) {
title.setText(value);
}
#Override
public void setHour(String value) {
hour.setText(value);
}
#Override
public void setBackgroundColor(int color) {
this.itemView.setBackgroundColor(color);
}
#Override
public void onClick(View view) {
EventBus.getDefault().post(new OpenEventDetailsMessage(orderId));
}
}
I'm requesting images from presenter in adapter:
#Override
public void onBindViewHolder(SiteAdapter.ViewHolder holder, int position)
{
Site site = sites.get(position);
holder.siteName.setText(site.getName());
requestHolderLogo(holder, site.getLinks().getLogoUrl());
}
private void requestHolderLogo(final ViewHolder holder, final String logoUrl)
{
compositeSubscription.add(
presenter.bitmap(logoUrl)
.subscribe(
bitmap -> {
holder.siteLogo.setImageBitmap(bitmap);
holder.siteLogo.setVisibility(View.VISIBLE);
},
error -> {
holder.siteName.setVisibility(View.VISIBLE);
})
);
}
I should unsubscribe when ViewHolder is re-used. It is easy.
But how stop all subscription when view is destroyed? I should also probably nullify presenter reference to avoid memory leak
I think the best way to do that would be to:
Keep a subscription reference in the SiteAdapter.ViewHolder
unsubscribe the subscription object in onBindViewHolder (it's called when the ViewHolder is reused)
Keep the CompositeSubscription object in your adapter
Use the onDetachedFromRecyclerView method of your Adapter to unsubscribe the compositeSubscription
Like so:
public class SiteAdapter extends RecyclerView.Adapter<SiteAdapter.ViewHolder> {
private CompositeSubscription compositeSubscription = new CompositeSubscription();
// other needed SiteAdapter methods
#Override
public void onBindViewHolder(SiteAdapter.ViewHolder holder, int position) {
if (holder.subscription != null && !holder.subscription.isUnsubscribed()) {
compositeSubscription.remove(holder.subscription);
// this will unsubscribe the subscription as well
}
Site site = sites.get(position);
holder.siteName.setText(site.getName());
requestHolderLogo(holder, site.getLinks().getLogoUrl());
}
private void requestHolderLogo(final SiteAdapter.ViewHolder holder, final String logoUrl) {
holder.subscription = presenter.bitmap(logoUrl)
.subscribe(
bitmap -> {
holder.siteLogo.setImageBitmap(bitmap);
holder.siteLogo.setVisibility(View.VISIBLE);
},
error -> {
holder.siteName.setVisibility(View.VISIBLE);
});
compositeSubscription.add(holder.subscription);
}
#Override
public void onDetachedFromRecyclerView(RecyclerView recyclerView) {
compositeSubscription.unsubscribe();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public Subscription subscription;
// some holder-related stuff
public ViewHolder(View itemView) {
super(itemView);
// init holder
}
}
}
For others which have the same problem:
viewDetachedFromWindow in the adapter is only called when the adapter is set to null in the onPause (Activity, Fragment) or onDetachFromWindow (Activity, Fragment)
recyclerview.setAdapter(null)
Then you get viewDetachedFromWindow(...) where you can release your internal states and subscriptions. I would setup your subscriptions in on bind, make sure before every bind call you relase old subscriptions as a view can be recycled.
Another possibility is to inflate a custom view instead of only a layout in your factory. Then you can make the cleanup in the custom view onDetachFromWindow(). You get the onDetachedFromWindow also without setting the adapter to null.
One can call public void onViewRecycled(#NonNull VH holder)
Overview: I'm having a chat application. Till now, I was using CursorAdapter with a Listview to load my chat items in the list. But now, I'm planning to refactor the code to use RecyclerView with RecyclerView.Adapter and a "Load More" functionality like whatsapp.
Issue: Memory consumption. With CursorAdapter, items not in viewable area were getting Garbage Collected, but now since I'm using an ArrayList of my CustomModal, once you load all the items in the list (by clicking on the "Load More" button) I'm seeing high memory consumption in the memory logs (No Garbage Collection).
My guess is now, I'm loading all the items in an ArrayList and that is causing the issue. Is that it?
Is there a way to avoid the issue or optimize the problem?
EDIT:
Can't post the complete code here, but here is a snippet of the kind of Adapter that I've implemented:
public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.MyViewHolder> {
private ArrayList<MyModal> mMyModals;
public MessageAdapter(ArrayList<MyModal> mMyModals) {
this.mMyModals = mMyModals;
//... Some fields initialization here
}
public void changeList(ArrayList<MyModal> myModals, boolean isLoadMoreEnabled){
this.mMyModals = myModals;
//... Some fields initialization here
notifyDataSetChanged();
}
public void toggleLoadMore(boolean isLoadMoreEnabled){
if(isLoadMoreEnabled){
//..Checks if load more is already enabled or not
//..If not then enables it by adding an item at 0th poition of MyModal list
//..Then notifyDataSetChanged()
}else{
//..Checks if load more is already disabled or not
//..If not then disables it by removing an item at 0th poition of MyModal list
//..Then notifyDataSetChanged()
}
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
MyViewHolder messageViewHolder = null;
View itemLayoutView = null;
MyModal.MessageType messageType = MyModal.MessageType.getMessageTypeFromValue(viewType);
switch (messageType){
case MESSAGE_TYPE1:
itemLayoutView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.layout1, null);
messageViewHolder = new Type1ViewHolder(itemLayoutView);
break;
case MESSAGE_TYPE2:
itemLayoutView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.layout2, null);
messageViewHolder = new Type2ViewHolder(itemLayoutView);
break;
}
return messageViewHolder;
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
final MyModal myModal = mMyModals.get(position);
MyModal.MessageType messageType = myModal.getMessageType();
holder.initialize(myModal);
}
#Override
public int getItemCount() {
return (mMyModals != null)?mMyModals.size():0;
}
#Override
public int getItemViewType(int position) {
return mMyModals.get(position).getMessageType().getValue();
}
public abstract class MyViewHolder extends RecyclerView.ViewHolder {
public MyViewHolder(View itemLayoutView) {
super(itemLayoutView);
}
public abstract void initialize(MyModal myModal);
}
class Type1ViewHolder extends MyViewHolder {
//...Variables
public Type1ViewHolder(View itemLayoutView) {
super(itemLayoutView);
//...variables initialization here
}
#Override
public void initialize(MyModal myModal) {
//...Setting values in view using myModal
}
}
class Type2ViewHolder extends MyViewHolder {
//...Variables
public TextViewHolder(View itemLayoutView) {
super(itemLayoutView);
//...variables initialization here
}
#Override
public void initialize(MyModal myModal) {
//...Setting values in view using myModal
}
}
}
First of all :
public void changeList(ArrayList<MyModal> myModals, boolean isLoadMoreEnabled){
this.mMyModals = myModals;
//... Some fields initialization here
notifyDataSetChanged();
}
Here you are creating a new arraylist and assigning it to your mMyModals. This means there are 2 arraylists at this point and they take up twice the amount of space than required. GC doesnt work the way you expect it to. Since the arraylist is initialized in your activity it will persist as long as the arraylist persists and so will the initial arraylist.
Instead of creating a new arraylist in your activity and passing it to changeList. Just clear your old arraylist and pass that.And also in adapter changeList method you can do the below
public void changeList(ArrayList<MyModal> myModals, boolean isLoadMoreEnabled){
this.mMyModals.clear();
this.mMyModels.addAll(myModels);
//... Some fields initialization here
notifyDataSetChanged();
}
Please let me know if i am not clear. Also show your activity code if this does not work.
Instead of replacing the whole ArrayList and calling notifyDataSetChanged, try adding the items to the ArrayList and then call notifyItemRangeInserted(int positionStart, int itemCount), maybe that could work. Also, you dont have to replace the Adapter's ArrayList. Your Activity/Fragment probably has the same ArrayList, just editing this list in your Activity/Fragment and then calling notifyItemRangeInserted(int positionStart, int itemCount) should do the trick. Also, instead of retrieving all the messages, you could also try to only get the next X amount of messages, so you wont retrieve the messages you already retrieved before (if you didn't do that already).
Hello I need help to open a new fragment and pass data when clicked on my Recycler CardView Grid.
Android Grid Image
I want to click on for example the champion Aatrox (first grid) and open a new fragment with Aatrox InformatiĆ³n. the same with the others champions of League of Legends.
I know that is inside of onClick function but I dont know how to do it.
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.itemView.setClickable(true);
holder.itemView.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
}
});
Here is my full ChampAdapter.java
public class ChampAdapter extends RecyclerView.Adapter<ChampAdapter.ViewHolder> {
public List<ChampionItemModel> champItem;
public ChampAdapter(List<ChampionItemModel> champItem){this.champItem = champItem;}
public class ViewHolder extends RecyclerView.ViewHolder{
TextView champName;
TextView roleChamp;
ImageView champImg;
public ViewHolder(View itemView) {
super(itemView);
this.champName = (TextView)itemView.findViewById(R.id.champ_name);
this.roleChamp = (TextView)itemView.findViewById(R.id.champ_role);
this.champImg = (ImageView)itemView.findViewById(R.id.champ_image);
}
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_champs,parent,false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.itemView.setClickable(true);
holder.itemView.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
}
});
holder.champName.setText(champItem.get(position).champName);
holder.roleChamp.setText(champItem.get(position).roleChamp);
holder.champImg.setImageResource(champItem.get(position).champImg);
}
#Override
public int getItemCount() { return champItem.size();}
}
First you should embed the RecyclerView inside a fragment, like you normally would, let's call it ChampionOverviewFragment.
Now you should have a SingleChampionFragment with a static newInstance method that accepts as parameters everything that you need to build the champion information (for example a String with the id of your champ). We want to open this fragment when we click on one of the cards in your cardview.
Your activity now only has one HostFragment that you fill with the ChampionOverviewFragment in its onCreate method. See my answer on how to create nested fragments.
Your onClick method can now look like this:
#Override
public void onClick(View v){
((MainActivity) holder.itemView.getContext()).openChampionFragment(holder.getChampionId);
}
Of course, then your MainActivity has to include the following method:
public void openChampionFragment(String id)
this.hostFragment.replaceFragment(SingleChampionFragment.newInstance(id));
}
If you also need backstack navigation, refer to the tutorial I linked in the other answer.
Below is a general method on how to communicate between fragments, so it should be applicable to your issue also.
Place the recyclerView inside a fragment.
I am assuming you are able to get the position of the Adapter. Put recyclerview in a fragment call RV and it's response is to be seen in fragment say RVvalues. You use an interface called PassRVValues
Code in RV fragment:
public class RV extends Fragment{
int RVposition;
PassRVValues passRVValues;
//
your code for recyclerview and other things
//
RVposition = position_value_obtained;
passRVValues.sendToOtherFragment(RVposition);
Here's the code for the interface. Make a new java class having just the interface.
public interface PassRVValues{
sendToOtherFragment(int value_from_RVFragment);
}
Code in the activity
public class MainActivity implements PassRVValues{
//
some code
//
#Override
public void sendToOtherFragment(int use_value_from_RVFragment){
//
Do something with data if you want
//
RVvalues Frag = (RVValues)getFragmentManager().findFragmentById(R.id.rvvalues);
Frag.ShowDataBasedOnPositionInRV(something_based_from_any_process_in_sendToOtherFragment_Method);
}
Now for the code in the RVValues fragment
public class RVValues extends Fragment{
//again any codes//
public void ShowDataBasedOnPositionInRV(int data_based_on_RV_position){
//do something//
}
This is the way to implement inter-fragment communication in the simplest manner. Hope this helps!
Cheers!