繁体   English   中英

为什么我在 Android 中的 RecyclerView 没有正确显示视图?

[英]Why my RecyclerView in Android is not showing views correctly?

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

这是我的 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 class:

@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 class 适配器:

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

您能否在您的回收站视图中尝试以下代码 XML

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM