I created a very basic RecyclerView example.
Layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MyActivity">
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"/>
</RelativeLayout>
Activity:
public class MyActivity extends Activity {
RecyclerView mRecyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
mRecyclerView.setAdapter(new TestAdapter());
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
}
public static class TestAdapter extends RecyclerView.Adapter<TestAdapter.ViewHolder> {
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(android.R.layout.simple_list_item_1, viewGroup, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
viewHolder.tv.setText("Row " + (i + 1));
}
@Override
public int getItemCount() {
return 30;
}
public static class ViewHolder extends RecyclerView.ViewHolder {
TextView tv;
public ViewHolder(View itemView) {
super(itemView);
tv = (TextView) itemView.findViewById(android.R.id.text1);
}
}
}
}
Now when I scroll beyond the first item, the scrollbar jumps back to the top and then continues normally. The second problem is when I scroll to the bottom, the scrollbar stops to early. Is this a bug in the support lib or my own fault?
Make a setDataList method in your adapter class. And set your updated list to adapter list. And then every time of calling API set that list to setDataList and call adapter. notifyDataSetChanged() method of your adapter class.
Does RecyclerView automatically scroll? There is a method in RecyclerView which is smoothScrollToPosition() which take position of recyclerview to scroll that position to current position.
To be able to scroll through a vertical list of items that is longer than the screen, you need to add a vertical scrollbar. Inside RecyclerView , add an android:scrollbars attribute set to vertical .
As stated here, this bug has been fixed in the new version of the support library v21.0.2.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With