How to create material design like custom scrollbar with numbers and alphabets bubble in recylerview

戏子无情 提交于 2019-12-03 03:06:36

问题


In many new android applications and their latest update those applications(mostly material design) have a custom scrollbar with letters and numbers, while scrolling the scrollbar with thumb, alphabets or numbers appear beside thumb.I have attached screenshot to the question of the scrollbar from the application 'Contacts'.

Screenshot:

So, How to modify a scrollbar in my application which is using recyclerview, to create scrollbar like that scrollbar with the alphabet and number bubble or is there any new API or library introduced for that?


回答1:


For anyone still looking for an answer for this. I have found a number of libraries which provide this functionality:

  1. https://github.com/krimin-killr21/MaterialScrollBar
  2. https://github.com/plusCubed/recycler-fast-scroll
  3. https://github.com/danoz73/RecyclerViewFastScroller
  4. https://github.com/timusus/RecyclerView-FastScroll

All of the above provide FastScroll mechanism (what you're looking for) - though some look nicer than others.

I have found the MaterialScrollBar easiest to get set with up and use, as well as the fact that it has the nicest cosmetics (adheres to material design guidelines and looks just like Contacts app).

This library is also actively maintained and developed, issues & PR's being closed etc.

Hope this may help someone as I spent a lot of time looking for this myself.




回答2:


As I can understand from your question, you want to implement the "Fast Scroll" feature in Android.

This feature lets you quickly scroll through a large list. You can easily implement it using this third-party library.

Its very simple, light-weight and is highly customization too. Don't underestimate it by looking at the screenshot above. You can easily make it look like the screenshot you provided. I used it sometime back in one of my apps and got excellent results.

Usage

Using this library is also simple. You just need to include its QuickScroll layout and implement the Scrollable interface on your BaseAdapter. Giving this widget the Material look-and-feel is really simple.

Update

Due to user demand, for all those who want to know how to use "Fast Scroll" feature in RecyclerView, here it is for you all. You can use this library.

It can give you results which is very close (if not exact) to the screenshot provided. You just have to use RecyclerViewFastScroller widget in your layout.

Usage

Step 1

Add this dependency

compile 'xyz.danoz:recyclerviewfastscroller:0.1.3'

Step 2

Now in your XML you need to put this widget

<android.support.v7.widget.RecyclerView
      android:id="@+id/recyclerView"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      />

  <xyz.danoz.recyclerviewfastscroller.vertical.VerticalRecyclerViewFastScroller
      android:id="@+id/fast_scroller"
      android:layout_width="@dimen/however_wide_you_want_this"
      android:layout_height="match_parent"
      android:layout_alignParentRight="true"
      />

and then in your code,

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
      View rootView = inflater.inflate(R.layout.recycler_view_frag, container, false);
      ...

      // Grab your RecyclerView and the RecyclerViewFastScroller from the layout
      RecyclerView recyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerView);
      VerticalRecyclerViewFastScroller fastScroller = (VerticalRecyclerViewFastScroller) rootView.findViewById(R.id.fast_scroller);

      // Connect the recycler to the scroller (to let the scroller scroll the list)
      fastScroller.setRecyclerView(recyclerView);

      // Connect the scroller to the recycler (to let the recycler scroll the scroller's handle)
      recyclerView.setOnScrollListener(fastScroller.getOnScrollListener());

      ...
      return rootView;
  }

There are lots of additional attributes to customize it the way you want to. You can create exactly the same look-and-feel of the screenshot you provided.

Using a library can save a lot of time, but in case if you want to build this feature from scratch, here is a very useful post for you.

Hope it helps.




回答3:


List View Variants library seems to be perfect for your needs, try it out.

Check out the demo below.




回答4:


I use this new one now - https://github.com/L4Digital/FastScroll

Below is my previous one.

I have used fastscroller by FutureMind in Android Studio

https://github.com/FutureMind/recycler-fast-scroll

Usage

1) Compile required dependency by adding

compile 'com.futuremind.recyclerfastscroll:fastscroll:0.2.4'

2) Edit Layout File to add FastScroller

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <com.futuremind.recyclerviewfastscroll.FastScroller
        android:id="@+id/fastscroll"
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentRight="true"/>
</RelativeLayout>

3) In Activity/Fragment associate fastScroller with your recycler view

    recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
    fastScroller = (FastScroller) findViewById(R.id.fastscroll);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    recyclerView.setAdapter(adapter);

    //has to be called AFTER RecyclerView.setAdapter()
    fastScroller.setRecyclerView(recyclerView);

4) In your RecyclerView.Adapter implement SectionTitleProvider to display the content on Bubble

    public class MyAdapter ... implements SectionTitleProvider{

        ...

        @Override
        public String getSectionTitle(int position) {
            //this String will be shown in a bubble for specified position
            return getItem(position).substring(0, 1);
        }

    }


来源:https://stackoverflow.com/questions/31261437/how-to-create-material-design-like-custom-scrollbar-with-numbers-and-alphabets-b

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