📜  如何使用Recycle-Fast-Scroll在RecyclerView中添加Fast Scroller?

📅  最后修改于: 2021-05-08 20:29:35             🧑  作者: Mango

在本文中,我们将使用recycle-fast-scroll库实现快速滚动。对于该项目,在RecycleView中,我们仅实现一个按钮,按钮的总数将由我们在MainActivity中设置。按钮的名称将是按钮的位置。下面给出了一个示例GIF,以了解我们将在本文中做些什么。注意,我们将使用Java语言实现该项目。

分步实施

步骤1:创建一个新项目

要在Android Studio中创建新项目,请参阅如何在Android Studio中创建/启动新项目。请注意,选择Java作为编程语言。

步骤2:将其添加到build.gradle:(app)文件中

implementation 'androidx.recyclerview:recyclerview:1.1.0'
implementation 'com.github.plusCubed:recycler-fast-scroll:0.3.2'
implementation 'com.google.android.material:material:1.0.0'

将此添加到build.gradle :(项目)文件中

allprojects {
    repositories {
        google()
        jcenter()
        maven { url 'https://jitpack.io' }
    }
}

步骤3:使用items.xml文件

转到应用程序> res>布局>新建>布局资源文件,并将文件命名为item 。转到item.xml文件,并参考以下代码。以下是item.xml文件的代码。

XML


 
    


Java
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
 
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
 
public class RecyclerViewAdapter extends RecyclerView.Adapter {
 
    private int mcount;
 
    public RecyclerViewAdapter(int mcount) {
        this.mcount = mcount;
    }
 
    @NonNull
    @Override
    public RecyclerViewAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.items, parent, false);
        return new ViewHolder(view);
    }
 
    @Override
    public void onBindViewHolder(@NonNull RecyclerViewAdapter.ViewHolder holder, int position) {
        // Adding the items in recycler view with name "Button" + position
        holder.itemclick.setText("Button" + position);
    }
 
    @Override
    public int getItemCount() {
        return mcount;
    }
 
    public class ViewHolder extends RecyclerView.ViewHolder {
        Button itemclick;
 
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            itemclick = itemView.findViewById(R.id.itemclick);
        }
    }
}


XML


 
    
 
    
     


Java
import android.os.Bundle;
 
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
 
import com.pluscubed.recyclerfastscroll.RecyclerFastScroller;
 
public class MainActivity extends AppCompatActivity {
 
    RecyclerView recyclerView;
    RecyclerFastScroller fastScroller;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         
        // initialising the recycler view
        recyclerView = findViewById(R.id.rcv);
        recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
         
        // Initialising the size of recycler as 50
        recyclerView.setAdapter(new RecyclerViewAdapter(50));
        fastScroller = findViewById(R.id.fasttrcv);
        fastScroller.attachRecyclerView(recyclerView);
    }
}


步骤4:使用RecyclerViewAdapter。 Java文件

在android studio中创建一个新的Java类,并将该类命名为RecyclerViewAdapter。转到RecyclerViewAdapter。 Java文件并参考以下代码。下面是RecyclerViewAdapter的代码。 Java文件。

Java

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
 
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
 
public class RecyclerViewAdapter extends RecyclerView.Adapter {
 
    private int mcount;
 
    public RecyclerViewAdapter(int mcount) {
        this.mcount = mcount;
    }
 
    @NonNull
    @Override
    public RecyclerViewAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.items, parent, false);
        return new ViewHolder(view);
    }
 
    @Override
    public void onBindViewHolder(@NonNull RecyclerViewAdapter.ViewHolder holder, int position) {
        // Adding the items in recycler view with name "Button" + position
        holder.itemclick.setText("Button" + position);
    }
 
    @Override
    public int getItemCount() {
        return mcount;
    }
 
    public class ViewHolder extends RecyclerView.ViewHolder {
        Button itemclick;
 
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            itemclick = itemView.findViewById(R.id.itemclick);
        }
    }
}

步骤5:使用activity_main.xml文件

导航到应用程序> res>布局> activity_main.xml,然后将以下代码添加到该文件中。以下是activity_main.xml文件的代码。

XML格式



 
    
 
    
     

步骤6:使用MainActivity。 Java文件

转到MainActivity。 Java文件并参考以下代码。下面是MainActivity的代码。 Java文件。在代码内部添加了注释,以更详细地了解代码。

Java

import android.os.Bundle;
 
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
 
import com.pluscubed.recyclerfastscroll.RecyclerFastScroller;
 
public class MainActivity extends AppCompatActivity {
 
    RecyclerView recyclerView;
    RecyclerFastScroller fastScroller;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         
        // initialising the recycler view
        recyclerView = findViewById(R.id.rcv);
        recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
         
        // Initialising the size of recycler as 50
        recyclerView.setAdapter(new RecyclerViewAdapter(50));
        fastScroller = findViewById(R.id.fasttrcv);
        fastScroller.attachRecyclerView(recyclerView);
    }
}

输出: