美文网首页
基于接口编程示例——联系人列表

基于接口编程示例——联系人列表

作者: muco | 来源:发表于2016-07-14 12:27 被阅读43次

    代码地址
    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. 产品

    未完待续

    相关文章

      网友评论

          本文标题:基于接口编程示例——联系人列表

          本文链接:https://www.haomeiwen.com/subject/ftpejttx.html