1

我已经用 StaggeredGridLayoutManager 实现了一个简单的 recyclerview,如下所示:

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 为我做到这一点: 在此处输入图像描述

4

0 回答 0