水平 RecyclerView 顶部的滚动条

2024-05-24

我正在做简单的演示卧式RecyclerView.

我想与回收视图一起显示滚动条。所以我添加了android:scrollbars="horizontal" and android:scrollbarSize="5dp"在 XML 中。

我可以获得滚动条,但它显示在底部。我想要实现的是将其显示在顶部。我发现了一些这样的问题,但没有一个是针对水平recyclerView + 顶部滚动条。

这是我迄今为止尝试过的代码:

<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:isScrollContainer="false"
    android:orientation="horizontal"
    android:scrollbars="horizontal"
    android:scrollbarSize="5dp"
    android:visibility="visible" />

Thanks!


我已经搜索了几个小时,但没有找到符合您要求的任何内容。但是有一些三轮车或一些黑客代码,我们可以根据您的要求获得输出。

设置你的回收视图 https://developer.android.com/guide/topics/ui/layout/recyclerview.html如下所示在您的 xml 文件中。

<android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbarAlwaysDrawHorizontalTrack="true"
        android:scrollbarSize="10dp"
        android:scrollbarStyle="outsideInset"
        android:scrollbarThumbVertical="@color/black"
        android:scrollbars="horizontal"
        android:verticalScrollbarPosition="right"/>

将您的数据以相反的顺序放入List https://developer.android.com/reference/java/util/List.html or 数组列表 https://developer.android.com/reference/java/util/ArrayList.html因为我们需要旋转 recyclerviews,所以当我们旋转它时,我们的数据将显示为 ASEC 顺序。

   //call this method for set recyclerview
   private void setRecyclerView()
    {
        //Please make sure with your item that it will be inserted in revers order then an then it will be working
        ArrayList<String> itemList = new ArrayList<>();
        for (int i = 50; i > 0; i--){
            itemList.add("item " + i);
        }

        ContainerAdapter adapterMessage = new ContainerAdapter(MainActivity.this, itemList);
        if (adapterMessage != null)
        {
            rvItemList.setHasFixedSize(true);
            rvItemList.setLayoutManager(new LinearLayoutManager(MainActivity.this,
                    LinearLayoutManager.HORIZONTAL, false);
            rvItemList.setItemAnimator(new DefaultItemAnimator());
            rvItemList.setAdapter(adapterMessage);
            //here is the main line for your requirement
            rvItemList.setRotation(180);
            adapterMessage.notifyDataSetChanged();
        }

最后,在您的适配器中,请按如下所示的相反方向旋转您的所有视图。

public class ViewHolder extends RecyclerView.ViewHolder
    {
        TextView txt_name;
        public ViewHolder(View itemView) {
            super(itemView);
            txt_name = (TextView) itemView.findViewById(R.id.txt_name);
            // here you need to revers rotate your view because your recyclerview is already rotate it so your view is also rotated and you need to revers rotate that view
            txt_name.setRotation(-180);
        }
    }

如果您按照上面的方式进行编码,那么您的项目及其输出如下所示OutPut https://i.stack.imgur.com/q3mpi.jpg

请确保执行此类代码,因为 android 不会对此类代码负责,但根据您的要求,您可以这样做。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

水平 RecyclerView 顶部的滚动条 的相关文章

随机推荐