RecyclerView的快速滚动:RecyclerView-FastScroll
jopen
9年前
RecyclerView的快速滚动,支持使用了LinearLayoutManager和GridLayoutManager的垂直RecyclerViews。
使用说明:
样式大致基于最新Lollipop版本的ListView FastScroller。大量借鉴了 Google的Launcher3 FastScroller。
你必须把FastScrollRecyclerView作为你的base RecyclerView。
如果你的RecyclerView Adapter实现了SectionIndexer,FastScrollPopup将显示从那个实现返回的text。
/* * Copyright (c) 2016 Tim Malseed * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.simplecity.recyclerview_fastscroll.sample.activity; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.View; import android.view.ViewGroup; import android.widget.SectionIndexer; import android.widget.TextView; import com.simplecity.recyclerview_fastscroll.sample.R; import com.simplecity.recyclerview_fastscroll.views.FastScrollRecyclerView; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); FastScrollRecyclerView recyclerView = (FastScrollRecyclerView) findViewById(R.id.recycler); recyclerView.setLayoutManager(new LinearLayoutManager(this)); recyclerView.setAdapter(new RecyclerAdapter()); } private class RecyclerAdapter extends RecyclerView.Adapter implements SectionIndexer { @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { TextView textView = new TextView(MainActivity.this); textView.setPadding(100, 100, 100, 100); return new ViewHolder(textView); } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { ((TextView) holder.itemView).setText(String.format("Item %d", position)); } @Override public int getItemCount() { return 20; } @Override public Object[] getSections() { Object[] objects = new Object[getItemCount()]; for (int i = 0, length = getItemCount(); i < length; i++) { objects[i] = i; } return objects; } @Override public int getPositionForSection(int sectionIndex) { return sectionIndex; } @Override public int getSectionForPosition(int position) { return position; } } private class ViewHolder extends RecyclerView.ViewHolder { public ViewHolder(View itemView) { super(itemView); } } }