mardi 12 août 2014

Android - affichage ListView Image chargeur universel en Fragments ne fonctionne pas - Stack Overflow


I am trying to make a ListView using the Universal Image Loader inside a fragment using the SherlockNavigationDrawer


Problem: The Universal Image Loader works well but only displays a single list item


InitialFragment.java


public class InitialFragment extends SherlockFragment
{

DisplayImageOptions options;
String[] imageUrls;

protected AbsListView listView;

public static ImageLoader imageLoader = ImageLoader.getInstance();


public static Fragment newInstance()
{
Fragment f = new InitialFragment();
return f;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View simpleFragmentView = inflater.inflate(R.layout.fragment_menu1, container, false);

imageUrls = com.sherlock.test1.listview.Constants.IMAGES;

options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.ic_stub)
.showImageForEmptyUri(R.drawable.ic_empty)
.showImageOnFail(R.drawable.ic_error)
.cacheInMemory(true)
.cacheOnDisc(true)
.considerExifParams(true)
.displayer(new RoundedBitmapDisplayer(20))
.build();


listView = (ListView) simpleFragmentView.findViewById(R.id.list2);
((ListView) listView).setAdapter(new ItemAdapter(getActivity()));
listView.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
Toast.makeText(getActivity(), "You Clicked at " + position, Toast.LENGTH_SHORT).show();
//startImagePagerActivity(position);
}
});


initImageLoader(getActivity());

return simpleFragmentView;

}

public static void initImageLoader(Context context)
{
// This configuration tuning is custom. You can tune every option, you may tune some of them,
// or you can create default configuration by
// ImageLoaderConfiguration.createDefault(this);
// method.

ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
.threadPriority(Thread.NORM_PRIORITY - 2)
.denyCacheImageMultipleSizesInMemory()
.discCacheFileNameGenerator(new Md5FileNameGenerator())
.tasksProcessingOrder(QueueProcessingType.LIFO)
.writeDebugLogs() // Remove for release app
.build();
// Initialize ImageLoader with configuration.
ImageLoader.getInstance().init(config);
}

public class ItemAdapter extends BaseAdapter
{
Context context;
ItemAdapter(Context context)
{
Log.d("dexter", "in ItemAdapter Constructor");
this.context = context;
}
private ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener();

public class ViewHolder
{
public TextView text;
public ImageView image;
}

@Override
public int getCount()
{
return imageUrls.length;
}

@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)
{
Log.d("dexter", "in getView");
View view = convertView;
final ViewHolder holder;
if (convertView == null)
{
view = LayoutInflater.from(context).inflate(R.layout.item_list_image, parent, false);
//view = getActivity().getLayoutInflater().inflate(R.layout.item_list_image, parent, false);
holder = new ViewHolder();
holder.text = (TextView) view.findViewById(R.id.text);
holder.image = (ImageView) view.findViewById(R.id.image);
view.setTag(holder);
}
else
{
holder = (ViewHolder) view.getTag();
}

holder.text.setText("Item " + (position + 1));



InitialFragment.imageLoader.displayImage(imageUrls[position], holder.image, options, animateFirstListener);

return view;
}
}

public static class AnimateFirstDisplayListener extends SimpleImageLoadingListener
{
static final List<String> displayedImages = Collections.synchronizedList(new LinkedList<String>());

@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage)
{
if (loadedImage != null)
{
ImageView imageView = (ImageView) view;
boolean firstDisplay = !displayedImages.contains(imageUri);
if (firstDisplay)
{
FadeInBitmapDisplayer.animate(imageView, 500);
displayedImages.add(imageUri);
}
}
}
}

}


fragment_menu1.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearlayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:orientation="vertical" >

<ListView
android:id="@+id/list2"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>


If this helps:


1. The InitialFragment is a Child Fragment and is inflated in a FrameLayout at runtime


2. This code works perfectly well when tried within an Activity (without any fragments)


3. The ItemAdapter's getView() gets called only once




Problem solved, Hadn’t added android:fillViewport:”true” in the ScrollView


<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"

>
<!-- As the main content view, the view below consumes the entire
space available using match_parent in both dimensions. -->
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbarStyle="outsideOverlay"
android:fillViewport="true"
>


<FrameLayout
android:id="@+id/child_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"

/>

</ScrollView>


<!-- android:layout_gravity="left" tells DrawerLayout to treat
this as a sliding drawer on the left side. The drawer is
given a fixed width in dp and extends the full height of
the container. A solid background is used for contrast
with the content view. -->

<ListView android:id="@+id/left_drawer"
android:layout_width="300dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:background="@android:color/white"/>



