Recyclerview с помощью StaggeredGridLayoutManager не закрывает пробелы внизу

#android #android-recyclerview #staggered-gridview #staggeredgridlayout #staggeredgridlayoutmanager

#Android #android-recyclerview #в шахматном порядке -gridview #staggeredgridlayout #staggeredgridlayoutmanager

Вопрос:

Я реализовал простой recyclerview с помощью StaggeredGridLayoutManager следующим образом:

activity_main.xml

 <androidx.constraintlayout.widget.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<androidx.recyclerview.widget.RecyclerView
    android:id="@ id/simple_recyclerview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="5dp"/>

</androidx.constraintlayout.widget.ConstraintLayout>
 

recyclerview_layout.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp">

<TextView
    android:id="@ id/row_tv"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="#25000000"/>

</LinearLayout>
 

MyRecyclerViewAdapter.java

 public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> {
   private List<String> mData;
   private LayoutInflater mInflater;

   public MyRecyclerViewAdapter(Context context, List<String> data) {
       this.mInflater = LayoutInflater.from(context);
       this.mData = data;
   }

   @Override
   public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
       View view = mInflater.inflate(R.layout.recyclerview_row, parent, false);
       return new ViewHolder(view);
   }

   @Override
   public void onBindViewHolder(ViewHolder holder, int position) {
       String txt = mData.get(position);
       holder.myTextView.setText(Html.fromHtml(txt));
   }

   @Override
   public int getItemCount() {
       return mData.size();
   }

   public class ViewHolder extends RecyclerView.ViewHolder {
       TextView myTextView;

       ViewHolder(View itemView) {
           super(itemView);
           myTextView = itemView.findViewById(R.id.row_tv);
       }
   }

   String getItem(int id) {
       return mData.get(id);
   }
}
 

MainActivity.kt

 class MainActivity : AppCompatActivity() {
   override fun onCreate(savedInstanceState: Bundle?) {
       super.onCreate(savedInstanceState)
       setContentView(R.layout.activity_main)

       val data = arrayListOf<String>()

       data.add("<b>1. </b>This is a text. This is a text. This is a text. This is a text. This is a text. This is a text. This is a text. This is a text. This is a text. This is a text. This is a text. This is a text. This is a text. This is a text. This is a text. This is a text. This is a text. This is a text. ")
       data.add("<b>2. </b>This is a text. This is a text. This is a text. This is a text. This is a text. This is a text. This is a text. ")
       data.add("<b>3. </b>This is a text. This is a text. This is a text. This is a text. This is a text. This is a text. This is a text. ")
       data.add("<b>4. </b>This is a text. This is a text. This is a text. This is a text. This is a text. This is a text. This is a text. ")
       data.add("<b>5. </b>This is a text. This is a text. This is a text. This is a text. This is a text. This is a text. This is a text. ")
       data.add("<b>6. </b>This is a text. This is a text. This is a text. This is a text. This is a text. This is a text. This is a text. This is a text. This is a text. This is a text. This is a text. This is a text. This is a text. This is a text. This is a text. This is a text. This is a text. This is a text. ")

       val recyclerView = findViewById<RecyclerView>(R.id.simple_recyclerview)
       recyclerView.layoutManager = getLayoutManager()
       val adapter = MyRecyclerViewAdapter(this, data)
       recyclerView.adapter = adapter
   }

   fun getLayoutManager(): RecyclerView.LayoutManager {
       val sglm = StaggeredGridLayoutManager(2, 1)
       sglm.gapStrategy = StaggeredGridLayoutManager.GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS
       return sglm
   }
}
 

Текст в списке данных имеет разные размеры, поэтому, чтобы закрыть пробелы, я установил стратегию распределенных пробелов как GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS. Но в настоящее время вывод:
введите описание изображения здесь

Но я хочу закрыть пробелы внизу, переставив элементы, как показано ниже, и я не знаю, как заставить StaggeredGridLayoutManager сделать это за меня: введите описание изображения здесь