RecyclerView将中心项目对齐到中心后如何获取中心项目? [英] How to get the center item after RecyclerView snapped it to center?
问题描述
我正在实现水平RecyclerView
,该水平RecyclerView
可以在滚动后将项目对齐到中心,例如Google play App水平列表. 这是评论.
I'm implementing a horizontal RecyclerView
that snap items to center after scrolling, like Google play App horizontal lists. This is a review.
我的代码如下:
MainMenuAdapter mainMenuAdapter = new MainMenuAdapter(this, mDataset);
final LinearLayoutManager layoutManagaer = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
RecyclerView mainMenu = (RecyclerView) findViewById(R.id.main_menu);
mainMenu.setLayoutManager(layoutManagaer);
mainMenu.setAdapter(mainMenuAdapter);
final SnapHelper snapHelper = new LinearSnapHelper();
snapHelper.attachToRecyclerView(mainMenu);
在RecyclerView
对齐中心后,如何获取中心项(位置)?没有为此提供任何侦听器的方法吗?
另外,当触摸项目视图时,我想将其对齐.我该怎么办?
How I can get the center item (position) after RecyclerView
snapped it to center? Isn't there any listener implementation for this?
Also, when an item view be touched, I want to snap it to center. How can I do this?
推荐答案
如果您需要View,可以致电
if you need the View, you can call
View view = snapHelper.findSnapView(layoutManagaer);
一旦有了View,就应该能够获得该View在数据集上的位置.例如使用
once you have the View, you should be able to get the position on the dataset for that View. For instance using
mainMenu.getChildAdapterPosition(view)
这篇关于RecyclerView将中心项目对齐到中心后如何获取中心项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!