TreeView icon indicating copy to clipboard operation
TreeView copied to clipboard

how to set on click listener and toast id item click?

Open promahdimosavi opened this issue 4 years ago • 1 comments

promahdimosavi avatar May 31 '21 08:05 promahdimosavi

Hi I don't know why there is not enough documentation but it is possible to handle onClick(), onLongClick() or any other kind of action that you want.

To do that you should follow below steps:

  1. Add an interface to handle on click into ViewBinder like below:
     public BookViewBinder(View itemView, OnClick onClick) {
        super(itemView);
        this.onClick = onClick;
        rootView = itemView.findViewById(R.id.item_nest_book_root);
    }
  1. In bindView method use it like below:
    @Override
    public void bindView(TreeNode treeNode) {

        rootView.setOnLongClickListener(v -> {
            onClick.onBookLongClick((Book) (treeNode.getValue()));  //cast treeNode.value() to  prefered data type
            return true;
        });
    } 
  1. Then pass the interface instance through the ViewFactory constructor like below:

public class BookViewFactory extends BaseNodeViewFactory {

    OnClick onClick;

    public BookViewFactory(OnClick onClick) {
        this.onClick = onClick;
    }
  1. In the final step when you create new instance of BookViewFactory implement interface methods.

good luck.

ashrafimostafa avatar Aug 15 '22 19:08 ashrafimostafa