简体   繁体   中英

How to smoothly scroll to clicked item to top of RecyclerView?

I have a list of item as RecyclerView (expandable item) and I want to the item on list was moved/scrolled smoothly when clicked.

So let say that I have list as below on first screen and I click item "value 6". I want to achieve situation on second screen by smoothly scroll item "value 6" to top.

在此处输入图片说明 在此处输入图片说明

My Layout:

    <?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout >

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/toolbar_layout"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                app:layout_collapseMode="pin" />

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

    <android.support.v7.widget.RecyclerView 
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        />
</android.support.design.widget.CoordinatorLayout>

And my actual Adapter (it's inner class in ScrollingActivity which is for Google sample)

   public class Adapter extends RecyclerView.Adapter<Adapter.ItemHolder> {

        Container[] values;

        public Adapter() {
            values = new Container[100];
            for (int i = 0; i < values.length; i++) {
                values[i] = new Container();
                values[i].text = "value " + i;
            }

        }

        @Override
        public ItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new ItemHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.row, parent, false));
        }

        @Override
        public void onBindViewHolder(ItemHolder holder, int position) {
            holder.text.setText(values[position].text);
            holder.more.setVisibility(values[position].visibility);
        }

        @Override
        public int getItemCount() {
            return values.length;
        }

        public class ItemHolder extends RecyclerView.ViewHolder {

            private final TextView more; //it is expandable part
            TextView text;                

            public ItemHolder(View itemView) {
                super(itemView);
                text = (TextView) itemView.findViewById(R.id.text);
                more = (TextView) itemView.findViewById(R.id.more);

                text.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        int pos = getAdapterPosition();
                        if (more.getVisibility() == View.VISIBLE) {
                            more.setVisibility(View.GONE);
                            values[pos].visibility = View.GONE;
                        } else {
                            more.setVisibility(View.VISIBLE);
                            values[pos].visibility = View.VISIBLE;

                            int vfpos = linearLayoutManager.findFirstCompletelyVisibleItemPosition();
                            int d = pos - vfpos;
                            recyclerView.smoothScrollToPosition(pos);
                        }
                    }
                });
            }
         }
    }

So what I miss? I suppose that recyclerView.smoothScrollToPosition(pos) do nothing because this item is actual on screen. But how to fix it?

I tried editing the answer from Move/scroll clicked listitem view at top of screen in the expandablelistview for my Expandable RecyclerView, and it works.

onLayoutExpanded() is just an abstract method that is called when the view is expanded.

recyclerAdapter = new RecyclerAdapter(this, recyclerData) {
    @Override
    public void onLayoutExpanded(int position) {
        int offset = position - recyclerLayoutManager.findFirstVisibleItemPosition();
        if (recyclerLayoutManager.findFirstVisibleItemPosition() > 0) offset -= 1;
        recyclerLayoutManager.scrollToPositionWithOffset(position, offset);
    }
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM