Reputation: 5990
I am using the ExpandableHeightGridView
Its used in my app to load images. its working fine but if I load images more than about 35 to 40 images then it gives blank screen also hides other controls in my scroll view. Can anybody help me to fix this.
Edit
Code:
package com.abc.util;
import android.content.Context;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.GridView;
public class ExpandableHeightGridView extends GridView
{
boolean expanded = false;
public ExpandableHeightGridView(Context context)
{
super(context);
}
public ExpandableHeightGridView(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public ExpandableHeightGridView(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
}
public boolean isExpanded()
{
return expanded;
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
// HACK! TAKE THAT ANDROID!
if (isExpanded())
{
// Calculate entire height by providing a very large height hint.
// View.MEASURED_SIZE_MASK represents the largest height possible.
int expandSpec = MeasureSpec.makeMeasureSpec(MEASURED_SIZE_MASK, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
ViewGroup.LayoutParams params = getLayoutParams();
params.height = getMeasuredHeight();
}
else
{
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
public void setExpanded(boolean expanded)
{
this.expanded = expanded;
}
}
XML:
<com.abc.util.ExpandableHeightGridView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/gridViewImages"
android:gravity="center"
android:horizontalSpacing="5dp"
android:layout_margin="10dp"
android:numColumns="3"
android:verticalSpacing="10dp"/>
Adapter:
public class GridViewImageAdapter extends BaseAdapter
{
private ArrayList<String> list;
private ActivitiesDetail activityImages;
public GridViewImageAdapter(ArrayList<String> list, ActivitiesDetail activityImages)
{
this.list = list;
this.activityImages = activityImages;
}
@Override
public int getCount()
{
return list.size();
}
@Override
public Object getItem(int position)
{
return position;
}
@Override
public long getItemId(int position)
{
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
convertView = getLayoutInflater().inflate(R.layout.layout_item_grid_view_images, null);
try
{
final TextView textViewCaption = (TextView) convertView.findViewById(R.id.lblGridViewCellValue);
fonts.setFontOpenSansRegular(textViewCaption);
final String[] imageDetail = list.get(position).split("~");
textViewCaption.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
if (dialogs == null)
{
dialogs = new Dialogs(ActivitiesDetail.this);
}
dialogs.showImageCaptionDialog(imageDetail[0], textViewCaption, activityImages);
}
});
textViewCaption.setText(imageDetail[1]);
ImageView imageView = (ImageView) convertView.findViewById(R.id.imageViewGridCellImage);
imageView.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
if (imagesSelected)
{
Intent intent = new Intent(ActivitiesDetail.this, ImageSlider.class);
intent.putStringArrayListExtra("KEY_ARRAY_IMAGES", list);
intent.putExtra("KEY_POSITION", position);
startActivity(intent);
overridePendingTransition(R.anim.right_in, R.anim.left_out);
}
}
});
convertView.findViewById(R.id.imageViewPlayOverlay).setVisibility(View.GONE);
Picasso.with(ActivitiesDetail.this).load("file://" + imageDetail[0]).resize(100, 100).into(imageView);
}
catch (Exception ex)
{
ex.printStackTrace();
}
return convertView;
}
public void setList(ArrayList<String> list)
{
this.list = list;
notifyDataSetChanged();
}
private void clear()
{
list.clear();
notifyDataSetChanged();
}
}
Upvotes: 1
Views: 1207
Reputation: 5440
Generally you shouldn't use a scrollable view inside ScrollView but if your requirement is so then you have to manually in the code by changing the LayoutParams of GridView.
This is how we change LayoutParams:
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) gridView.getLayoutParams();
params.height = rowHeight*(imageList.size()/noOfColumns);
mUploadImageList.setLayoutParams(params);
where gridView is your GridView, rowHeight is the height of one row, imageList is a list of images we are showing in GridView, noOfColumns is the number of columns in our GridView. You can set the rowHeight and noOfColumns in res/values for easy accessible.
Upvotes: 2
Reputation: 24848
Try to replace your adapter with this
public class GridViewImageAdapter extends BaseAdapter
{
private ArrayList<String> list;
private ActivitiesDetail activityImages;
private Context context;
public GridViewImageAdapter(Context context,ArrayList<String> list, ActivitiesDetail activityImages)
{
this.context=context;
this.list = list;
this.activityImages = activityImages;
}
@Override
public int getCount()
{
return list.size();
}
@Override
public Object getItem(int position)
{
return position;
}
@Override
public long getItemId(int position)
{
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
ViewHolder holder;
if(convertView==null){
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.layout_item_grid_view_images, null,false);
holder.textViewCaption = (TextView) convertView.findViewById(R.id.lblGridViewCellValue);
holder.imageViewGridCellImage = (TextView) convertView.findViewById(R.id.imageViewGridCellImage);
holder.imageViewPlayOverlay = (TextView) convertView.findViewById(R.id.imageViewPlayOverlay);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
fonts.setFontOpenSansRegular(holder.textViewCaption);
final String[] imageDetail = list.get(position).split("~");
holder.textViewCaption.setText(imageDetail[1]);
holder.textViewCaption.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
if (dialogs == null)
{
dialogs = new Dialogs(ActivitiesDetail.this);
}
dialogs.showImageCaptionDialog(imageDetail[0], v, activityImages);
}
});
holder.imageViewGridCellImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (imagesSelected) {
Intent intent = new Intent(ActivitiesDetail.this, ImageSlider.class);
intent.putStringArrayListExtra("KEY_ARRAY_IMAGES", list);
intent.putExtra("KEY_POSITION", position);
startActivity(intent);
overridePendingTransition(R.anim.right_in, R.anim.left_out);
}
}
});
holder.imageViewPlayOverlay.setVisibility(View.GONE);
Picasso.with(ActivitiesDetail.this).load("file://" + imageDetail[0]).resize(100, 100).into(holder.imageViewGridCellImage);
return convertView;
}
private class ViewHolder{
TextView textViewCaption;
ImageView imageViewGridCellImage;
ImageView imageViewPlayOverlay;
}
public void setList(ArrayList<String> list)
{
this.list = list;
notifyDataSetChanged();
}
private void clear()
{
list.clear();
notifyDataSetChanged();
}
}
Upvotes: 1