Android GridLayout 中的不同高度

Different heights in an Android GridLayout

我正在开发一个类似于 Google 保持其外观的笔记应用程序。但是,我无法弄清楚如何根据文本长度动态更改便条卡的高度。这是我的应用程序的屏幕截图:

app screenshot

这是卡片的布局文件:

notes_card.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:card_view="http://schemas.android.com/tools"
    android:id="@+id/notes_card"
    android:layout_width="match_parent"
    android:layout_gravity="center"
    app:cardElevation="3dp"
    android:layout_margin="4dp"
    app:cardUseCompatPadding="true"
    android:layout_height="wrap_content"
    card_view:cardMaxElevation="10dp">
    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:layout_height="match_parent">
        <TextView
            android:id="@+id/note_text1"
            android:minLines="3"
            android:maxLines="15"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>
</androidx.cardview.widget.CardView>

笔记片段的布局:

fragment_notes_view.xml

<androidx.recyclerview.widget.RecyclerView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/notes_recyclerview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:scrollbars="vertical"
    />

片段 onViewCreated 函数:

    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        //Initialize the RecyclerView and push it onto the fragment
        RecyclerView recyclerView = view.findViewById(R.id.notes_recyclerview);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(NotesView.this.getContext(), 2);
        recyclerView.setLayoutManager(gridLayoutManager);
        NotesRecyclerAdapter adapter = new NotesRecyclerAdapter(this.getContext());
        recyclerView.setAdapter(adapter);
        super.onViewCreated(view, savedInstanceState);
    }

RecyclerViewAdapter 代码:

package xyz.incepted.thereminder.utils;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

import xyz.incepted.thereminder.R;
import xyz.incepted.thereminder.types.QuickNote;

public class NotesRecyclerAdapter extends RecyclerView.Adapter <NoteViewHolder> {
    //Initialize class variables
    private Context context;
    private ArrayList<QuickNote> notes;
    private NotesConnector notesConnector = new NotesConnector();

    /**
     * Creates a RecylerViewAdapter based on the context, also fetches the notes from the NotesConnector
     * @param context context to be based on
     */
    public NotesRecyclerAdapter(Context context){
        this.context = context;
        this.notes = notesConnector.getNotes();
    }

    /**
     * Executed when the RecylerView is created
     * @param parent parent of the view
     * @param viewType type of the view
     * @return
     */
    @NonNull
    @Override
    public NoteViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //Inflate the view and return it
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.notes_card, parent, false);
        return new NoteViewHolder(view);
    }

    /**
     * Sets the view properties
     * @param holder holder
     * @param position which text to be used
     */
    @Override
    public void onBindViewHolder(@NonNull NoteViewHolder holder, int position) {
        holder.text1.setText(notes.get(position).getText1());
    }

    /**
     * Returns the item size of the list
     * @return item size
     */
    @Override
    public int getItemCount() {
        return notes.size();
    }
}

/**
 * Class to hold NoteView
 * //TODO divide it into two, since QuickNote and Note will have different designs
 */
class NoteViewHolder extends RecyclerView.ViewHolder{
    TextView text1;
    NoteViewHolder(View view){
        super(view);
        text1 = view.findViewById(R.id.note_text1);
    }
}

如何根据文本长度动态更改便签卡的高度 - 您可以像这样将布局高度设置为 wrap_content :

android:layout_height="wrap_content"

您可以像这样将您的项目放在 scrollView 中:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent">


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <TextView
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:text="short text !!!!!!!!!!!!?!@#!@%$%$#"
        android:layout_marginBottom="8dp"/>


    <TextView
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:text="Longggggggggggggggggggggggggggggggggggggggggggggggg  text very long"/>
</LinearLayout>

</ScrollView>

它看起来像这样:

蓝线表示您的 2 个视图之间的分隔。

如果您想使用 RecyclerView 实现此目的,请查看 this post, Staggered RecyclerView and Heterogeneous GridLayout