Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scrollbar on Top Side in Horizontal RecyclerView

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

Image, that describes my query

Thanks!

like image 275
Ronak Thakkar Avatar asked Dec 05 '17 18:12

Ronak Thakkar


1 Answers

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.

like image 153
Shailesh Avatar answered Nov 10 '22 21:11

Shailesh