我正在尝试使用Universal Image Loader为喜欢的图片创建网格视图。目前,没有图像出现或加载。同样,我的调试也发现只调用getView = position == 0.请帮助!
下面我列出:
创建虚拟arraylist和自定义适配器的片段代码片段
片段的
使用通用图像加载器的自定义适配器和一个视图持有者,其中包含一个带有imageView的relativelayout。
gridview / viewholder项目的xml布局文件。
public class FavoritesFragment extends Fragment {
private GridView gridView;
private Button imageButton;
private FavoritesImageAdapter favoritesImageAdapter;
...
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayList<String> exampleUrls = new ArrayList<String>(); //Used for testing
exampleUrls.add("http://i.imgur.com/gOD1PVH.jpg");
exampleUrls.add("http://i.imgur.com/HB6wZox.jpg");
exampleUrls.add("http://i.imgur.com/gOD1PVH.jpg");
exampleUrls.add("http://i.imgur.com/HB6wZox.jpg");
exampleUrls.add("http://i.imgur.com/HB6wZox.jpg");
exampleUrls.add("http://i.imgur.com/gOD1PVH.jpg");
favoritesImageAdapter = new FavoritesImageAdapter(getActivity(), 0, exampleUrls);
setHasOptionsMenu(true); // Allows the fragment to change the menu buttons
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_favorites, container, false);
gridView = (GridView) view.findViewById(R.id.gridView);
gridView.setAdapter(favoritesImageAdapter);
getActivity().getActionBar().setTitle("Favorites");
return view;
}
}
fragment_favorites.xml:
<GridView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/gridView"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:numColumns="3"
android:background="@color/blue_200"
android:layout_above="@+id/favoritesButtonLayout"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:paddingBottom="10px"
android:id="@+id/favoritesButtonLayout">
<Space
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageButton
android:layout_weight="10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton"
android:src="@drawable/ic_action_camera"/>
<Space
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageButton
android:layout_weight="10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/videoButton"
android:src="@drawable/ic_action_video"/>
<Space
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/onboardingFavoritesImageView"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
FavoritesImageAdapter.java:
公共类FavoritesImageAdapter扩展了BaseAdapter {
LayoutInflater layoutInflater;
private Context mContext;
private ArrayList<String> imageUrls;
private ImageLoader imageLoader;
DisplayImageOptions options;
public FavoritesImageAdapter(Context context, int resource, ArrayList<String> urls){
layoutInflater = layoutInflater.from(context);
mContext = context;
this.imageUrls = urls;
imageLoader = ImageLoader.getInstance();
options = new DisplayImageOptions.Builder()
.showImageForEmptyUri(R.drawable.ic_empty)
.showImageOnFail(R.drawable.ic_error)
.resetViewBeforeLoading(true)
.cacheOnDisk(true)
.imageScaleType(ImageScaleType.EXACTLY)
.bitmapConfig(Bitmap.Config.RGB_565)
.considerExifParams(true)
.displayer(new FadeInBitmapDisplayer(300))
.build();
}
@Override
public int getCount() {
Log.d("howmany", ""+imageUrls.size());
return imageUrls.size();
}
@Override
public Object getItem(int position) {
return imageUrls.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final FavoritesImageViewHolder favoritesImageViewHolder;
final String url = (String)getItem(position); //TODO If the cast works delete this TODO
if(convertView == null){
RelativeLayout rootView = (RelativeLayout) layoutInflater.inflate(R.layout.item_grid_favorites_image, parent, false);
favoritesImageViewHolder = FavoritesImageViewHolder.create(rootView);
rootView.setTag(favoritesImageViewHolder);
}
else{
favoritesImageViewHolder = (FavoritesImageViewHolder) convertView.getTag();
}
imageLoader.displayImage((String)getItem(position), favoritesImageViewHolder.imageView, options, new SimpleImageLoadingListener() {//TODO If the cast works delete this TODO
@Override
public void onLoadingStarted(String imageUri, View view) {
//holder.progressBar.setProgress(0);
//holder.progressBar.setVisibility(View.VISIBLE);
Log.d("doesthishappen", "yes0");
}
@Override
public void onLoadingFailed(String imageUri, View view,
FailReason failReason) {
//holder.progressBar.setVisibility(View.GONE);
Log.d("doesthishappen", "yes1");
}
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
//holder.progressBar.setVisibility(View.GONE);
Log.d("doesthishappen", "yes2");
}
}, new ImageLoadingProgressListener() {
@Override
public void onProgressUpdate(String imageUri, View view, int current,
int total) {
//holder.progressBar.setProgress(Math.round(100.0f * current / total));
}
}
);
return favoritesImageViewHolder.rootView;
}
/**
* ViewHolder's allow for a single object to maintain a Goal row item, so that the row item
* doesn't have to create each individual component (textview layout etc.) each time the
* row object is created/recreated. Allows for fast scrolling with little latency.
*/
private static class FavoritesImageViewHolder {
public final RelativeLayout rootView;
public final ImageView imageView;
private FavoritesImageViewHolder(RelativeLayout rootView, ImageView imageView) {
this.rootView = rootView;
this.imageView = imageView;
}
public static FavoritesImageViewHolder create(RelativeLayout rootView){
ImageView imageView = (ImageView)rootView.findViewById(R.id.favoritesMediaView);
return new FavoritesImageViewHolder(rootView, imageView);
}
}
}
item_grid_favorites_image.xml:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/favoritesMediaView"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
我不确定为什么会这样。我知道所有6个假网址都被传递给适配器,我知道getView方法被调用了6次。我也知道Universal Image Loader永远不会到达onLoadingComplete()回调方法,但它会调用onLoadingStarted()6次。所有这6次使用arraylist的位置0。请帮助,我希望通过这个侧面项目在android上变得更好。另外,我为开头的代码格式错误道歉。我试图解决它,但它不起作用。
答案 0 :(得分:1)
@eduardowarded:我观察的是你使用Holder pattern进行快速滚动。我可以建议你在FavoritesImageViewHolder中添加新属性作为int位置。在getVirew方法中,您必须分配favoritesImageViewHolder.position = position。要访问位置,您应该使用favoritesImageViewHolder.position。
出现这种类型的问题是因为在滚动列表getView
时会调用它并且它不同步,这会导致线程更新值。
我希望这可以帮助您解决问题。
答案 1 :(得分:1)
希望它可以帮到你
if(convertView == null){
RelativeLayout rootView = (RelativeLayout) layoutInflater.inflate(R.layout.item_grid_favorites_image, parent, false);
favoritesImageViewHolder = FavoritesImageViewHolder.create(rootView);
rootView.setTag(favoritesImageViewHolder);
//add this code to your adapter
favoritesImageViewHolder.imageView.setTag(position);
}
答案 2 :(得分:1)
我发现了答案。我应该有一个与这个基本相同的item_grid_favorites_image.xml:https://github.com/nostra13/Android-Universal-Image-Loader/blob/master/sample/res/layout/item_grid_image.xml
如果您想了解其工作原理:
之前,我使用的是item_grid_favorites_image.xml,其相对布局具有fill_parent高度,这导致通用图像加载器永远需要加载图片。此外,这就是为什么onLoadingStarted()被调用了6次,但是onLoadingComplete()被调用了0次。它刚刚加载了加载时间。
TIL我是个白痴