【问题标题】:how to slide two listviews with one touch如何一键滑动两个列表视图
【发布时间】:2020-02-16 14:13:19
【问题描述】:

image of two listviews

我正在尝试构建我提供链接的图像。第一个列表视图右侧的第二个列表视图。两个列表视图项目都可以单独点击。沿着 y 轴,第二个列表视图的项目必须在第一个列表视图的项目之间。如果用户滚动其中一个,它们都应该滚动。我有一些解决方案。 第一个是一键控制两个列表视图。 另一种解决方案是使用一个列表视图。但它有第二个列表视图的触摸事件问题。似乎第一种方法是正确的,但我不知道如何连接两个列表视图。提前致谢。

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    我认为它可以使用OnScrollListener 来实现,基本上你可以在滚动它的兄弟视图时滚动其他列表/回收器视图。 这里是sn-p,假设你使用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)
                    }
                }
            })
        }
    }
    

    焦点开关监听器

    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
            }
        }
    }
    

    和活动 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>
    

    【讨论】:

    • 此解决方案适用于 RecyclerView 而不是 ListView。我试过但不知道如何使它与 ListView 一起工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-29
    相关资源
    最近更新 更多