Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RecyclerView scrollbar jumps back to top after first item

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?

like image 948
Kuno Avatar asked Nov 08 '14 23:11

Kuno


People also ask

How do I stop refreshing RecyclerView data scroll to top position android?

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?

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.

Does RecyclerView scroll?

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 .


1 Answers

As stated here, this bug has been fixed in the new version of the support library v21.0.2.

like image 106
Kuno Avatar answered Nov 11 '22 03:11

Kuno