Java Android网格布局中的不同高度

Java Android网格布局中的不同高度,java,android,android-recyclerview,android-gridlayout,Java,Android,Android Recyclerview,Android Gridlayout,我正在努力创建一个Notes应用程序,它的外观类似于Google Keep。然而,我不知道如何根据笔记卡的文本长度动态地改变其高度。以下是我的应用程序的屏幕截图: 以下是卡的布局文件: notes\u card.xml notes片段的布局: fragment\u notes\u view.xml 片段onViewCreated函数: public void onViewCreated(@NonNull视图,@Nullable Bundle savedInstanceState){ /

我正在努力创建一个Notes应用程序,它的外观类似于Google Keep。然而,我不知道如何根据笔记卡的文本长度动态地改变其高度。以下是我的应用程序的屏幕截图:

以下是卡的布局文件:

notes\u card.xml


notes片段的布局:

fragment\u notes\u view.xml


片段onViewCreated函数:

public void onViewCreated(@NonNull视图,@Nullable Bundle savedInstanceState){
//初始化RecyclerView并将其推送到片段上
RecycleView RecycleView=view.findViewById(R.id.notes\u RecycleView);
GridLayoutManager GridLayoutManager=新的GridLayoutManager(NotesView.this.getContext(),2);
setLayoutManager(gridLayoutManager);
NotesRecyclerAdapter=新的NotesRecyclerAdapter(this.getContext());
recyclerView.setAdapter(适配器);
super.onViewCreated(视图,savedInstanceState);
}
RecycleServiceAdapter代码:

包xyz.incepted.thereminder.utils;
导入android.content.Context;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.TextView;
导入androidx.annotation.NonNull;
导入androidx.recyclerview.widget.recyclerview;
导入java.util.ArrayList;
输入xyz.incepted.thereminder.R;
导入xyz.incepted.thereminder.types.QuickNote;
公共类NotesRecyclerAdapter扩展了RecyclerView.Adapter{
//初始化类变量
私人语境;
私人数组列表注释;
private NotesConnector NotesConnector=new NotesConnector();
/**
*基于上下文创建RecylerViewAdapter,并从NotesConnector获取注释
*@param上下文将基于
*/
public NotesRecyclerAdapter(上下文){
this.context=上下文;
this.notes=notesConnector.getNotes();
}
/**
*在创建RecylerView时执行
*@param视图的父级
*@param viewType视图的类型
*@返回
*/
@非空
@凌驾
public NoteViewHolder onCreateViewHolder(@NonNull ViewGroup父级,int viewType){
//将视图充气并返回
View=LayoutFlater.from(parent.getContext()).flate(R.layout.notes\u card,parent,false);
返回新的NoteViewHolder(视图);
}
/**
*设置视图属性
*@param-holder
*@param要使用的文本的位置
*/
@凌驾
public void onBindViewHolder(@NonNull NoteViewHolder,int位置){
holder.text1.setText(notes.get(position.getText1());
}
/**
*返回列表的项目大小
*@返回项目大小
*/
@凌驾
public int getItemCount(){
returnnotes.size();
}
}
/**
*类来保存NoteView
*//TODO将其分为两部分,因为QuickNote和Note具有不同的设计
*/
类NoteViewHolder扩展了RecyclerView.ViewHolder{
TextView text1;
NoteViewHolder(视图){
超级(视图);
text1=view.findViewById(R.id.note_text1);
}
}
如何根据注释卡的文本长度动态更改注释卡的高度-您只需将布局高度设置为
wrap\u content
,如下所示:

android:layout\u height=“wrap\u 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>

它将如下所示:

蓝线表示两个视图之间的间隔


如果您想使用RecyclerView签出实现这一点,我将假设您想要实现交错网格视图。如果是,结帐