gpt4 book ai didi

android - 如何重用 RecyclerView 对象、适配器和 View 持有者

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:23:53 26 4
gpt4 key购买 nike

我的应用程序中有一系列以前称为 listView 的东西,现在是 RecyclerView。我很好奇提高模块化和减少我应用程序这一部分重复的最有效方法。

例如,我有 GroupMemberFragmentFriendFragment,它们具有几乎相同的 RecyclerView.ViewHoldersRecyclerView.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();
}
}
}

最佳答案

您可以从两个类中取出适配器类并为其创建一个单独的 Java 文件,并为您拥有的两个类使用相同的适配器。为此,您可以为适配器创建两个不同的构造函数并实现它。

关于android - 如何重用 RecyclerView 对象、适配器和 View 持有者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34868085/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com