本文介绍了滚动时更新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)
发布评论