Scroll bar on top side in horizontal RecyclerView

I am working on a simple demonstration of the Horizontal RecyclerView .

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

I can get the scroll bar, but it appears at the bottom. What I want to achieve is to show it from above. I found several such questions, but none of them are for horizontal scrollbar horizontal recyclerView +.

Here is the code I've 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 request

Thanks!

+6
source share
1 answer

I searched a couple of hours, but did not find anything according to your requirement. But there are some tricks or do with some hacked code that we can get in accordance with your requirements.

Install RecyclerView as shown 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 descending order in a List or ArrayList , because we need to rotate recyclerviews, so when we rotate it, our data will be displayed 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, in your adapter, please rotate your view in the opposite direction, as shown 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 make the code as above, your element and its output look like OutPut

Please do not forget to make such a code, because android is not responsible for this type of code, but according to your requirement you can do it.

+1
source

Source: https://habr.com/ru/post/1273866/


All Articles