2012-03-29 69 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