I am trying to make a ListView using the Universal Image Loader inside a fragment using the SherlockNavigationDrawer


Problem: The Universal Image Loader works well but only displays a single list item


InitialFragment.java


public class InitialFragment extends SherlockFragment
{

DisplayImageOptions options;
String[] imageUrls;

protected AbsListView listView;

public static ImageLoader imageLoader = ImageLoader.getInstance();


public static Fragment newInstance()
{
Fragment f = new InitialFragment();
return f;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View simpleFragmentView = inflater.inflate(R.layout.fragment_menu1, container, false);

imageUrls = com.sherlock.test1.listview.Constants.IMAGES;

options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.ic_stub)
.showImageForEmptyUri(R.drawable.ic_empty)
.showImageOnFail(R.drawable.ic_error)
.cacheInMemory(true)
.cacheOnDisc(true)
.considerExifParams(true)
.displayer(new RoundedBitmapDisplayer(20))
.build();


listView = (ListView) simpleFragmentView.findViewById(R.id.list2);
((ListView) listView).setAdapter(new ItemAdapter(getActivity()));
listView.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
Toast.makeText(getActivity(), "You Clicked at " + position, Toast.LENGTH_SHORT).show();
//startImagePagerActivity(position);
}
});


initImageLoader(getActivity());

return simpleFragmentView;

}

public static void initImageLoader(Context context)
{
// This configuration tuning is custom. You can tune every option, you may tune some of them,
// or you can create default configuration by
// ImageLoaderConfiguration.createDefault(this);
// method.

ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
.threadPriority(Thread.NORM_PRIORITY - 2)
.denyCacheImageMultipleSizesInMemory()
.discCacheFileNameGenerator(new Md5FileNameGenerator())
.tasksProcessingOrder(QueueProcessingType.LIFO)
.writeDebugLogs() // Remove for release app
.build();
// Initialize ImageLoader with configuration.
ImageLoader.getInstance().init(config);
}

public class ItemAdapter extends BaseAdapter
{
Context context;
ItemAdapter(Context context)
{
Log.d("dexter", "in ItemAdapter Constructor");
this.context = context;
}
private ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener();

public class ViewHolder
{
public TextView text;
public ImageView image;
}

@Override
public int getCount()
{
return imageUrls.length;
}

@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)
{
Log.d("dexter", "in getView");
View view = convertView;
final ViewHolder holder;
if (convertView == null)
{
view = LayoutInflater.from(context).inflate(R.layout.item_list_image, parent, false);
//view = getActivity().getLayoutInflater().inflate(R.layout.item_list_image, parent, false);
holder = new ViewHolder();
holder.text = (TextView) view.findViewById(R.id.text);
holder.image = (ImageView) view.findViewById(R.id.image);
view.setTag(holder);
}
else
{
holder = (ViewHolder) view.getTag();
}

holder.text.setText("Item " + (position + 1));



InitialFragment.imageLoader.displayImage(imageUrls[position], holder.image, options, animateFirstListener);

return view;
}
}

public static class AnimateFirstDisplayListener extends SimpleImageLoadingListener
{
static final List<String> displayedImages = Collections.synchronizedList(new LinkedList<String>());

@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage)
{
if (loadedImage != null)
{
ImageView imageView = (ImageView) view;
boolean firstDisplay = !displayedImages.contains(imageUri);
if (firstDisplay)
{
FadeInBitmapDisplayer.animate(imageView, 500);
displayedImages.add(imageUri);
}
}
}
}

}


fragment_menu1.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearlayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:orientation="vertical" >

<ListView
android:id="@+id/list2"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>


If this helps:


1. The InitialFragment is a Child Fragment and is inflated in a FrameLayout at runtime


2. This code works perfectly well when tried within an Activity (without any fragments)


3. The ItemAdapter's getView() gets called only once



Problem solved, Hadn’t added android:fillViewport:”true” in the ScrollView


<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"

>
<!-- As the main content view, the view below consumes the entire
space available using match_parent in both dimensions. -->
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbarStyle="outsideOverlay"
android:fillViewport="true"
>


<FrameLayout
android:id="@+id/child_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"

/>

</ScrollView>


<!-- android:layout_gravity="left" tells DrawerLayout to treat
this as a sliding drawer on the left side. The drawer is
given a fixed width in dp and extends the full height of
the container. A solid background is used for contrast
with the content view. -->

<ListView android:id="@+id/left_drawer"
android:layout_width="300dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:background="@android:color/white"/>


0 commentaires:

Enregistrer un commentaire