2012-03-29 101 views
1

我試圖在一個佈局中放入三個或甚至四個GridView並同時滾動它們。同時滾動三個GridView

由於GridView了自己ScrollView的小工具,我需要禁用ScrollView通過android:isScrollContainer="false"屬性,並覆蓋GrideView措施有wrap_content功能,這裏是我的擴展GrideView:

public class ExpandedGridView extends GridView 
{ 
    public ExpandedGridView (Context context) 
    { 
     super(context); 
    } 

    public ExpandedGridView (Context context, AttributeSet attrs) 
    { 
     super(context, attrs); 
    } 

    public gridView(Context context, AttributeSet attrs, int defStyle) 
    { 
     super(context, attrs, defStyle); 
    } 

    @Override 
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) 
    { 
     // Calculate entire height by providing a very large height hint. 
     // But do not use the highest 2 bits of this integer; those are 
     // reserved for the MeasureSpec mode. 
     int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE/2, MeasureSpec.AT_MOST); 
     super.onMeasure(widthMeasureSpec, expandSpec); 
    } 
} 

它的工作,但它是禁用一些BaseAdapter的服務員喜歡回收,重複使用等等,所以這使得我的GridView在滾動中變得很慢,而且不夠流暢!

有關如何使其更快的任何想法?

更新1:加入我的GridAdapter類:

public class GridAdapter extends BaseAdapter 
{ 
    private Context c; 
    private DisplayImageOptions mOptions; 
    private ImageLoader mImageLoader; 
    private String[] appIconUri, appName, appPrice, appRate; 

    public GridAdapter(Context c, String[] appIconUri, String[] appName 
      , String[] appPrice, String[] appRate) 
    { 
     this.appIconUri = appIconUri; 
     this.appName = appName; 
     this.appPrice = appPrice; 
     this.appRate = appRate; 
     this.c = c; 

     // Setup ImageLoader 
     this.mImageLoader = ImageLoader.getInstance(); 

     this.mOptions = new DisplayImageOptions.Builder() 
      .showStubImage(R.drawable.border) 
      .showImageForEmptyUri(R.drawable.border) 
      .imageScaleType(ImageScaleType.IN_SAMPLE_INT) 
      .bitmapConfig(Bitmap.Config.RGB_565) 
      .resetViewBeforeLoading() 
      .cacheOnDisc() 
      .displayer(new FadeInBitmapDisplayer(750)) 
      .build(); 

     if (!mImageLoader.isInited()) 
     { 
      ImageLoaderConfiguration mImageLoaderConfiguration = new ImageLoaderConfiguration.Builder(c) 
       .threadPoolSize(2) 
       .memoryCache(new WeakMemoryCache()) 
       .discCacheFileNameGenerator(new Md5FileNameGenerator()) 
       .build(); 
      mImageLoader.init(mImageLoaderConfiguration); 
     } 
    } 

    public ImageLoader getCurrentImageLoader() 
    { 
     return mImageLoader; 
    } 

    @Override 
    public int getCount() 
    { 
     return appName.length; 
    } 

    @Override 
    public Object getItem(int itemData) 
    { 
     return null; 
    } 

    @Override 
    public long getItemId(int itemPosition) 
    { 
     return itemPosition; 
    } 

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

     if (convertView == null) 
     { 
      LayoutInflater mLayoutInflater = ((Activity) c).getLayoutInflater(); 
      convertView = mLayoutInflater.inflate(R.layout.grid_list, null); 

      mViewHolder = new ViewHolder(); 

      mViewHolder.lblName = (textView) convertView.findViewById(R.id.appName); 
      mViewHolder.lblPrice = (textView) convertView.findViewById(R.id.appPrice); 
      mViewHolder.rtbRate = (RatingBar) convertView.findViewById(R.id.appRate); 
      mViewHolder.imgIcon = (ImageView) convertView.findViewById(R.id.appIcon); 

      convertView.setTag(mViewHolder); 
     } 
     else 
      mViewHolder = (ViewHolder) convertView.getTag(); 

     mViewHolder.lblName.setText(appName[position]); 

     // This line should be changed 
     mViewHolder.lblPrice.setText((Integer.valueOf(appPrice[position]) == 0) ? "مجانی" : appPrice[position]); 

     mViewHolder.lblPrice.setGravity(Gravity.RIGHT); 
     mViewHolder.rtbRate.setRating(appRate[position] != null ? Float.valueOf(appRate[position]) : 0); 

     mImageLoader.displayImage(appIconUri[position], mViewHolder.imgIcon, mOptions); 

     return convertView; 
    } 

    private static class ViewHolder 
    { 
     textView lblName, lblPrice; 
     RatingBar rtbRate; 
     ImageView imgIcon; 
    } 
+0

你可以顯示你同時滾動的網格截圖嗎?..我的意思是,如何在屏幕上對齊? – bakriOnFire 2013-05-10 14:18:32

+0

對我來說,你似乎很奇怪,你需要同時在屏幕上顯示多個GridView,你可能首先想重新考慮這個設計。你能提供一個你試圖效仿的例子嗎?你也不解釋你爲什麼要包裝內容的工作,否則你可以嘗試同步滾動列表。 – Eluvatar 2013-05-10 14:23:07

+0

@bakriOnFire請參閱此鏈接澄清http://www.jayway.com/2012/10/04/how-to-make-the-height-of-a-gridview-wrap-its-content/ – iSun 2013-05-10 14:40:44

回答

1

快速搜索,我發現一個圖書館有人生來就能做到這一點已經http://tonicartos.github.io/StickyGridHeaders/沒有必要推倒重來之後。

+0

這是一個很好的解決方案,但我的佈局還包含一些其他的東西,如'Buttons','ViewFlipper'等。 – iSun 2013-05-10 18:29:23

+0

它只是一個視圖,你不會被迫只使用1視圖。你會像使用多個網格一樣使用它,現在只需用stickyGridHeader視圖替換它們全部 – Eluvatar 2013-05-10 21:11:13

+0

我可以把它放在ScrollView中嗎? – iSun 2013-05-12 08:37:58