Scrollbar on Top Side in Horizontal RecyclerView

最后都变了- 提交于 2019-12-08 20:41:59

问题


I am working on the Simple demo of Horizontal RecyclerView.

I want to display scrollbar along with the recyclerview. So I have added android:scrollbars="horizontal" and android:scrollbarSize="5dp" in XML.

I am able to get the Scrollbar but it is showing on the bottom. What I want to achieve is to show it on Top. I have found some questions like this but none of them are for horizontal recyclerView + top side scrollbar.

Here is the code which I have tried so far:

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


回答1:


I have searched couple of hours but not found anything as per your requirement. But there are some trikes or do with some hacky code we can get output as per your requirement.

Set your RecyclerView as below in your xml file.

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

Put your data in revers order in your List or ArrayList because we need to rotate the recyclerviews so when we rotate it then our data will be display as an ASEC order.

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

Now last, In your adapter please rotate your all view in revers direction like below.

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

If you do code as above then your item and its output look like this OutPut

Please make sure to doing this kind of code because android will not responsible for this kind of code, but as per your requirement you can do like this.



来源:https://stackoverflow.com/questions/47660089/scrollbar-on-top-side-in-horizontal-recyclerview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!