平滑滚动recyclerview [英] smooth scrolling for recyclerview

查看:92
本文介绍了平滑滚动recyclerview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在滚动recyclerview时遇到一些问题.我在LinearLayoutManager中添加了smoothScrollToPosition方法,但它没有任何改变.我尝试添加名为app:fastScrollEnabled的属性,但它导致了错误.我想要一个平滑的滚动.我该怎么办?预先谢谢你:)

I got some problems in scrolling the recyclerview. I added the smoothScrollToPosition method to LinearLayoutManager but it didn't change anything.I tried to added the attribute named app:fastScrollEnabled but it caused error. I want a smooth scrolling. What should i do? Thank you in advance :)

XML代码:

  <?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.Toolbar
        android:id="@+id/stu_toolbar"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#a653a3"
        app:layout_scrollFlags="scroll|enterAlways">

        <ImageView
            android:id="@+id/check_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:layout_margin="@dimen/normal_margin"
            android:src="@drawable/ic_done_white_24dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:layout_marginRight="@dimen/normal_margin"
            android:text="دانش آموزان"
            android:textSize="20sp" />


    </android.support.v7.widget.Toolbar>

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collpase_view_pager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_scrollFlags="scroll|enterAlways">

        <android.support.v4.view.ViewPager
            android:id="@+id/slide_show"
            android:layout_width="match_parent"
            android:layout_height="150dp" />

    </android.support.design.widget.CollapsingToolbarLayout>

</android.support.design.widget.AppBarLayout>


<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycle2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</android.support.v4.widget.NestedScrollView>

</android.support.design.widget.CoordinatorLayout>

Java代码:

public class Student extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.student_list);


    RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycle2);
    LinearLayoutManager layoutManager = new LinearLayoutManager(this) {

        @Override
        public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
            LinearSmoothScroller smoothScroller = new LinearSmoothScroller(Student.this) {

                private static final float SPEED = 300f;

                @Override
                protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
                    return SPEED / displayMetrics.densityDpi;
                }

            };
            smoothScroller.setTargetPosition(position);
            startSmoothScroll(smoothScroller);
        }

    };
    recyclerView.setLayoutManager(layoutManager);
    AdapterSTList adapterSTList = new AdapterSTList(this, Generator.getStudents());
    recyclerView.setAdapter(adapterSTList);
    ViewPager viewPager = (ViewPager) findViewById(R.id.slide_show);
    ImageView check_btn = (ImageView) findViewById(R.id.check_button);
    ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(this);
    viewPager.setAdapter(viewPagerAdapter);

    check_btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(Student.this,Main.class);
            startActivity(intent);
        }
    });
}
}

推荐答案

这是因为您的回收站视图位于嵌套的Scroll视图内.

Its because your recycler view is inside a nested Scroll view.

<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior">

<android.support.v7.widget.RecyclerView
    android:id="@+id/recycle2"
    android:nestedScrollingEnabled="false"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

在您的代码中也这样做以支持较旧的设备:

also in your code do this to support older devices :

 recyclerView.setNestedScrollingEnabled(false);

这篇关于平滑滚动recyclerview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