滚动时更新recyclerview的位置(android)

编程入门 行业动态 更新时间:2024-10-18 22:32:48
本文介绍了滚动时更新recyclerview的位置(android)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何更新滚动位置.现在,每次我单击项目时,位置都会更新.我想在每次滚动时更新位置.我想做的是拥有一个文本视图,当滚动浏览各个项目时,该视图将得到更新.

How can i update position on scroll. right now the position gets updated every time i click on item. i want to update the position every time i scroll. What i wanna do is have a text view which will get updated when scrolling through the items.

recyclerView.addOnItemTouchListener(new GalleryAdapter.RecyclerTouchListener(this, recyclerView, new GalleryAdapter.ClickListener() { @Override public void onClick(View view, int position) { Intent intent = new Intent(mainActivityCarasoul.this, PDFViewerActivity.class); intent.putExtra(PDFViewerActivity.TAG, books.get(position)); intent.putExtra("from", "mainActivityCarasoul"); startActivity(intent); } @Override public void onLongClick(View view, int position) { } })); recyclerView.addOnScrollListener(new CenterScrollListener()); recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrollStateChanged(RecyclerView recyclerView, int newState) { super.onScrollStateChanged(recyclerView, newState); // title.setText(books.get(position).getName()); } });

推荐答案

使用此方法,您将必须在您的活动中保持一个previousPosition,而在您的适配器类中必须保持一个selectedItem,因为int会初始化previousPoistion = -1;并selectedPosition = 1;

use this, you will have to maintain a previousPosition in your activity and in your adapter class a selectedItem as int intialize previousPoistion=-1; and selectedPosition=1;

@Override public void onScrollStateChanged(RecyclerView recyclerView, int newState) { super.onScrollStateChanged(recyclerView, newState); switch (newState) { case 0: int pos = linearLayoutManager1.findLastVisibleItemPosition(); yourAdapter.selectedPosition = pos - 1; previousPosition = pos - 1; yourTextView.setText(yourLIst.get(pos-1)); break; } } });

并在适配器的onBindView容器中

and in your onBindView Holder of adapter

@Override public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) { if (position == selectedPosition) { //do what you want when selected } }

更多推荐

滚动时更新recyclerview的位置(android)

本文发布于:2023-10-08 01:29:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1471114.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:位置   recyclerview   android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!