Trần Quốc Trung
Trần Quốc Trung

Reputation: 135

GridView dose not show all items in a wrap_content height Linearlayout

[SOVLED]

The Solution for those need : replace the Gridview with Recyclerview and use this method to calculate the "auto_fit" rows for Gridview . set Gridview item layout size to wrap_content with attribute adjustViewBounds="true" for the images.

 public int calculateNoOfColumns(Context context) {
        DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
        float dpWidth = displayMetrics.widthPixels / displayMetrics.density;
        int noOfColumns = (int) (dpWidth / 180);
        return noOfColumns;
    }

The problem is when I set the wrap_content height for the LinearLayout, the GridView doesn't show all items ( just a half ). It works if the layout_height is match_parent, but each item will have a lot of space. (I use recyclerview with HasFixedSize(true). the GridView will show all items if height and width are fixed size

This is the Layout contains Recyclerview :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    android:fitsSystemWindows="true"
    tools:context=".Activities.Chat">

//APPBAR LAYOUT HERE. BUT DOES NOT MATTER

    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:background="@color/white"
        android:fitsSystemWindows="true"
        android:layout_height="match_parent">

            <android.support.v7.widget.RecyclerView
                android:layout_width="match_parent"
                android:scrollbars="none"
                app:stackFromEnd="true"
                android:background="@color/white"
                android:layout_height="match_parent">

            </android.support.v7.widget.RecyclerView>

    </android.support.design.widget.CoordinatorLayout>

   //SOME VIEWS HERE BUT NOT IMPORTANT

</RelativeLayout>

this is the code of RecyclerView

mLayoutManager =new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
mLayoutManager.setStackFromEnd(true);
Recycler.setHasFixedSize(true);
Recycler.setLayoutManager(mLayoutManager);
Recycler.setAdapter(Adapter);
Recycler.setItemAnimator(null);

this is the RecyclerView Item :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="right"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@color/white"
    android:orientation="vertical"
    android:padding="5dp"
    xmlns:android="http://schemas.android.com/apk/res/android">

   //more views here but not important

    <LinearLayout android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="right"
        android:paddingLeft="60dp"
        android:gravity="right"
        android:orientation="vertical"
        xmlns:android="http://schemas.android.com/apk/res/android">

            <android.support.v7.widget.CardView
                android:layout_width="match_parent"
                app:cardCornerRadius="10dp"
                android:layout_marginLeft="70dp"
                android:layout_marginRight="17dp"
                android:layout_marginTop="5dp" 
                android:layout_height="wrap_content">

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center">

                <GridView
                    android:layout_width="match_parent"
                    android:layout_gravity="center"
                    android:gravity="center"
                    android:scrollbars="none"
                    android:layout_height="wrap_content">

                </GridView>

                <ImageView
                    xmlns:app="http://schemas.android.com/apk/res-auto"
                    android:scaleType="centerCrop"
                    android:layout_height="match_parent"
                    android:layout_width="match_parent"
                    android:minHeight="100dp"
                    android:minWidth="100dp"
                    android:adjustViewBounds="true"
                    android:layout_gravity="center"/>

                    <ImageView
                        android:layout_width="60dp"
                        android:layout_gravity="center"
                        android:src="@drawable/ic"
                        android:layout_height="60dp"/>

                </RelativeLayout>

                </android.support.v7.widget.CardView>

</LinearLayout>

this is the grid view item and grid view adapter

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_gravity="center"
    android:layout_height="wrap_content">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="centerCrop"
        android:background="@color/grey"
        android:layout_gravity="center"
        android:adjustViewBounds="true"
        android:id="@+id/img_row_multiple_media"/>

    <ImageView
        android:layout_width="30dp"
        android:layout_gravity="center"
        android:id="@+id/playbutton_row_multiple_media"
        android:src="@drawable/ic_play_button"
        android:layout_height="30dp"/>

</FrameLayout>
 @Override
    public View getView(int mIndex, View convertView, ViewGroup parent) {

        mViewHolder viewHolder;
        if (convertView==null){
            viewHolder=new mViewHolder();
            LayoutInflater inflater= (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView=inflater.inflate(R.layout.row_multiple_media,null);
            viewHolder.imageView=(ImageView)convertView.findViewById(R.id.img_row_multiple_media);
            viewHolder.playbutton=(ImageView)convertView.findViewById(R.id.playbutton_row_multiple_media);

            convertView.setTag(viewHolder);
        }else viewHolder = (mViewHolder) convertView.getTag();


        if (mList.get(mIndex).getType().equalsIgnoreCase(MessagesUtils.MESSAGE_TYPE_PICTURE))
            viewHolder.playbutton.setVisibility(View.GONE);
        else  viewHolder.playbutton.setVisibility(View.VISIBLE);


        if (mList.get(mIndex).getLink_thumb_media()!=null) {
            Glide.with(mContext.getApplicationContext())
                    .load(mList.get(mIndex).getLink_thumb_media())
                    .into(viewHolder.imageView);

        }

        return convertView;
    }

Upvotes: 1

Views: 895

Answers (1)

Lukas Novicky
Lukas Novicky

Reputation: 952

try this one:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@color/white"
    android:orientation="vertical"
    android:padding="5dp"
    xmlns:android="http://schemas.android.com/apk/res/android">

MORE VIEWS HERE BUT IT DOES NOT MATTER.

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="70dp"
            android:layout_marginRight="17dp"
            android:layout_marginTop="5dp"
            android:id="@+id/layout_media">

                <FrameLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center">

                <GridView
                    android:layout_width="wrap_content"
                    android:layout_gravity="center"
                    android:background="@color/black"
                    android:gravity="center"
                    android:numColumns="auto_fit"
                    android:id="@+id/multiple_media_list"
                    android:layout_height="wrap_content">
                        <ImageView
                            xmlns:app="http://schemas.android.com/apk/res-auto"
                            android:id="@+id/message_picture"
                            android:scaleType="fitCenter"
                            android:layout_height="wrap_content"
                            android:minHeight="150dp"
                            android:minWidth="150dp"
                            android:layout_width="wrap_content"
                            android:layout_gravity="center"
                            android:adjustViewBounds="true"/>
                        <ImageView
                            android:layout_width="60dp"
                            android:layout_gravity="center"
                            android:id="@+id/playbutton"
                            android:src="@drawable/ic_play_button"
                            android:layout_height="60dp"/>
                    </GridView>
                </FrameLayout>
        </RelativeLayout>
</LinearLayout>

It seems that your GridView was empty in code you've pasted. Images you said supposed to be in it, was outside of closing tab.

Upvotes: 1

Related Questions