RecyclerView上方的隐藏视图

时间:2019-05-21 10:27:29

标签: android android-recyclerview recyclerview-layout

我想在recyclerview上方做一些隐藏的过滤器选项(例如像旧版本的Spotify):

enter image description here

该怎么做?我在回收站上方使用AppBar。

5 个答案:

答案 0 :(得分:2)

您可以按照上面答案中的说明进行操作(通过更改搜索视图的可见性,以便当可见性为View.GONE时,使用ContraintLayout可以使recycleView向上移动)。或将附加ViewHolder添加到您的适配器中(例如包含您的搜索视图的TopSearchViewHolder)。 像这样(在getItemViewType中,您将决定何时显示特定的viewType):

      override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val orderViewHolder = OrderViewHolder.create(parent).apply {
            listener = this@OrdersAdapter.listener
        }
        return when (viewType) {
            R.layout.list_item1 -> orderViewHolder
            R.layout.list_item2 -> NetworkStateViewHolder.create(parent, retryCallback)
            else -> throw IllegalAccessException("Unknown view type $viewType")
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        when (getItemViewType(position)) {
            R.layout.list_item1 -> (holder as OrderViewHolder).bind(getItem(position))
            R.layout.list_item2 -> (holder as NetworkStateViewHolder).bind(getItem(position))
        }
    }
   override fun getItemViewType(position: Int): Int {
        return if (hasExtraRow() && position == itemCount - 1) {
            R.layout.list_item1
        } else {
            R.layout.list_item2
        }
    }

答案 1 :(得分:1)

您可以使用此代码执行所需的操作

        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
                    @Override
                    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                        super.onScrollStateChanged(recyclerView, newState);

                    }

                    @Override
                    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                        super.onScrolled(recyclerView, dx, dy);
                        if (dy > 0) {
                         //You should HIDE filter view here
                        } else if (dy < 0) {
                            System.out.println("Scrolled Upwards");
                            if (mLayoutManager.findFirstCompletelyVisibleItemPosition() == 0) {
                                //this is the top of the RecyclerView
                               System.out.println("==================================== >>>> Detect top of the item List");
                             //You should visible filter view here
                            } else {
                              //You should HIDE filter view here
                             }
                        } else {
                            System.out.println("No Vertical Scrolled");
                            //You should HIDE filter view here
                        }
                    }
                });

答案 2 :(得分:1)

使用我在像这样的评论中提到的RecyclerView滚动侦听器

<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/White"
android:orientation="vertical"
android:layout_gravity="center"
xmlns:android="http://schemas.android.com/apk/res/android">

//use FilterView here
//hide this view initially.
   <Filter-view
    android:visibility="gone"
    android:id="@+id/filterView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    />

<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/moviesRecyclerView"
    android:layout_marginBottom="?attr/actionBarSize"/>

<TextView
    android:visibility="gone"
    android:layout_marginTop="@dimen/dimen_200dp"
    android:id="@+id/noMoviesMessage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:gravity="center"
    android:padding="@dimen/dimen_10dp"
    android:fontFamily="@font/noirden_bold"
    android:textColor="@color/Black"
    android:textSize="@dimen/text_18sp"
    android:text="@string/the_are_no_movies_showing_right_now_please_check_later"/>

</LinearLayout>

用于RecyclerView布局的XML

template<uint8_t Port, uint8_t Pin> class Gpio {};

尝试后让我知道

答案 3 :(得分:0)

您可以为此使用相对布局,并在其中添加包含搜索栏的线性布局,因此当您将可见性设置为搜索栏时,线性布局仍然存在,其中将包含以前的布局。

答案 4 :(得分:0)

我目前的解决方案:

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        int dy = 0;
        Handler handler = new Handler();

        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, final int newState) {
            super.onScrollStateChanged(recyclerView, newState);
            final boolean isOnTop = layoutManager.findFirstCompletelyVisibleItemPosition() == 0;
            if (isOnTop && newState == 1)
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (dy <= 0) {
                            KLog.d("show filter");
                        }
                    }
                }, 20);
            else if (newState == 1 && dy > 0)
                KLog.e("gone filter");

        }

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            this.dy = dy;
        }
    });

不是很优雅,仍然需要找到一种使隐藏视图动起来的方法。