代码地址
https://github.com/MuchContact/TreeViewForContactBook.git
1. 模型
联系人列表组件关心的模型只有三个:
GroupRepository 模型入口,获取分组信息
Group 掌管每个组的内部信息
ContactMember 联系人,属于某一组
2. 原型
使用者只需要实现这三个接口,下面代码展示如何快速构建一个原型:
GroupRepositoryImpl
public class GroupRepositoryImpl implements GroupRepository {
private List<Group> groups;
public GroupRepositoryImpl() {
this.groups = new ArrayList<>();
Group group1 = new GroupImpl("局领导");
group1.addMembers(new ContactMemberImpl("张良勖", "87463739"), new ContactMemberImpl("李存浩", "76353674"));
Group group2 = new GroupImpl("办公室");
group2.addMembers(new ContactMemberImpl("张良勖", "87463739"), new ContactMemberImpl("李存浩", "76353674"));
groups.add(group1);
groups.add(group2);
}
@Override
public Group getGroup(int groupPosition) {
return groups.get(groupPosition);
}
@Override
public List<Group> getGroups() {
return groups;
}
}
GroupImpl
public class GroupImpl implements Group {
private final String name;
private List<ContactMember> members;
public GroupImpl(String groupName) {
this.name = groupName;
members = new ArrayList<>();
}
@Override
public String name() {
return name;
}
@Override
public ContactMember member(int index) {
return index < members.size() ? members.get(index) : null;
}
@Override
public void addMembers(ContactMember member, ContactMember... newMembers) {
members.add(member);
for (ContactMember mem : newMembers) {
members.add(mem);
}
}
@Override
public int memberCount() {
return members.size();
}
}
ContactMemberImpl
public class ContactMemberImpl implements ContactMember {
private final String name;
private final String phone;
public ContactMemberImpl(String name, String phone) {
this.name = name;
this.phone = phone;
}
@Override
public String name() {
return name;
}
@Override
public String phone() {
return phone;
}
}
调用示例
public class MainActivity extends Activity {
private TreeView treeView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
treeView = (TreeView) findViewById(R.id.tree_view);
treeView.setHeaderView(getLayoutInflater().inflate(R.layout.list_head_view, treeView,
false));
GroupRepository groupRepository = new GroupRepositoryImpl();
treeView.setAdapter(new DefaultTreeViewAdapter(this, treeView, groupRepository));
}
}
原型效果
3. 产品
未完待续