简体   繁体   English

选择时如何将项目设置为回收站视图的中心

[英]How to set item to center of Recycler view when selected

I am using a RecyclerView to display items horizontally.我正在使用RecyclerView水平显示项目。 I want to set the selected item to center of the view like this我想像这样将所选项目设置为视图的中心

在此处输入图像描述 . .

This is how I am doing it:这就是我的做法:

LinearLayoutManager layoutManager
                = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
        recyclerView.setLayoutManager(layoutManager);

I am using a RecyclerView to display items horizontally.我正在使用RecyclerView水平显示项目。 I want to set the selected item to center of the view like this我想像这样将所选项目设置为视图的中心

在此处输入图片说明 . .

This is how I am doing it:这就是我的做法:

LinearLayoutManager layoutManager
                = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
        recyclerView.setLayoutManager(layoutManager);

I am using a RecyclerView to display items horizontally.我正在使用RecyclerView水平显示项目。 I want to set the selected item to center of the view like this我想像这样将所选项目设置为视图的中心

在此处输入图片说明 . .

This is how I am doing it:这就是我的做法:

LinearLayoutManager layoutManager
                = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
        recyclerView.setLayoutManager(layoutManager);

I am using a RecyclerView to display items horizontally.我正在使用RecyclerView水平显示项目。 I want to set the selected item to center of the view like this我想像这样将所选项目设置为视图的中心

在此处输入图片说明 . .

This is how I am doing it:这就是我的做法:

LinearLayoutManager layoutManager
                = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
        recyclerView.setLayoutManager(layoutManager);

I am using a RecyclerView to display items horizontally.我正在使用RecyclerView水平显示项目。 I want to set the selected item to center of the view like this我想像这样将所选项目设置为视图的中心

在此处输入图片说明 . .

This is how I am doing it:这就是我的做法:

LinearLayoutManager layoutManager
                = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
        recyclerView.setLayoutManager(layoutManager);

Define your custom layout manager as将您的自定义布局管理器定义为

class CenterLayoutManager : LinearLayoutManager {
    constructor(context: Context) : super(context)
    constructor(context: Context, orientation: Int, reverseLayout: Boolean) : super(context, orientation, reverseLayout)
    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int, defStyleRes: Int) : super(context, attrs, defStyleAttr, defStyleRes)

    override fun smoothScrollToPosition(recyclerView: RecyclerView, state: RecyclerView.State, position: Int) {
        val centerSmoothScroller = CenterSmoothScroller(recyclerView.context)
        centerSmoothScroller.targetPosition = position
        startSmoothScroll(centerSmoothScroller)

    }

    private class CenterSmoothScroller(context: Context) : LinearSmoothScroller(context) {
        override fun calculateDtToFit(viewStart: Int, viewEnd: Int, boxStart: Int, boxEnd: Int, snapPreference: Int): Int = (boxStart + (boxEnd - boxStart) / 2) - (viewStart + (viewEnd - viewStart) / 2)
    }
}

Then assign this layout manager to your recycler view然后将此布局管理器分配给您的回收站视图

myRecyclerview.layoutManager =
        CenterLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)

In recyclerview's item onClick method use在 recyclerview 的 item onClick 方法中使用

myRecyclerview.smoothScrollToPosition(position)

where position should get from onBindViewHolder位置应该从 onBindViewHolder 获得

Also use LinearSnapHelper as也使用 LinearSnapHelper 作为

 val snapHelper = LinearSnapHelper()
    snapHelper.attachToRecyclerView(myRecyclerview)

it will controll scrolling effectively它将有效地控制滚动

Also attatch scroll listner to recyclerview to get item at center position还将滚动侦听器附加到 recyclerview 以在中心位置获取项目

Recyclerview.setOnScrollListener(object:
            RecyclerView.OnScrollListener() {
            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)
                var view=recyclerView[0]

            }
        })

check out this stackoverflow answer for more details查看此 stackoverflow 答案以获取更多详细信息

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

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