【问题标题】:Why my RecyclerView in Android is not showing views correctly?为什么我在 Android 中的 RecyclerView 没有正确显示视图?
【发布时间】:2020-06-30 12:59:08
【问题描述】:

我正在做一个简单的项目,其中我使用 RecyclerView 在我的片段中使用 GridLayoutManager 显示一些 ImageView 和 TextView 的网格。我按照谷歌上的一些教程做了。但是我不知道为什么垂直视图之间会自动有这么多的填充,就好像只有 2 个相邻的视图只显示了一样。 Here is a GIF of it

这是我的 fragment_home.xml 文件,recylerview 将在其中显示:

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/gridrecyle
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="220dp"
    android:gravity="center"
    android:numColumns="2"
    android:columnWidth="180dp"/> 

这是每个视图的 single_item.xml 文件:

<LinearLayout
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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
    android:id="@+id/imagessssss"
    android:layout_width="180dp"
    android:layout_margin="10dp"
    android:layout_marginBottom="30dp"
    android:background="@drawable/grid_image"
    android:layout_height="180dp" />
<TextView
    android:id="@+id/texttsss"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:lines="1"
    android:maxLines="1"
    android:textSize="25sp" />
 </LinearLayout>

这是我的 fragment_home java 类:

@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
     final  View vii = inflater.inflate(R.layout.fragment_home, null);
     context = getActivity();
     RecyclerView recyclerView = vii.findViewById(R.id.gridrecyle);
     CustomRecycleAdaptor adaptor = new CustomRecycleAdaptor(context);
     recyclerView.setAdapter(adaptor);
     recyclerView.setLayoutManager(new GridLayoutManager(context , 2 , GridLayoutManager.VERTICAL , false ));

这是我用于适配器的 CustomRecycleAdaptor java 类:

public class CustomRecycleAdaptor extends RecyclerView.Adapter <CustomRecycleAdaptor.Viewholder>{

private final Context mcontext;

public CustomRecycleAdaptor(Context mcontext) {
    this.mcontext = mcontext;
}


//This is a method for giving padding to imageviews with converting dp to pixels
public static float convertDpToPixel(float dp, Context context){
    return dp * ((float) context.getResources().getDisplayMetrics().densityDpi / DisplayMetrics.DENSITY_DEFAULT);
}


public Integer[] mimg = {
    R.drawable.cake, R.drawable.donut, R.drawable.ice_creame, R.drawable.smoothie
};

public String[] mstrng = {
        "Cakes" , "Donuts" , "Ice Creams" , "Smoothies"
};

@NonNull
@Override
public CustomRecycleAdaptor.Viewholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {


    LayoutInflater layoutInflater = LayoutInflater.from(mcontext);
    View vii = layoutInflater.inflate(R.layout.single_item , parent , false);
    Viewholder viewHolder = new Viewholder(vii);
    return viewHolder;
}

@Override
public void onBindViewHolder(@NonNull CustomRecycleAdaptor.Viewholder holder, int position) {

    TextView textView = holder.texts;
    ImageView imageView = holder.images;

    textView.setText(mstrng[position]);
    imageView.setImageResource(mimg[position]);

    int imgpadding = Math.round(convertDpToPixel(10 , mcontext));
    int txtpadding = Math.round(convertDpToPixel(40 , mcontext));

    imageView.setPadding(imgpadding , imgpadding , imgpadding , imgpadding);
    textView.setPadding(txtpadding , 0 , 0 , 0);

}

@Override
public int getItemCount() {
    return mstrng.length;
}


public class Viewholder extends RecyclerView.ViewHolder {
    public TextView texts;
    public ImageView images;

    public Viewholder(@NonNull View itemView) {
        super(itemView);

        texts = itemView.findViewById(R.id.texttsss);
        images = itemView.findViewById(R.id.imagessssss);
    }
}}

请帮助我,在此先感谢!

更新:上传完整的 single_actvity.xml

【问题讨论】:

  • 请提供完整的 single_item xml 脚本标签
  • 您可能没有正确约束视图,您的视图的父布局是什么?
  • 同意@AderojuIsrael。
  • 在 single_item.xml 中,尝试将 LinearLayout android:layout_height="match_parent" 更改为 android:layout_height="wrap_content"。
  • 谢谢! , i_A_mok ,将我的线性布局高度设置为“包装内容”后,它工作得非常好。 (没看一眼)谢谢!

标签: android android-recyclerview


【解决方案1】:

您能否在您的 recyclerview XML 中尝试以下代码

android:layout_height="wrap_content"
    app:layout_constrainedHeight="true"

更改高度以包裹内容并添加第二行。这样,recyclerview 的高度应该与其内容相同,但永远不会超过准则/约束。

【讨论】:

    【解决方案2】:

    答案正如@i_A_mok 所说,single_item.xml 中的线性布局高度应设置为“包装内容”,以便仅考虑单个视图而不是全屏(match_parent)。谢谢!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-10
      • 2021-06-08
      • 2021-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多