我有一个listview,每行都有viewpager。
<android.support.v4.view.ViewPager
android:id="@+id/csp_main_imageview_pager"
android:layout_width="fill_parent"
android:layout_height="@dimen/image_max_height"
android:layout_marginBottom="@dimen/default_small"
android:layout_marginLeft="@dimen/default_small"
android:layout_marginRight="@dimen/default_small" />
这是每行xml的一部分。
@Override
public View getView(int position, View convertView, ViewGroup parent){
if (convertView == null){
convertView = inflater.inflate(R.layout.container_post, null);
}
...
ViewPager imageViewPager = (ViewPager)convertView
.findViewById(R.id.main_imageview_pager);
imageViewPager.setId(position + getCount());
if (row should have image){
imageViewPager.setVisibility(View.VISIBLE);
imageViewPager.setAdapter(new ImageAdapter(getActivity()
.getSupportFragmentManager(),
post.getPhotos()));
} else {
imageViewPager.setVisibility(View.GONE);
}
return convertView;
}
这是获取listview行的代码的一部分
private class ImageAdapter extends FragmentStatePagerAdapter {
ArrayList<String> photos;
public ImageAdapter(FragmentManager fm, ArrayList<String> photos) {
super(fm);
this.photos = photos;
}
@Override
public Fragment getItem(int i) {
Fragment fragment = new ImageFragment();
Bundle args = new Bundle();
args.putString(ImageFragment.ARG_IMAGE_LINK, Requests.SERVER_ADDRESS + photos.get(i));
fragment.setArguments(args);
return fragment;
}
@Override
public int getCount() {
return photos.size();
}
@Override
public CharSequence getPageTitle(int position) {
return "";
}
}
这是我用于viewpager的适配器。
ImageFragment只是膨胀视图,它只是imageview并将图像加载到其上,并添加了一些onclicklistener。
我尝试了在谷歌上搜索过的几个代码,但每个代码都创建了至少一个例外。
当我删除
if (convertView == null)
条件,它工作正常,但滚动非常不平滑, 我正在尝试重用膨胀的视图。 有什么建议吗?
编辑:
07-27 16:54:21.379 29762-29762/com.onthego.onthego E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.onthego.onthego, PID: 29762
java.lang.NullPointerException
at com.onthego.onthego.main.ShareListFragment$ShareListAdapter.getView(ShareListFragment.java:161)
at android.widget.AbsListView.obtainView(AbsListView.java:2712)
at android.widget.ListView.makeAndAddView(ListView.java:1811)
at android.widget.ListView.fillSpecific(ListView.java:1348)
at android.widget.ListView.layoutChildren(ListView.java:1619)
at android.widget.AbsListView.onLayout(AbsListView.java:2563)
at android.view.View.layout(View.java:15762)
at android.view.ViewGroup.layout(ViewGroup.java:4867)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1677)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1531)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1440)
at android.view.View.layout(View.java:15762)
at android.view.ViewGroup.layout(ViewGroup.java:4867)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:15762)
at android.view.ViewGroup.layout(ViewGroup.java:4867)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1677)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1531)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1440)
at android.view.View.layout(View.java:15762)
at android.view.ViewGroup.layout(ViewGroup.java:4867)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1055)
at android.view.View.layout(View.java:15762)
at android.view.ViewGroup.layout(ViewGroup.java:4867)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:15762)
at android.view.ViewGroup.layout(ViewGroup.java:4867)
at android.support.v7.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:502)
at android.view.View.layout(View.java:15762)
at android.view.ViewGroup.layout(ViewGroup.java:4867)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:15762)
at android.view.ViewGroup.layout(ViewGroup.java:4867)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1677)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1531)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1440)
at android.view.View.layout(View.java:15762)
at android.view.ViewGroup.layout(ViewGroup.java:4867)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:15762)
at android.view.ViewGroup.layout(ViewGroup.java:4867)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2356)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2069)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1254)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6637)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:814)
at android.view.Choreographer.doCallbacks(Choreographer.java:614)
at android.view.Choreographer.doFrame(Choreographer.java:584)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:800)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5602)
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:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
这是我滚动列表视图时得到的结果。 和文件的第161行是
imageViewPager.setId(position + getCount());
当我从另一项活动
回来时我得到的另一个例外07-27 16:54:21.379 29762-29762/com.onthego.onthego E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.onthego.onthego, PID: 29762
java.lang.NullPointerException
at com.onthego.onthego.main.ShareListFragment$ShareListAdapter.getView(ShareListFragment.java:161)
at android.widget.AbsListView.obtainView(AbsListView.java:2712)
at android.widget.ListView.makeAndAddView(ListView.java:1811)
at android.widget.ListView.fillSpecific(ListView.java:1348)
at android.widget.ListView.layoutChildren(ListView.java:1619)
at android.widget.AbsListView.onLayout(AbsListView.java:2563)
at android.view.View.layout(View.java:15762)
at android.view.ViewGroup.layout(ViewGroup.java:4867)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1677)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1531)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1440)
at android.view.View.layout(View.java:15762)
at android.view.ViewGroup.layout(ViewGroup.java:4867)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:15762)
at android.view.ViewGroup.layout(ViewGroup.java:4867)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1677)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1531)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1440)
at android.view.View.layout(View.java:15762)
at android.view.ViewGroup.layout(ViewGroup.java:4867)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1055)
at android.view.View.layout(View.java:15762)
at android.view.ViewGroup.layout(ViewGroup.java:4867)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:15762)
at android.view.ViewGroup.layout(ViewGroup.java:4867)
at android.support.v7.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:502)
at android.view.View.layout(View.java:15762)
at android.view.ViewGroup.layout(ViewGroup.java:4867)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:15762)
at android.view.ViewGroup.layout(ViewGroup.java:4867)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1677)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1531)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1440)
at android.view.View.layout(View.java:15762)
at android.view.ViewGroup.layout(ViewGroup.java:4867)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:15762)
at android.view.ViewGroup.layout(ViewGroup.java:4867)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2356)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2069)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1254)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6637)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:814)
at android.view.Choreographer.doCallbacks(Choreographer.java:614)
at android.view.Choreographer.doFrame(Choreographer.java:584)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:800)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5602)
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:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)