我正在尝试制作一个网格视图,以便从Web服务器动态加载图像和数据,但如果我尝试使用Glide加载图像,它会影响我的布局,我是一个关于Android编程的新手,所以请耐心等待
但如果我启用Glide,它会显示如下:
正如你所看到的,底部有一个很大的差距,不应该存在
这是我的网格适配器:
public class GridViewAdapter extends BaseAdapter {
private Context mContext;
ArrayList<HashMap<String, String>> hotelsList;
TextView hotelNameTv;
TextView hotelDistanceTv;
ImageView hotelImage;
public GridViewAdapter(Context c, ArrayList<HashMap<String, String>> hotelsList ) {
mContext = c;
this.hotelsList = hotelsList;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return hotelsList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return hotelsList.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View grid;
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
grid = new View(mContext);
grid = inflater.inflate(R.layout.hotel_item, null);
hotelNameTv = (TextView) grid.findViewById(R.id.hotelName);
hotelDistanceTv = (TextView) grid.findViewById(R.id.hotelDistance);
hotelImage = (ImageView) grid.findViewById(R.id.hotelImage);
HashMap<String, String> hotel=hotelsList.get(position);
hotelNameTv.setText(hotel.get("name"));
hotelDistanceTv.setText(hotel.get("distancemsg"));
/*
Glide.with(mContext)
.load("http://192.168.0.6/hoteles360ws/img/kron02.jpg")
.into(hotelImage);
*/
} else {
grid = (View) convertView;
}
return grid;
}
}
这是我的活动主资源文件:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="@color/windowBackground">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:orientation="vertical">
<include
android:id="@+id/toolbar"
layout="@layout/toolbar" />
<GridView
android:id="@+id/hotelsGrid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:verticalSpacing="@dimen/small_margin"
android:horizontalSpacing="@dimen/small_margin"
android:stretchMode="columnWidth"
android:numColumns="2"
android:padding="10dp" />
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
我的项目资源文件
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:elevation="30dp"
>
<TextView
android:text="Hotel Name"
android:layout_width="match_parent"
android:layout_height="56sp"
android:gravity="center_vertical"
android:id="@+id/hotelName"
android:textAppearance="@android:style/TextAppearance.Material.Large"
android:textColor="@color/colorPrimary"
android:paddingLeft="5sp" />
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="@drawable/hotel"
android:id="@+id/hotelImage"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:cropToPadding="false"
android:layout_below="@+id/hotelName"
android:layout_alignParentStart="true" />
<TextView
android:text="Hotel Distance"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/hotelDistance"
android:background="#222222"
android:textAppearance="@android:style/TextAppearance.Material.Inverse"
android:layout_below="@+id/hotelImage"
android:layout_alignParentStart="true"
android:padding="5sp"
android:textColor="@color/gold" />
</RelativeLayout>
</RelativeLayout>
感谢您的帮助!!!
答案 0 :(得分:0)
不确定我做了什么...首先我改变了我的观点:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if(convertView == null){
final LayoutInflater layoutInflater = LayoutInflater.from(mContext);
convertView = layoutInflater.inflate(R.layout.hotel_item, null);
}
final TextView hotelName = (TextView)convertView.findViewById(R.id.hotelName);
final TextView hotelDistance = (TextView)convertView.findViewById(R.id.hotelDistance);
final ImageView hotelImage = (ImageView)convertView.findViewById(R.id.hotelImage);
HashMap<String, String> hotel=hotelsList.get(position);
hotelName.setText(hotel.get("name"));
hotelDistance.setText(hotel.get("distancemsg"));
Glide.with(mContext)
.load("http://192.168.0.6/hoteles360ws/img/kron02.jpg")
.into(hotelImage);
return convertView;
}
它没有用......
但后来我从滑翔变为毕加索,它完美地运作了......
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if(convertView == null){
final LayoutInflater layoutInflater = LayoutInflater.from(mContext);
convertView = layoutInflater.inflate(R.layout.hotel_item, null);
}
final TextView hotelName = (TextView)convertView.findViewById(R.id.hotelName);
final TextView hotelDistance = (TextView)convertView.findViewById(R.id.hotelDistance);
final ImageView hotelImage = (ImageView)convertView.findViewById(R.id.hotelImage);
HashMap<String, String> hotel=hotelsList.get(position);
hotelName.setText(hotel.get("name"));
hotelDistance.setText(hotel.get("distancemsg"));
Picasso.with(mContext)
.load("http://192.168.0.6/hoteles360ws/img/kron02.jpg")
.into(hotelImage);
return convertView;
}
如果有人解释我发生的事情,我将非常感激......
答案 1 :(得分:0)
您可以查看以下链接。 Android中的contentView
非常苛刻。 (我建议您将Gridview
与Recyclerview
)
How to have a GridView that adapts its height when items are added