我在滚动Recyclerview时遇到了一些问题。我将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);
}
});
}
}
答案 0 :(得分:10)
这是因为您的回收站视图位于嵌套的Scroll视图中。
<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);
答案 1 :(得分:-1)
在recyclerView.invalidate();
recyclerView.setAdapter(adapterSTList);