我正在尝试使用Universal Image Loader
在片段内使用SherlockNavigationDrawer制作ListView问题:通用图片加载器运行良好,但只显示一个列表项
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>
如果这有帮助:
1。 InitialFragment是一个子片段,在运行时在FrameLayout中膨胀
2。此代码在Activity中尝试时效果非常好(没有任何片段)
3。 ItemAdapter的getView()只被调用一次
答案 0 :(得分:0)
问题解决了,没有在ScrollView中添加android:fillViewport:“true”
<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"/>