简体   繁体   中英

Why my RecyclerView in Android is not showing views correctly?

I am working on a simple project and in which I am using RecyclerView to show a grid of some ImageView and TextView using GridLayoutManager in my fragment. I followed some tutorials on Google and made it. But I don't know why there is automatically so much padding between vertical views as if only 2 adjacent views are only showing up. Here is a GIF of it

This is my fragment_home.xml file in which the recylerview is to be shown:

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

This is my single_item.xml file for each view:

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

This is my 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 ));

This is my CustomRecycleAdaptor java class for the adaptor:

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);
    }
}}

Kindly Assist me, Thanks in advance!

Update: Uploaded full single_actvity.xml

Could you please try the following code in your recyclerview XML

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

Change the height to wrap content and add the second the line. This way the recyclerview should have the height same as its content but never exceeds the guideline/contraint.

The answer is as @i_A_mok said, the linear layout's height in single_item.xml should be set to "wrap content" in order to just consider the single view not the full screen (of match_parent). Thank You!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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