Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dragging with Recyclerview #1

Open
Muratoter opened this issue Jul 20, 2018 · 5 comments
Open

Dragging with Recyclerview #1

Muratoter opened this issue Jul 20, 2018 · 5 comments

Comments

@Muratoter
Copy link

Auto scroll not working during dragging process with using recyclerview.

@403462630
Copy link
Owner

must be use FCNestedScrollView + FCRecyclerview, and FCRecyclerview must be set fc_scroll_mode

@Muratoter
Copy link
Author

Muratoter commented Aug 10, 2018

Still FCRecyclerview auto scrolling not working when dragging.

`<com.fc.nestedscrollview.FCNestedScrollView
android:id="@+id/sv_project_main"
android:layout_width="match_parent"
android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <com.fc.nestedscrollview.FCRecyclerView
            android:id="@+id/rv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fillViewport="true"
            app:fc_scroll_mode="all" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:visibility="visible">

            <Button
                android:id="@+id/btn1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingEnd="20dp"
                android:paddingStart="20dp"
                android:textAlignment="textStart"
                android:textColor="?attr/content_text"
                android:textSize="15sp"
                android:textStyle="bold" />

            <Button
                android:id="@+id/btn2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingEnd="20dp"
                android:paddingStart="20dp"
                android:textAlignment="textStart"
                android:textColor="?attr/content_text"
                android:textSize="15sp"
                android:textStyle="bold" />
        </LinearLayout>
    </LinearLayout>
</com.fc.nestedscrollview.FCNestedScrollView>`

@403462630
Copy link
Owner

FCRecyclerView height can not set wrap_content, must set actual height
if FCRecyclerView height set wrap_content,FCRecyclerView all item is rendered,so FCRecyclerView can not scroll

@Muratoter
Copy link
Author

I set the match_parent but still not working. I think there is a misunderstanding. It is not work only when recyclerview item on long touch and dragging to bottom. In this case recyclerview is not working auto scroll to bottom.

@403462630
Copy link
Owner

@Muratoter this is not bug, Because auto scroll work only when drag the item to the last visible item position, but the last visible item is no show, so is not auto scroll.

this is a way that resolve this problem

example: Tab1Fragment

private fun getViewLocation(view: View) : Rect {
        val location = intArrayOf(0, 0)
        view.getLocationInWindow(location)
        val height = view.height
        val width = view.width
        val rect = Rect()
        rect.left = location[0]
        rect.top = location[1]
        rect.bottom = rect.top + height
        rect.right = rect.left + width
        return rect
    }

    fun ifNeedScrollToBottomOrTop(recyclerView: RecyclerView, source: RecyclerView.ViewHolder, y: Int) {
        nestedScrollView?.let {
            val rect = getViewLocation(recyclerView)
            val scrollViewRect = getViewLocation(it)
            Log.i("ifNeedScrollToBottom", "height: ${recyclerView.height}, itemView.height: ${source.itemView.height}, scrollViewRect.top: ${scrollViewRect.top}, scrollViewRect.bottom: ${scrollViewRect.bottom}, y: $y, rect.top: ${rect.top} rect.bottom: ${rect.bottom}")
            if (y + source.itemView.height + rect.top > scrollViewRect.bottom) {
                if (it.canScrollVertically(1)) {
                    Log.i("ifNeedScrollToBottom", "scroll to bottom")
                    // 1. scroll to recyclerView bottom position
                    val bottomOffset = rect.bottom - scrollViewRect.bottom
                    it.smoothScrollBy(0, bottomOffset)
                    // 2. scroll itemView height distance
//                    it.smoothScrollBy(0, source.itemView.height)
                    // 3. scroll to scrollview bottom
//                    it.smoothScrollTo(0, it.getChildAt(0).height)
                }
            } else if (y + rect.top < scrollViewRect.top) {
                if (it.canScrollVertically(-1)) {
                    Log.i("ifNeedScrollToBottom", "scroll to top")
                    // 1. scroll to recyclerView top position
                    val topOffset = rect.top - scrollViewRect.top
                    it.scrollBy(0, topOffset)
                    // 2. scroll itemView height distance
//                    it.smoothScrollBy(0, -source.itemView.height)
                    // 3. scroll to scrollview top
//                    it.smoothScrollTo(0, 0)
                }
            }
        }
    }

//ItemTouchHelper.Callback
override fun onMoved(recyclerView: RecyclerView?, source: RecyclerView.ViewHolder?, fromPos: Int, target: RecyclerView.ViewHolder?, toPos: Int, x: Int, y: Int) {
        super.onMoved(recyclerView, source, fromPos, target, toPos, x, y)
        ifNeedScrollToBottomOrTop(recyclerView!!, source!!, y)
 }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants