2017-01-09 3 views
0

Я пытаюсь создать сетку для динамической загрузки изображений и данных с веб-сервера, но если я попытаюсь использовать Glide для загрузки изображения, это испортит мой макет, im новичок в программировании на Android поэтому, пожалуйста, будьте терпеливыGlide messes up gridlayout

правильная планировка такова: correct layout

но если я включить Скольжение это показывает, как это:

enter image description here

Как вы можете видеть, большой разрыв в нижней части, которые не должны быть там

Вот мой сетки адаптер:

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

Умм, вся ваша функция GetView перепутались. Он не будет работать должным образом для любого вида, которое было переработано - оно не будет работать, если вы вообще пролистнете. –

+0

mmm Я постараюсь полностью переписать мою функцию getview, чтобы увидеть, работает ли это, im not shure, если я могу это сделать, потому что я просто изучаю программирование на Android: P – Chico3001

+0

Ваша проблема в том, что все, что задает значение для представления, должно появиться после утверждения if - вам нужно сделать это независимо от того, есть ли его переработанное представление или новое представление. –

ответ

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

И не работал ...

Но тогда я изменился с скольжением на Picasso, она работала отлично ...

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

Я буду очень признателен, если кто-то объясняет мне, что случилось ...