2017-01-09 107 views
0

我試圖做一個網格視圖動態加載圖像和數據從Web服務器,但如果我嘗試使用Glide加載圖像它弄亂了我的佈局,即時消息在Android編程新手所以請耐心等待滑動網格佈局

正確的佈局是這樣的: 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函數,看看是否有效,即時消息不能確定如果我可以做到這一點,因爲即時通訊只是學習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; 
} 

,並沒有工作......

但後來我從滑行改變畢加索,它的工作完美...

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

我會很感激,如果有人介紹我發生了什麼......