2017-02-09 55 views
0

我使用gridview的以下代碼的類。我怎麼能通過視圖或其他方法來平滑我的gridview滾動? 我只想我的滾動gridview表現非常順利。我使用這個gridview顯示圖像和名稱,並通過點擊eacj項目打開另一個活動的細節。如何在android中平滑我的gridview滾動?

import android.content.Context; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.ImageView; 
import android.widget.TextView; 

public class CustomGrid extends BaseAdapter{ 

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; 
} 
@Override 
public int getCount() { 
// TODO Auto-generated method stub 
    return web.length; 
} 
@Override 
public Object getItem(int position) { 
// TODO Auto-generated method stub 
    return null; 
} 
@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); 

    grid = new View(mContext); 
    grid = inflater.inflate(R.layout.grid_item, null); 
    TextView textView = (TextView) grid.findViewById(R.id.grid_text); 
    ImageView imageView = (ImageView)grid.findViewById(R.id.grid_image); 
    textView.setText(web[position]); 
    imageView.setImageResource(Imageid[position]); 


    return grid; 
} 
} 
+0

使用viewHolder模式 – d1vivek681065

+0

作爲一個說明什麼@ d1vivek681065已經說了,一定要仔細閱讀http://stackoverflow.com/questions/11945563/how-listviews-recycling-mechanism-works。只要確保你明白爲什麼它太慢了。只解決問題不會解決真正的問題 –

回答

0

始終嘗試使用ViewHolder模式列表和girds這提高了列表和網格的性能:

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 

    ViewHolder viewHolder = null; 
    if(convertView==null){ 
     viewHolder = new ViewHolder(); 
     convertView = inflater.inflate(R.layout.grid_item,parent,false); 

     convertView.setTag(viewHolder); 
    } 
    else { 
     viewHolder=(ViewHolder)convertView.getTag(); 
    } 
    viewHolder.image=(ImageView)convertView.findViewById(R.id.thumbnail); 
    viewHolder.title=(TextView)convertView.findViewById(R.id.title); 
    viewHolder.title.setText(Html.fromHtml(values.getPosts().get(position).getTitle())); 

    imageView.setImageResource(Imageid[position]); 
    return convertView; 
} 

private static class ViewHolder{ 
    public static ImageView image; 
    public static TextView title; 
}