How to Reuse RecipeViewView Objects, Adapters, and Holders

I have a series that used to be called listViews in my application, which are now RecyclerViews. I am curious as the most effective way to increase modularity and reduce the repetition of this section of my application.

For example, I have GroupMemberFragment and FriendFragment , which have almost identical RecyclerView.ViewHolders and RecyclerView.Adapters

What is the best way to reorganize to reduce code duplication, but still allow each respective list of items to be unique to their data and flexible enough to add additional data in the future?

GroupMemberFragment

 public class GroupMemberFragment extends Fragment { RecyclerView mRecyclerView; ArrayList<Group> mGRoups; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.recycler_layout, container, false); mRecyclerView = (RecyclerView) view.findViewById(R.id.recycler_view_layout); mRecyclerView.setHasFixedSize(true); mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); // Only things that need to change across all the list classes.... mGRoups = GroupListProvider.getInstance(getActivity()).getGroups(); mRecyclerView.setAdapter(new GroupAdapter(mGRoups)); return view; } // inner Class private class GroupViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { private final TextView mName; private final TextView mChannel; public GroupViewHolder(View itemView) { super(itemView); mName = (TextView) itemView.findViewById(R.id.tv_name); mChannel = (TextView) itemView.findViewById(R.id.tv_number); mName.setOnClickListener(this); } public void bindGroup(Group group){ mName.setText(group.getName().toString()); mChannel.setText(group.getGroupChannelID().toString()); } @Override public void onClick(View view) { int itemPosition = getAdapterPosition(); Toast.makeText(getContext(), "Position is: " + String.valueOf(itemPosition), Toast.LENGTH_LONG).show(); } } class GroupAdapter extends RecyclerView.Adapter<GroupViewHolder> { ArrayList<Group> groupList; GroupAdapter(ArrayList<Group> object){ this.groupList = object; } @Override public GroupViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()) .inflate(R.layout.rv_item, parent, false); return new GroupViewHolder(view); } @Override public void onBindViewHolder(GroupViewHolder holder, int position) { Group group = groupList.get(position); holder.bindGroup(group); } @Override public int getItemCount() { return groupList.size(); } } } 

FriendFragment

 public class FriendFragment extends Fragment { RecyclerView mRecyclerView; ArrayList<Friend> mContacts; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.recycler_layout, container, false); mRecyclerView = (RecyclerView) view.findViewById(R.id.recycler_view_layout); mRecyclerView.setHasFixedSize(true); mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); mContacts = FriendProvider.getInstance(getActivity()).getFriends(); mRecyclerView.setAdapter(new ContactAdapter(mContacts)); return view; } // Inner Class private class ContactViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{ private final TextView mName; private final TextView mNumber; private Friend mContact; public ContactViewHolder(View itemView) { super(itemView); mName = (TextView) itemView.findViewById(R.id.tv_name); mNumber = (TextView) itemView.findViewById(R.id.tv_number); mName.setOnClickListener(this); } public void bindContact(Friend contact){ mContact = contact; mName.setText(contact.getFirstName().toString() + " " + contact.getLastName()); mNumber.setText(contact.getFriendChannelID().toString()); } @Override public void onClick(View view) { int itemPosition = getAdapterPosition(); Toast.makeText(getContext(), "Position is: " + String.valueOf(itemPosition), Toast.LENGTH_LONG).show(); } } class ContactAdapter extends RecyclerView.Adapter<ContactViewHolder> { ArrayList<Friend> contactList; ContactAdapter(ArrayList<Friend> object){ this.contactList = object; } @Override public ContactViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()) .inflate(R.layout.rv_item, parent, false); return new ContactViewHolder(view); } @Override public void onBindViewHolder(ContactViewHolder holder, int position) { Friend contact = contactList.get(position); holder.bindContact(contact); } @Override public int getItemCount() { return contactList.size(); } } } 
+5
source share
2 answers

You could create factory methods to create your object to make it a little tidy, but your code is not tightly coupled, it just looks messy because you do the same thing twice, however they do completely separate things, so either leave it or , clear it with some object factories or application class variables

0
source

you can take the adapter class from both classes and create a separate Java file for it and use the same adapter for both classes. To do this, you can create two different constructor for the adapter and achieve this.

0
source

Source: https://habr.com/ru/post/1240888/


All Articles