如何使用graphview库显示族树结构

zi8p0yeb  于 2021-07-12  发布在  Java
关注(0)|答案(0)|浏览(186)

我正在开发一个android应用程序,我想使用teambloxgraphview库显示一个家族树。
这是一个树类,它保存了家谱的数据(灵感来自于这个答案:)。

public class PersonTree implements Serializable {

    private Person data;

    private PersonTree parent;

    private ArrayList<PersonTree> children;

    public PersonTree() {
    }

    public Person getData() {
        return data;
    }

    public PersonTree getParent() {
        return parent;
    }

    public ArrayList<PersonTree> getChildren() {
        return children;
    }

    public boolean isRoot() {
        return parent == null;
    }

    public boolean isLeaf() {
        return children == null || children.isEmpty();
    }

    public int getLevel() {
        return isRoot() ? 0 : parent.getLevel() + 1;
    }
}

这是保存person数据的person类:

public class Person implements Serializable {

    private String id;

    private String name;

    private Date birthdate;

    private String fatherId;

    private String fatherName;

    public Person() {
    }

    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public Date getBirthdate() {
        return birthdate;
    }

    public String getFatherId() {
        return fatherId;
    }

    public String getFatherName() {
        return fatherName;
    }
}

在库示例中,我以相同的方式构建了图形视图,但我一直在将节点添加到图形中,下面是我的代码:

public class GraphActivity extends AppCompatActivity {

    PersonTree familyTree;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        GraphView graphView = findViewById(R.id.graph);

        final Graph graph = new Graph();

        //How to loop through the tree and add children to the graph, the next lines are from the 
        //library code sample.

        final Node node1 = new Node(parent.getName());
        final Node node2 = new Node(child1.getName());
        final Node node3 = new Node(child2.getName());

        graph.addEdge(node1, node2);
        graph.addEdge(node1, node3);

        GraphAdapter adapter = new GraphAdapter<GraphView.ViewHolder>(graph) {

            @NonNull
            @Override
            public GraphView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                final View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.node, parent, false);
                return new SimpleViewHolder(view);
            }

            @Override
            public void onBindViewHolder(GraphView.ViewHolder viewHolder, Object data, int position) {
                ((SimpleViewHolder) viewHolder).textView.setText((String)data);
            }
        };
        graphView.setAdapter(adapter);

        final BuchheimWalkerConfiguration configuration = new BuchheimWalkerConfiguration.Builder()
                .setSiblingSeparation(100)
                .setLevelSeparation(300)
                .setSubtreeSeparation(300)
                .setOrientation(BuchheimWalkerConfiguration.ORIENTATION_TOP_BOTTOM)
                .build();

        graphView.setLayout(new BuchheimWalkerAlgorithm(configuration));
    }
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题