TreeView
TreeView copied to clipboard
how to set on click listener and toast id item click?
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:
- 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);
}
- 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;
});
}
- 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;
}
- In the final step when you create new instance of BookViewFactory implement interface methods.
good luck.