简体   繁体   中英

how to slide two listviews with one touch

image of two listviews

I am trying to build this image that I gave the link. Second listview to the right of the first listview. Both listview items are clickable separately. Along the y-axis, second listview's items must be in between first listview's items. Both of them should scroll if user scrolls one of them. I have some solutions. First one is to control two listview in one touch. Another solution approach is using one list view. But it has problems of touch events for the second listview. It seems first approach is right but I don't know how to connect two listviews. Thanks in advance.

I thing it can be achieved using OnScrollListener , essentially you can scroll other list/recyclerview on scroll of it's sibling view. Here is snippet, assuming you use Recycleview

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val left = findViewById<RecyclerView>(R.id.left)
        val right = findViewById<RecyclerView>(R.id.right)
        left.adapter = Adapter()
        right.adapter = Adapter()

        val switcher = FocusSwitchListener(left, right)

        left.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)
                if (switcher.isLeftLastFocused) {
                    right.stopScroll()
                    right.scrollBy(dx, dy)
                }
            }
        })
        right.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)
                if (switcher.isRightLastFocused) {
                    left.stopScroll()
                    left.scrollBy(dx, dy)
                }
            }
        })
    }
}

FocusSwitchListener

class FocusSwitchListener(left: View, right: View) {
    var isLeftLastFocused: Boolean = false
        private set
    var isRightLastFocused: Boolean = false
        private set

    init {
        left.setOnTouchListener { _, event ->
            if (event?.action == MotionEvent.ACTION_DOWN) {
                isLeftLastFocused = true
                isRightLastFocused = false
            }
            false
        }
        right.setOnTouchListener { _, event ->
            if (event?.action == MotionEvent.ACTION_DOWN) {
                isRightLastFocused = true
                isLeftLastFocused = false
            }
            false
        }
    }
}

and activity xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:orientation="horizontal"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

   <androidx.recyclerview.widget.RecyclerView
       android:layout_width="0dp"
       android:id="@+id/left"
       android:layout_weight="0.5"
       app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
       android:layout_height="match_parent"/>
    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="0dp"
        android:id="@+id/right"
        android:layout_weight="0.5"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
        android:layout_height="match_parent"/>
</LinearLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM