我有GridView,我根据项目位置加载不同的视图:
@Override
public View getView(int _i, View _view, ViewGroup viewGroup){
BlockMod _b = (BlockMod)getItem(_i);
if(_b.getType().equals(BlockMod.BLOCK_TYPE_NOTE))
_view = new BlockViewNote(getActivity(),gridView.getColumnWidth(), _b);
if(_b.getType().equals(BlockMod.BLOCK_TYPE_IMAGE))
_view = new BlockViewImage(getActivity(),gridView.getColumnWidth(), _b);
if(_b.getType().equals(BlockMod.BLOCK_TYPE_STATUS))
_view = new BlockViewStatus(getActivity(),gridView.getColumnWidth());
return _view;
}
在这些视图中,我加载了一些远程图像:
public BlockViewImage(Context context, int _columnWidth, BlockMod _block){
super(context);
block = _block;
rootView = View.inflate(context, R.layout.story_block_image,this);
AbsListView.LayoutParams _lp = new AbsListView.LayoutParams(_columnWidth,_columnWidth);
this.setLayoutParams(_lp);
ImageSize targetSize = new ImageSize(_columnWidth, _columnWidth);
final ImageView _background = (ImageView)rootView.findViewById(R.id.storyBlockImageBackground);
DisplayImageOptions _imageOptions = new DisplayImageOptions.Builder().cacheInMemory(true).cacheOnDisc(true).build();
ImageLoader.getInstance().loadImage(block.getImageSmall(), targetSize, _imageOptions , new SimpleImageLoadingListener(){
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage){
_background.setImageBitmap(loadedImage);
}
});
}
问题是它不会为任何初始视图加载图像,如果我向下滚动它将加载下一个视图的所有图像。如果我向上滚动它将加载以前的图像。但是如果没有滚动,就永远不会加载它。
如果我为图像使用不同的库,那么它可以正常工作:
Picasso.with(context)
.load(block.getImageSmall())
.into(_background);
完整Fragment类的代码:
package net.eq.pck.fragments.main.story;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import net.eq.pck.*;
import net.eq.pck.compoundViews.CustomGridView;
import net.eq.pck.fragments.main.story.blocks.*;
import net.eq.pck.model.block.BlockMod;
import net.eq.pck.model.stories.StoryMod;
import net.eq.pck.server.Scheduler;
import java.util.HashMap;
/**
* Created by usr on 1/21/14.
*/
public class StoryFragment2 extends Fragment{
private StoryMod story;
private View rootView;
private CustomGridView gridView;
private CustomAdapter gridAdapter;
public void setStory(StoryMod _story){
story = _story;
}
public interface StoryFragmentInterface{
void onBack();
}
public StoryFragment2(){
}
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
rootView = inflater.inflate(R.layout.story_fragment, container, false);
Scheduler.getInstance().scheduleStoryUpdate(story);
gridView = getViewGrid();
gridAdapter = new CustomAdapter();
return rootView;
}
@Override
public void onResume(){
super.onResume();
gridView.setAdapter(gridAdapter);
}
private CustomGridView getViewGrid(){
return (CustomGridView) rootView.findViewById(R.id.storyFragmentGridView);
}
private class CustomAdapter extends BaseAdapter{
private HashMap<Integer,View> customCash = new HashMap<Integer, View>();
@Override
public int getCount(){
return story.getBlocks().length;
}
@Override
public Object getItem(int position){
return story.getBlocks()[position];
}
@Override
public long getItemId(int position){
return 0;
}
@Override
public View getView(int _i, View _view, ViewGroup viewGroup){
BlockMod _b = (BlockMod)getItem(_i);
if(customCash.containsKey(_i))
return customCash.get(_i);
if(_b.getType().equals(BlockMod.BLOCK_TYPE_NOTE))
_view = new BlockViewNote(getActivity(),gridView.getColumnWidth(), _b);
if(_b.getType().equals(BlockMod.BLOCK_TYPE_IMAGE))
_view = new BlockViewImage(getActivity(),gridView.getColumnWidth(), _b);
if(_b.getType().equals(BlockMod.BLOCK_TYPE_STATUS))
_view = new BlockViewStatus(getActivity(),gridView.getColumnWidth());
customCash.put(_i,_view);
return _view;
}
}
}
CustomGridView:
public class CustomGridView extends GridView{
public CustomGridView(Context context){
super(context);
}
public CustomGridView(Context context, AttributeSet attrs){
super(context, attrs);
}
public CustomGridView(Context context, AttributeSet attrs, int defStyle){
super(context, attrs, defStyle);
}
@SuppressLint("NewApi")
@Override
public int getColumnWidth(){
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) return super.getColumnWidth();
else{
try{
Field field = GridView.class.getDeclaredField("mColumnWidth");
field.setAccessible(true);
Integer value = (Integer) field.get(this);
field.setAccessible(false);
return value.intValue();
}catch(NoSuchFieldException e){
throw new RuntimeException(e);
}catch(IllegalAccessException e){
throw new RuntimeException(e);
}
}
}
}
答案 0 :(得分:0)
这可能是因为您没有在onCreateView中设置适配器吗?
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
rootView = inflater.inflate(R.layout.story_fragment, container, false);
Scheduler.getInstance().scheduleStoryUpdate(story);
gridView = getViewGrid();
gridAdapter = new CustomAdapter();
gridView.setAdapter(gridAdapter);
return rootView;
}