我RecyclerView通过为页眉和页脚添加单独的布局来自定义.我创建了常量来确定适配器类中的页眉,页脚和列表项的属性.我还创建了一个ViewHolder模式,并根据视图类型指定要显示的布局.我通过覆盖getItemViewType方法将标题固定在第0个位置和页脚最后位置的页脚.
我想让页脚元素可点击,所以我指定setOnClickListener(new View.OnClickListener())并覆盖onClick(view: View)
我的目标是单击页脚和scrollToPosition0或1(0 =页眉,1 =第一项元素).
那是MyAdapter的定义:
class MyAdapter(context: Context, data: Array[String]) extends RecyclerView.Adapter[RecyclerView.ViewHolder]
...
override def onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int): Unit = holder match {
...
case holder:FooterViewHolder =>
holder.backToTop.setOnClickListener(new View.OnClickListener() {
override def onClick (view: View) {
backToTop(???)
Toast.makeText (context, "Clicked Footer", Toast.LENGTH_SHORT).show()
}
})
...
}
...
Run Code Online (Sandbox Code Playgroud)
我读过我只需要这样做: recyclerView.getLayoutManager().scrollToPosition(position)
不幸的是我无法从Adapter类访问LayoutManager.任何想法我能做什么?
我的android项目中有一个recyclerview,它在每个视图中显示媒体内容.我想要实现的是,当我向上和向下滚动时,我能够播放/暂停媒体.我需要获得完全可见视图的适配器位置.我正在做这样的事情.
在我的活动片段中我有这个:
layoutmanager = new LinearLayoutManager(Activity);
adapter = new FeedAdapter(vid, userName, this.Context);
feeditem.SetLayoutManager(layoutmanager);
feeditem.SetAdapter(adapter);
var onScrollListener = new XamarinRecyclerViewOnScrollListener(Activity, layoutmanager, adapter);
Run Code Online (Sandbox Code Playgroud)
滚动侦听器事件如下所示:
public override void OnScrollStateChanged(RecyclerView recyclerView, int newState)
{
base.OnScrollStateChanged(recyclerView, newState);
if (newState == (int)ScrollState.Idle)
{
layoutmanager = (LinearLayoutManager)recyclerView.GetLayoutManager();
int firstVisiblePosition = layoutmanager.FindFirstCompletelyVisibleItemPosition();
int visible = layoutmanager.FindFirstVisibleItemPosition();
int last = layoutmanager.FindLastVisibleItemPosition();
if (firstVisiblePosition >= 0)
{
if (oldFocusedLayout != null)
{
Toast.MakeText(ctx, "Stop Video", ToastLength.Long).Show();
}
}
currentFocusedLayout = layoutmanager.FindViewByPosition(firstVisiblePosition);
Toast.MakeText(ctx, "Play video", ToastLength.Long).Show();
oldFocusedLayout = currentFocusedLayout;
}
} …Run Code Online (Sandbox Code Playgroud) 我findLastCompletelyVisibleItemPosition()用来确定我的RecyclerView中的最后一个可见项目.
这是我如何设置布局的代码片段:
mRecyclerView.setHasFixedSize(true);
LinearLayoutManager mLayoutManager = new LinearLayoutManager(getContext());
mRecyclerView.setLayoutManager(mLayoutManager);
Run Code Online (Sandbox Code Playgroud)
布局XML:
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipe_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/message_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:paddingBottom="@dimen/footer_progress_bar"
android:paddingTop="16dp"
android:scrollbars="vertical" />
<ProgressBar
android:id="@+id/progress_bar"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:visibility="gone" />
<ProgressBar
android:id="@+id/footer_progress_bar"
android:layout_width="@dimen/footer_progress_bar"
android:layout_height="@dimen/footer_progress_bar"
android:layout_gravity="center|bottom"
android:visibility="gone" />
</FrameLayout>
</android.support.v4.widget.SwipeRefreshLayout>
Run Code Online (Sandbox Code Playgroud)
在纵向模式下,此工作正常,并始终返回正确的位置.
但是在横向模式下,返回的位置始终为-1.
我的问题是:
有谁知道为什么会这样?
我怎样才能覆盖它以返回正确的位置?
或者任何人都可以推荐另一种解决方案来获得最后一个项目在景观中的正确位置?
android android-layout android-recyclerview linearlayoutmanager
我正在使用 RecyclerView,我希望将单击的项目滚动到 RecyclerView 的顶部。我尝试了 RecyclerView 中的平滑滚动方法,但没有一个按我的预期工作。
唯一有效的是 LinearLayoutManager 中的scrollToPositionWithOffset,但没有流畅的动画:项目直接跳转到顶部。
有没有办法让这个scrollToPositionWithOffset方法实现平滑的动画?
Tried all the related questions but it did not work :(
I am building endless scrolling with Recycler View.
PROBLEM: onScrolled method is always called without the user scrolling the screen.
Android Documentation Guide describes this for onScrolled method:
Case 1) Callback method to be invoked when the RecyclerView has been scrolled. This will be called after the scroll has completed.
Case 2) This callback will also be called if visible item range changes after a layout calculation. In that …
我见过许多类型的布局管理器,例如:
LineraLayoutManagerRecyclerView.LayoutManagerListViewLayoutManager实际LayoutManager是什么以及为什么使用它以及有哪些不同类型的LayoutManagers?做android系统中所有UI组件一样Button,TextView,EditText等都有自己的LayoutManagers?
android android-layout gridlayoutmanager linearlayoutmanager
在我的火电视应用我使用的是recyclerview用horizontal layout.
滚动与dpad工作和项目正在获得关注.
但是,当我按住按钮时,它会非常快速地滚动,因为许多keydown事件被触发,并且项目正在失去焦点而且不可能再滚动,因为Textview我的recyclerview上方的另一个正在获得焦点.
它看起来像一个bug.这有什么解决方法吗?
到目前为止我一直在尝试创建一个recyclerView,但现在我遇到了一个问题。
我需要让它看起来像这样
我需要将其设置为类似网格的列表,但是我无法将它们并排放置。
其次,如果最后一项是“单独的”,我需要最后一项来填充两个空格。
有任何想法吗?
android gridlayoutmanager android-recyclerview linearlayoutmanager
我正在尝试制作一个类似于 Instagram 过滤器布局工作方式的布局。基本上,当您选择一个过滤器时,它会滚动到您选择的项目 + 1,向您显示还有更多过滤器。
LinearLayoutManager我目前正在尝试为我的水平线构建自定义RecyclerView:
public class LinearLayoutSnapManager extends LinearLayoutManager {
private int mCurrentPos = 0;
public LinearLayoutSnapManager(Context context) {
super(context);
}
public LinearLayoutSnapManager(Context context, int orientation, boolean reverseLayout) {
super(context, orientation, reverseLayout);
}
public LinearLayoutSnapManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
public void snap(RecyclerView rv, int position) {
if (mCurrentPos == position) {
// No move
return;
}
boolean goingRight = true;
if (position < mCurrentPos) {
goingRight …Run Code Online (Sandbox Code Playgroud) android smooth-scrolling android-recyclerview linearlayoutmanager