6

我正在研究Horizo​​ntal RecyclerView的简单演示。

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

我能够获得滚动条,但它显示在底部。我想要实现的是将它显示在顶部。我发现了一些这样的问题,但没有一个是针对水平 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" />

图片,描述了我的查询

谢谢!

4

1 回答 1

1

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

在 xml 文件中如下设置RecyclerView 。

<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"/>

将您的数据以相反的顺序放在您的ListArrayList中,因为我们需要旋转 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

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

于 2017-12-06T13:42:00.760 回答