2017-07-31 336 views
1

這是我的GridView自定義適配器類。我試圖通過一次調用佈局壓縮器來優化網格滾動的速度,並使用視圖持有者,但現在滾動速度非常慢。GridView非常緩慢滾動

private Context mContext; 
private final String[] web; 
private final int[] Imageid; 

public CustomGrid(Context c, String[] web, int[] Imageid) { 
    mContext = c; 
    this.Imageid = Imageid; 
    this.web = web; 
} 

class MyViewHolder { 
    ImageView imageView; 
    TextView textView; 

    MyViewHolder(View v) { 
     imageView = (ImageView) v.findViewById(R.id.grid_image); 
     textView = (TextView) v.findViewById(R.id.grid_text); 
    } 

} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    // TODO Auto-generated method stub 
    View grid = convertView; 
    MyViewHolder viewHolder; 

    if (grid == null) { 
     LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     grid = inflater.inflate(R.layout.grid_single, null); 
     viewHolder = new MyViewHolder(grid); 
     grid.setTag(viewHolder); 
    } else { 
     grid = convertView; 
     viewHolder= (MyViewHolder) grid.getTag(); 
    } 
    viewHolder.textView.setText(web[position]); 
    viewHolder.imageView.setImageResource(Imageid[position]); 
    return grid; 
} 

回答

1

我認爲你的代碼應該可以正常工作。唯一可能的原因是您加載的圖像太大以至於內存無法同時加載所有圖像。

您可能想嘗試更小的尺寸。或者您可以考慮使用Glide加載圖像,因爲它會根據ImageView的大小調整圖像大小。