gpt4 book ai didi

java - RecyclerView 中的网格项未正确显示

转载 作者:行者123 更新时间:2023-12-02 01:55:37 25 4
gpt4 key购买 nike

出于某种原因,我的 RecyclerView 的内容(即 String 数组中的字母)没有显示为异常。应该出现 6 个字母,但只有 2 个出现。我已经对 RecyclerView 本身及其父级使用了 android:layout_height="wrap_content" ,但这仍然没有产生任何影响。

enter image description here

包含 RecyclerView 的 CardView XML(用于网格)

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:id="@+id/cardview_gv">

<LinearLayout
android:id="@+id/cardview_gv_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp"
android:animateLayoutChanges="true">

<LinearLayout
android:id="@+id/cardview_gv_titlerow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="100">

<TextView
android:id="@+id/tv_gv_title"
android:layout_weight="90"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textColor="?android:attr/textColorPrimary"
style="@android:style/TextAppearance.Medium" />

<TextView
android:id="@+id/tv_gv_expandcollapse"
android:clickable="true"
android:focusable="true"
android:layout_weight="10"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="10dp" />
</LinearLayout>

<RelativeLayout
android:id="@+id/relativelayout_gv"
android:animateLayoutChanges="true"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:nestedScrollingEnabled="false"
android:overScrollMode="never"
android:scrollbars="none" />
</RelativeLayout>
</LinearLayout>
</android.support.v7.widget.CardView>

Fragment中RecyclerView相关代码

                static final String[] frenchVowels = new String[]{
"a", "e", "i", "o", "u", "y"
};

RecyclerViewAdapter rvAdapterGL;
final RecyclerView rvGL = viewHolder.itemView.findViewById(R.id.rv);
int numberOfColumns = 2;
rvGL.setLayoutManager(new GridLayoutManager(getActivity(), numberOfColumns));
rvAdapterGL = new RecyclerViewAdapter(getActivity(), frenchVowels);
rvGL.setAdapter(rvAdapterGL);

RecyclerView适配器类

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {

private String[] mData;
private LayoutInflater mInflater;

// data is passed into the constructor
public RecyclerViewAdapter(Context context, String[] data) {
this.mInflater = LayoutInflater.from(context);
this.mData = data;
}

// inflates the cell layout from xml when needed
@Override
@NonNull
public RecyclerViewAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = mInflater.inflate(R.layout.gridview_item, parent, false);
return new RecyclerViewAdapter.ViewHolder(view);
}

// binds the data to the TextView in each cell
@Override
public void onBindViewHolder(@NonNull RecyclerViewAdapter.ViewHolder holder, int position) {
holder.myTextView.setText(mData[position]);
}

// total number of cells
@Override
public int getItemCount() {
return mData.length;
}


// stores and recycles views as they are scrolled off screen
public class ViewHolder extends RecyclerView.ViewHolder {
TextView myTextView;

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

gridview_item.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="match_parent" >

<TextView
android:id="@+id/item_gridview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingStart="0dp"
android:paddingEnd="10dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"/>
</LinearLayout>

最佳答案

您必须将recyclerview项目的高度设置为wrap_content,否则它会扩展以占用所有可用空间(第2行及以后的行超出屏幕)。

<?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" >

<TextView
android:id="@+id/item_gridview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingStart="0dp"
android:paddingEnd="10dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"/>
</LinearLayout>

关于java - RecyclerView 中的网格项未正确显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52358394/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com