How to apply graphLayoutManager and TreeEdgedecoration classes to a recyclerview in java android?

I want to visually represent a tree in android with this library. by tree I mean a chart with nodes, not a file system-like tree. In their github page they provided a usage example with kotlin code:

private void setupGraphView {
    val recycler = findViewById(R.id.recycler)

    // 1. Set a layout manager of the ones described above that the RecyclerView will use.
    val configuration = BuchheimWalkerConfiguration.Builder()
                    .setSiblingSeparation(100)
                    .setLevelSeparation(100)
                    .setSubtreeSeparation(100)
                    .setOrientation(BuchheimWalkerConfiguration.ORIENTATION_TOP_BOTTOM)
                    .build()
    recycler.layoutManager = BuchheimWalkerLayoutManager(context, configuration)

    // 2. Attach item decorations to draw edges
    recycler.addItemDecoration(TreeEdgeDecoration())

    // 3. Build your graph
    val graph = Graph()
    val node1 = Node("Parent")
    val node2 = Node("Child 1")
    val node3 = Node("Child 2")

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

    // 4. You will need a simple Adapter/ViewHolder.
    // 4.1 Your Adapter class should extend from `AbstractGraphAdapter`
    adapter = object : AbstractGraphAdapter<NodeViewHolder>() {

        // 4.2 ViewHolder should extend from `RecyclerView.ViewHolder`
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NodeViewHolder {
            val view = LayoutInflater.from(parent.context)
                    .inflate(R.layout.node, parent, false)
            return NodeViewHolder(view)
        }

        override fun onBindViewHolder(holder: NodeViewHolder, position: Int) {
            holder.textView.text = getNodeData(position).toString()
        }
    }.apply {
        // 4.3 Submit the graph
        this.submitGraph(graph)
        recycler.adapter = this
    }
}

and I tried to recreate it in java:

public class GraphAdapter extends AbstractGraphAdapter{
    @NonNull
    @Override
    public NodeHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.nodelayout, parent, false);
        return new NodeHolder(view);

    }
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {

    }
    public class NodeHolder extends RecyclerView.ViewHolder {
        protected Button button;
        public NodeHolder(@NonNull View itemView) {super(itemView);button=itemView.findViewById(R.id.subjectbtn);}
    }

    private void setupGraphView(View v) {
        RecyclerView recycler = v.findViewById(R.id.recycler);

        BuchheimWalkerConfiguration configuration = new BuchheimWalkerConfiguration.Builder()
                .setSiblingSeparation(100)
                .setLevelSeparation(100)
                .setSubtreeSeparation(100)
                .setOrientation(BuchheimWalkerConfiguration.ORIENTATION_TOP_BOTTOM)
                .build();
        GraphLayoutManager graphLayoutManager=  new BuchheimWalkerLayoutManager(v.getContext(), configuration);
        recycler.setLayoutManager(graphLayoutManager);
        recycler.addItemDecoration(new TreeEdgeDecoration());


        Graph graph = new Graph();
        Node node1 = new Node("Parent");
        Node node2 = new Node("Child 1");
        Node node3 = new Node("Child 2");
        graph.addEdge(node1, node2);
        graph.addEdge(node1, node3);

        AbstractGraphAdapter<NodeHolder> adapter = new GraphAdapter();
        adapter.submitGraph(graph);
        recycler.setAdapter(adapter);
    }
}

however, I get an error in the following lines, first error second error

this is because the graphLayoutManager and the TreeEdgeDecoration dont extend\implement the classes that the methods require. So the recylerview in the kotlin version accepts them as arguments, but doesn't in the java version.

I cant find any other library to achieve this. But, In this library there is a GraphView class. it has the setAdapter() function,which doesnt rise errors, and addItemDecoration() that cant accept new TreeEdgeDecoration() as an argument (for the same reason as the recycler view). It also doesnt have a setLayoutManager() function, meaning I cant set the graph layout. I also dont know if this class acts exactly like the RecyclerView class.

  GraphView graphView = new GraphView(v.getContext());
  graphView.setAdapter(adapter);
  graphView.addItemDecoration(new TreeEdgeDecoration());

Upvotes: 0

Views: 28

Answers (0)

Related Questions