实现此功能的最佳策略是什么:
我有一个水平的RecyclerView卡.每张卡都将实现整个屏幕,但我希望它显示下一张卡的一部分,如果它有多个项目则显示前一张卡.
我知道我可以通过android:layout_width在适配器上设置我的卡来实现这一点,使其具有250dp的特定DP而不是match_parent.但它看起来不是一个合适的解决方案.
这是我的代码:
使用RecyclerView的活动:
class ListPokemon : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val items = createListPokemons()
recyclerView.adapter = PokemonAdapter(items)
recyclerView.layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)
recyclerView.setHasFixedSize(true)
val pagerSnapHelper = PagerSnapHelper()
pagerSnapHelper.attachToRecyclerView(recyclerView)
}
private fun createListPokemons(): List<Pokemon> {
val pokemons = ArrayList<Pokemon>()
pokemons += createPokemon("Pikachu")
pokemons += createPokemon("Bulbasaur")
pokemons += createPokemon("Charmander")
pokemons += createPokemon("Squirtle")
return pokemons
}
private fun createPokemon(name: String) = Pokemon(name = name, height = 1, weight = 69, id = …Run Code Online (Sandbox Code Playgroud)