我的应用程序中有一系列曾经被称为listViews的系列,现在是RecyclerViews。我很好奇提高模块性和减少我的应用程序的这一部分的重复的最有效方法。
例如,我有GroupMemberFragment
和FriendFragment
,它们几乎相同RecyclerView.ViewHolders
和RecyclerView.Adapters
重构的最佳方法是什么,以减少代码重复,但仍然允许每个相应的项目列表对其数据是唯一的,并且足够灵活,以便将来添加更多数据?
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();
}
}
}
答案 0 :(得分:0)
你可以为你的对象创建工厂方法,使它更整洁但你的代码没有紧密耦合它只是看起来很邋bec因为你做了两次同样的事情,但是他们做的是完全独立的事情,所以要么留下它,要么,用一些对象工厂或app类变量清理它
答案 1 :(得分:0)
你可以从这两个类中取出适配器类,并为它创建一个单独的Java文件,并为你拥有的两个类使用相同的适配器。 为此,您可以为适配器创建两个不同的构造函数并实现它。