我正在使用Custom GridView,我有2列图像。 当我将Grid向下滚动到下一个记录并返回到第一行记录时,我的记录从其位置开始连续洗牌。
表示当 getview()中的视图重新生成时, GridView 记录从一列到另一条记录的随机播放。
请建议我。
private class ImageAdapter extends BaseAdapter {
private LinkedList<GalleryFeed> imagesLinkedList = new LinkedList<GalleryFeed>();
private Context mContext = null;
private LayoutInflater _layoutInflater = null;
private ImageLoader _feedLoader = null;
/**
*
* @param localContext
* @param mListItems
*/
public ImageAdapter(Context localContext,LinkedList<GalleryFeed> mListItems) {
mContext = localContext;
this.imagesLinkedList = mListItems;
_layoutInflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
_feedLoader = new ImageLoader(DefaultGalleryFrag.this.getActivity().getParent());
}
public int getCount() {
return imagesLinkedList != null ? imagesLinkedList.size() : 0;
}
public Object getItem(int position) {
return imagesLinkedList.get(position);
}
public long getItemId(int position) {
return 0;
}
public class ViewHolder{
public ImageView imageView=null;
public ImageView videoImageView=null;
}
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
ViewHolder viewHolder = null;
GalleryFeed bean = imagesLinkedList.get(position);
if(convertView == null){
viewHolder=new ViewHolder();
LayoutInflater linf = (LayoutInflater) mContext
.getSystemService(mContext.LAYOUT_INFLATER_SERVICE);
v = linf.inflate(R.layout.item_gallery_image, null);
viewHolder.imageView=(ImageView)v.findViewById(R.id.image);
viewHolder.videoImageView=(ImageView)v.findViewById(R.id.videoPalyIV);
/**
* SET THE RESOLUTION PIC ON IMAGE VIEW
*/
if(bean.getMediaType()!=null && bean.getMediaType().equalsIgnoreCase("Image")){
if(bean.getResolution280()!=null && bean.getResolution280().length()>0){
File imgFile = new File(bean.getResolution280());
if(imgFile.exists()&& imgFile.length()>0){
Bitmap myBitmap = Constant.getBitmap(imgFile.getAbsolutePath());
if(myBitmap!=null){
viewHolder.imageView.setTag(bean.getResolution280());
Drawable d = new BitmapDrawable(getResources(),myBitmap);
viewHolder.imageView.setImageDrawable(d);
}else{
_feedLoader.DisplayImage(bean.getResolution280(), R.drawable.loader, viewHolder.imageView);
}
}else{
_feedLoader.DisplayImage(bean.getResolution280(), R.drawable.loader, viewHolder.imageView);
}
}else{
_feedLoader.DisplayImage(bean.getResolution280(), R.drawable.loader, viewHolder.imageView);
}
viewHolder.videoImageView.setVisibility(View.GONE);
}else{
/**
* IF ITS A VIDEO FEED
*/
viewHolder.videoImageView.setVisibility(View.VISIBLE);
//_feedLoader.DisplayImage(bean.getVideoFrame(), R.drawable.loader, viewHolder.imageView);
viewHolder.imageView.setBackgroundResource(R.drawable.profile_pic);
}
/*if(mediaLeftForDownload.contains(String.valueOf(position))&& _mediaUrlList!=null &&_mediaUrlList.size()>0){
_imageLoader.DisplayImage(_mediaUrlList.get(position), R.drawable.loader, viewHolder.imageView);
}else{
viewHolder.imageView.setBackgroundDrawable(imagesLinkedList.get(position));
}*/
v.setTag(viewHolder);
}
else{
viewHolder = (ViewHolder) v.getTag();
}
DisplayMetrics dm = new DisplayMetrics();
DefaultGalleryFrag.this.getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
int screenWidth = dm.widthPixels;
if(screenWidth<500){
viewHolder.imageView.setLayoutParams(/*new GridView.LayoutParams(300,300)*/ new LayoutParams(200,200));
}else{
viewHolder.imageView.setLayoutParams(new LayoutParams(300,300));
}
return v;
}
}
如图所示:
答案 0 :(得分:1)
视图符号模式以getView
:
ViewHolder holder = null;
if (convertView == null) {
convertView = inflater.inflate(R.layout.myLayout, null);
holder = new ViewHolder();
// Populate the holder
holder.textView = (TextView) convertView.findViewById(R.id.myTextView);
holder.imageView = (ImageView) convertView.findViewById(R.id.myImageView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
// Now, populate the views referenced by the viewHolder:
holder.textView.setText("Hello World");
整个想法是你不知道convertView是否将为null(即你将创建一个新的)或者它是否将从以前存在的视图中回收,从而节省了膨胀时间。
在您的情况下,您只在convertView
为空时填充视图。您需要将填充代码移到if子句之外。
答案 1 :(得分:0)
/**
* Adapter for our image files.
*/
private class ImageAdapter extends BaseAdapter {
private LinkedList<GalleryFeed> imagesLinkedList = new LinkedList<GalleryFeed>();
private Context mContext = null;
private LayoutInflater _layoutInflater = null;
private ImageLoader _feedLoader = null;
/**
*
* @param localContext
* @param mListItems
*/
public ImageAdapter(Context localContext,LinkedList<GalleryFeed> mListItems) {
mContext = localContext;
this.imagesLinkedList = mListItems;
_layoutInflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
_feedLoader = new ImageLoader(DefaultGalleryFrag.this.getActivity().getParent());
}
public int getCount() {
return imagesLinkedList != null ? imagesLinkedList.size() : 0;
}
public Object getItem(int position) {
return imagesLinkedList.get(position);
}
public long getItemId(int position) {
return 0;
}
public class ViewHolder{
public ImageView imageView=null;
public ImageView videoImageView=null;
}
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
ViewHolder viewHolder = null;
GalleryFeed bean = imagesLinkedList.get(position);
if(convertView == null){
viewHolder=new ViewHolder();
LayoutInflater linf = (LayoutInflater) mContext
.getSystemService(mContext.LAYOUT_INFLATER_SERVICE);
v = linf.inflate(R.layout.item_gallery_image, null);
viewHolder.imageView=(ImageView)v.findViewById(R.id.image);
viewHolder.videoImageView=(ImageView)v.findViewById(R.id.videoPalyIV);
v.setTag(viewHolder);
}
else{
viewHolder = (ViewHolder) v.getTag();
}
/**
* SET THE RESOLUTION PIC ON IMAGE VIEW
*/
if(bean.getMediaType()!=null && bean.getMediaType().equalsIgnoreCase("Image")){
if(bean.getResolution280()!=null && bean.getResolution280().length()>0){
File imgFile = new File(bean.getResolution280());
if(imgFile.exists()&& imgFile.length()>0){
Bitmap myBitmap = Constant.getBitmap(imgFile.getAbsolutePath());
if(myBitmap!=null){
viewHolder.imageView.setTag(bean.getResolution280());
Drawable d = new BitmapDrawable(getResources(),myBitmap);
viewHolder.imageView.setImageDrawable(d);
viewHolder.imageView.setTag(bean.getMediaId());//===set tag
}else{
_feedLoader.DisplayImage(bean.getResolution_280_280URL(), R.drawable.loader, viewHolder.imageView);
viewHolder.imageView.setTag(bean.getMediaId());//===set tag
}
}else{
_feedLoader.DisplayImage(bean.getResolution_280_280URL(), R.drawable.loader, viewHolder.imageView);
viewHolder.imageView.setTag(bean.getMediaId());//===set tag
}
}else{
_feedLoader.DisplayImage(bean.getResolution_280_280URL(), R.drawable.loader, viewHolder.imageView);
viewHolder.imageView.setTag(bean.getMediaId());//===set tag
}
viewHolder.videoImageView.setVisibility(View.GONE);
}else{
/**
* IF ITS A VIDEO FEED
*/
viewHolder.videoImageView.setVisibility(View.VISIBLE);
viewHolder.imageView.setTag(bean.getMediaId());//===set tag
//_feedLoader.DisplayImage(bean.getVideoFrame(), R.drawable.loader, viewHolder.imageView);
Bitmap icon = BitmapFactory.decodeResource(DefaultGalleryFrag.this.getActivity().getParent().getResources(),
R.drawable.profile_pic);
viewHolder.imageView.setImageBitmap(icon);
}
DisplayMetrics dm = new DisplayMetrics();
DefaultGalleryFrag.this.getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
int screenWidth = dm.widthPixels;
if(screenWidth<500){
viewHolder.imageView.setLayoutParams(/*new GridView.LayoutParams(300,300)*/ new LayoutParams(200,200));
}else{
viewHolder.imageView.setLayoutParams(new LayoutParams(300,300));
}
return v;
}
}