满足这些需求的解决方案:将Gridview
替换为Recyclerview
,然后使用此方法计算Gridview
的“ auto_fit”行。通过图像的属性Gridview
将wrap_content
的项目布局尺寸设置为adjustViewBounds="true"
。
public int calculateNoOfColumns(Context context) {
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
float dpWidth = displayMetrics.widthPixels / displayMetrics.density;
int noOfColumns = (int) (dpWidth / 180);
return noOfColumns;
}
问题是,当我为LinearLayout
设置wrap_content高度时,GridView
不会显示所有项目(仅显示一半)。如果layout_height为match_parent,则可以使用,但是每个项目都会有很多空间。 (我将recyclerview
与HasFixedSize(true)
一起使用。GridView
将显示所有项目,如果高度和宽度均为固定大小
<?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>
mLayoutManager =new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
mLayoutManager.setStackFromEnd(true);
Recycler.setHasFixedSize(true);
Recycler.setLayoutManager(mLayoutManager);
Recycler.setAdapter(Adapter);
Recycler.setItemAnimator(null);
<?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>
<?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;
}
答案 0 :(得分:1)
尝试这个:
<?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>
似乎您粘贴的代码中的GridView是空的。您说应该包含在其中的图像位于关闭标签的外面。