RecyclerView 的平滑滚动

2024-02-22

我在滚动回收器视图时遇到一些问题。我添加了smoothScrollToPosition方法LinearLayoutManager但它没有改变任何东西。我尝试添加名为的属性app:fastScrollEnabled但它导致了错误。我想要平滑的滚动。我应该怎么办?先感谢您 :)

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);
        }
    });
}
}

这是因为您的回收器视图位于嵌套的滚动视图内。

<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" />

还在您的代码中执行此操作以支持旧设备:

 recyclerView.setNestedScrollingEnabled(false);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

RecyclerView 的平滑滚动 的相关文章

随机推荐