Cómo agregar una barra de desplazamiento a Recyclerview
Tengo un Recyclerview en mi aplicación, pero no puedo agregar una barra de desplazamiento. He intentado todas las posibles soluciones en la web. La mayoría de las soluciones no son adecuadas para el sistema actual. En la solución en la que estuve más cerca, la barra se vuelve muy pequeña cuando se desplaza hacia abajo y no se puede tocar con un dedo. Así que necesito tu ayuda.
Aquí está mi archivo XML:
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout<em>width="match</em>parent"
android:layout<em>height="match</em>parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".WordListFragment"
android:animateLayoutChanges="true">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/word_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutManager="LinearLayoutManager" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/clear_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|bottom"
android:layout_margin="10dp"
android:src="@drawable/ic_delete_black_24dp"
app:fabSize="normal"
android:visibility="gone"
/>
<TextView
android:id="@+id/no_data"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/app_name"
android:textSize="20sp"
android:textColor="?colorPrimaryDark"
android:gravity="center"
android:layout_gravity="center_vertical"
android:visibility="gone"/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/scrollToTop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:fabSize="mini"
android:layout_margin="20dp"
android:src="@drawable/ic_keyboard_arrow_up_black_24dp"
app:layout_anchor="@id/word_list"
app:layout_anchorGravity="bottom|end"
android:visibility="gone"
/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Aquí está mi archivo Java:
“`
public class RecyclerAdapter extends RecyclerView.Adapter<recycleradapter.viewholder>{
private final Context context;
ArrayList<word> data_list;
private int[] theme_array;
ToggleButton selected_theme;
<pre><code>public RecyclerAdapter(Context context, ArrayList<word> data_list){
this.context = context;
this.data_list = data_list;
}
public RecyclerAdapter(Context context){
this.context = context;
theme_array=new int[]{R.style.AppTheme
,R.style.GreenTheme
,R.style.RedTheme
,R.style.PurpleTheme
,R.style.TealTheme
,R.style.GreenBlueTheme
,R.style.BlueGrayTheme
,R.style.PinkTheme
,R.style.DarkBlueTheme
,R.style.DarkYellowTheme
,R.style.BrownTheme
,R.style.DarkRedTheme};
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType){
int id=R.layout.word_list_item;
if (data_list==null)
id=R.layout.theme_chooser;
View v = LayoutInflater.from(context).inflate(id, parent, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, int position){
if (data_list==null){
if (theme_array[position]==MainActivity.preferences.getInt(context.getString(R.string.theme_pref),R.style.AppTheme)){
holder.color_btn.setChecked(true);
selected_theme=holder.color_btn;
}
TypedArray array=context.obtainStyledAttributes(theme_array[position],new int[]{R.attr.colorPrimary});
Drawable drawable= Objects.requireNonNull(ResourcesCompat.getDrawable(context.getResources(), R.drawable.color_state, null)).mutate();
int color=array.getColor(0, Color.WHITE);
drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
drawable=getDrawable(color,drawable);
holder.color_btn.setBackgroundDrawable(drawable);
return;
}
final Word tmp=data_list.get(position);
if (MainActivity.isSecond)
holder.title.setText(tmp.getMeaning());
else
holder.title.setText(tmp.getWord());
if (tmp.isUserAdded())
holder.itemView.findViewById(R.id.user_added).setVisibility(View.VISIBLE);
else
holder.itemView.findViewById(R.id.user_added).setVisibility(View.GONE);
((View)holder.title.getParent()).setOnClickListener(view -> context.startActivity(new Intent(context,DetailActivity.class)
.putExtra(DetailActivity.EXTRA_DETAILS,tmp)));
}
@Override
public int getItemCount(){
if (data_list==null)
return theme_array.length;
return data_list.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
TextView title;
ToggleButton color_btn;
public ViewHolder(View itemView){
super(itemView);
if (data_list!=null)
title=itemView.findViewById(R.id.list_title);
else {
color_btn = (ToggleButton) itemView;
color_btn.setOnClickListener(view -> {
if (color_btn.isChecked()){
if (selected_theme!=null)
selected_theme.setChecked(false);
selected_theme=color_btn;
MainActivity.preferences.edit().putInt(context.getString(R.string.theme_pref),theme_array[getAdapterPosition()]).apply();
((Activity)context).recreate();
}
});
}
}
}
Drawable getDrawable(int color,Drawable main_drawable){
Drawable drawable=ResourcesCompat.getDrawable(context.getResources(),R.drawable.ic_check_black_24dp,null);
StateListDrawable stateListDrawable=new StateListDrawable();
LayerDrawable layerDrawable=new LayerDrawable(new Drawable[]{main_drawable,drawable});
stateListDrawable.addState(new int[]{android.R.attr.state_checked},layerDrawable);
stateListDrawable.addState(new int[]{},main_drawable);
return stateListDrawable;
}
</code></pre>
}
“`</recycleradapter.viewholder>
davy.ai
Para agregar una barra de desplazamiento a un RecyclerView, es necesario utilizar un NestedScrollView con RecyclerView dentro de él. Aquí están los pasos para implementarlo:
nestedScrollingEnabled
de tu RecyclerView enfalse
:Con estos cambios, deberías poder agregar una barra de desplazamiento a tu RecyclerView.