简体   繁体   English

如何一键滑动两个列表视图

[英]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.沿着 y 轴,第二个列表视图的项目必须在第一个列表视图的项目之间。 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.我认为它可以使用OnScrollListener来实现,基本上你可以在它的兄弟视图滚动时滚动其他列表/回收器视图。 Here is snippet, assuming you use Recycleview这是片段,假设您使用 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和活动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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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