TellH / RecyclerTreeView

TreeView implement in Android with RecyclerView.
Apache License 2.0
651 stars 117 forks source link

Scroll up last visible item while expanding #15

Open vinayjoglekar opened 6 years ago

vinayjoglekar commented 6 years ago

Hi Team, I am trying to implement a functionality where I can scroll up and expand childs of node on click of node whose all childs are not visible on screen. Please help me if there is any workaround for the same.

Thanks in advance.

naveen3186 commented 3 years ago

Try this code

adapter.setOnTreeNodeListener(new TreeViewAdapter.OnTreeNodeListener() {
            @Override
            public boolean onClick(TreeNode node, RecyclerView.ViewHolder holder) {
                if (!node.isLeaf()) {
                    onToggle(!node.isExpand(), holder);
                    int position = holder.getAdapterPosition();
                    int distanceInPixels;
                    View firstVisibleChild = rv.getChildAt(0);
                    int itemHeight = firstVisibleChild.getHeight();
                    int currentPosition = rv.getChildAdapterPosition(firstVisibleChild);
                    int p = Math.abs(position - currentPosition);
                    if (p > 5) distanceInPixels = (p - (p - 5)) * itemHeight;
                    else       distanceInPixels = p * itemHeight;
                    linearLayoutManager.scrollToPositionWithOffset(position, distanceInPixels);
                }
                return false;
            }
});