我是一名能够提供翻译的助手。
我有一个布局,其中包括一个 ViewPager 和其他简单的视图。我将此布局用作 ListView 的列表项,它可以正常工作。但是当我尝试实现 ViewHolder 模式时,当我向下滚动时应用程序会崩溃。错误发生在将适配器设置为视图页时。
适配器没有使用 ViewHolder 实现:
public class FaceInSandAdapter extends ArrayAdapter<Gem> {
private Context mContext;
public FaceInSandAdapter(Context c, ArrayList<Gem> gems) {
super(c, 0, gems);
mContext = c;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final Gem gem = getItem(position);
convertView = ((Activity) mContext).getLayoutInflater().inflate(
R.layout.list_item_gem, null);
final ViewPager pager = (ViewPager) convertView
.findViewById(R.id.list_item_gem_photosViewPager);
pager.setId(gem.getId());
pager.setAdapter(new PhotoSliderPagerAdapter(((FragmentActivity) parent
.getContext()).getSupportFragmentManager(), gem.getImageUrls()));
TextView address = (TextView) convertView
.findViewById(R.id.list_item_gem_addressTextView);
address.setText(gem.getAddress());
return convertView;
}
}
如何正确实现ViewHolder模式,用于适配器(adapter)中的ViewPager所填充的布局?以下是我尝试实现这样一个适配器的代码,但会产生上述提到的错误:
public class FaceInSandAdapter extends ArrayAdapter<Gem> {
private Context mContext;
public FaceInSandAdapter(Context c, ArrayList<Gem> gems) {
super(c, 0, ads);
mContext = c;
}
static class ViewHolder {
ViewPager pager;
TextView address;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
final Gem gem = getItem(position);
if (convertView == null) {
convertView = ((Activity) mContext).getLayoutInflater().inflate(
R.layout.list_item_gem, null);
holder = new ViewHolder();
holder.pager = (ViewPager) convertView
.findViewById(R.id.list_item_gem_photosViewPager);
holder.address = (TextView) convertView
.findViewById(R.id.list_item_gem_addressTextView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
FragmentManager fm = ((FragmentActivity) convertView.getContext())
.getSupportFragmentManager();
holder.pager.setId(gem.getId());
holder.pager.setAdapter(new PhotoSliderPagerAdapter(fm, gem
.getImageUrls()));
holder.address.setText(gem.getAddress());
return convertView;
}
}
编辑
我在实现ViewHolder模式时遇到错误,以下是错误的堆栈跟踪信息:
FATAL EXCEPTION: main
Process: xx.yyy.zzzzzz, PID: 5010
android.content.res.Resources$NotFoundException: Unable to find resource ID #0x3fe1
at android.content.res.Resources.getResourceName(Resources.java:1776)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:919)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
at android.support.v4.view.ViewPager.setAdapter(ViewPager.java:442)
at xxx.xxx.xxx.FaceInSandAdapter.getView(FaceInSandAdapter.java:89)
at android.widget.AbsListView.obtainView(AbsListView.java:2240)
at android.widget.ListView.makeAndAddView(ListView.java:1790)
at android.widget.ListView.fillDown(ListView.java:691)
at android.widget.ListView.fillGap(ListView.java:655)
at android.widget.AbsListView.trackMotionScroll(AbsListView.java:5136)
at android.widget.AbsListView$FlingRunnable.run(AbsListView.java:4247)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
at android.view.Choreographer.doCallbacks(Choreographer.java:574)
at android.view.Choreographer.doFrame(Choreographer.java:543)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5102)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
**编辑2**
现在,由于viewpager作为listview的一个item正在工作,剩下的问题是我正在实例化ImageViews,并且PagerAdapter只有最基本的实现(没有保存状态,没有限制离屏视图),因此列表最终使用了大量的内存。