如何顺利滚动到单击的项目到RecyclerView的顶部?

时间:2015-12-29 22:59:39

标签: android android-recyclerview

我有一个项目列表作为RecyclerView(可扩展项目),我希望列表中的项目在单击时可以平滑移动/滚动。

因此,假设我在第一个屏幕上有如下列表,然后点击项目"值6"。我希望通过平滑滚动项目"值6"来实现第二屏幕上的情况。顶部。

enter image description here enter image description here

我的布局:

    <?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>

我的实际适配器(它是ScrollingActivity的内部类,适用于Google示例)

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

所以我想念的是什么? 我想recyclerView.smoothScrollToPosition(pos)什么都不做,因为这个项目在屏幕上是实际的。但是如何解决呢?

1 个答案:

答案 0 :(得分:2)

我尝试使用Move/scroll clicked listitem view at top of screen in the expandablelistview为我的Expandable RecyclerView编辑答案,但它确实有效。

onLayoutExpanded()只是在展开视图时调用的抽象方法。

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