我正在尝试实现一个画廊,每个单元格都是一个片段。
画廊项目.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/cardContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/img1" />
GalleryAdapter 的 getView()(扩展 BaseAdapter):
public View getView(int position, View convertView, ViewGroup parent)
{
if (convertView == null)
{
mHolder = new ViewHolder();
mFrameLayout = (FrameLayout) LayoutInflater.from(parent.getContext()).inflate(R.layout.gallery_item, null);
mFrameLayout.setPadding(25,0,25,0);
//Add fragment to frame layout
mFragmentManager.beginTransaction().add(mFrameLayout.getId(), new CardFrontFragment()).commit();
convertView = mFrameLayout;
mHolder.frameLayout = mFrameLayout;
convertView.setTag(mHolder);
}
else
{
mHolder = (ViewHolder) convertView.getTag();
}
mHolder.frameLayout.setLayoutParams(new Gallery.LayoutParams(Gallery.LayoutParams.MATCH_PARENT, Gallery.LayoutParams.MATCH_PARENT));
return mFrameLayout;
}
private static class ViewHolder {
FrameLayout frameLayout;
}
它正在工作,但问题是画廊在无限循环中调用了 getView()。
它看起来像一个 onMeasure 问题,这里是堆栈:
Thread [<1> main] (Suspended (breakpoint at line 62 in GalleryAdapter3))
GalleryAdapter3.getView(int, View, ViewGroup) line: 62
Gallery(AbsSpinner).onMeasure(int, int) line: 193
Gallery(View).measure(int, int) line: 15518
RelativeLayout.measureChildHorizontal(View, RelativeLayout$LayoutParams, int, int) line: 681
RelativeLayout.onMeasure(int, int) line: 461
RelativeLayout(View).measure(int, int) line: 15518
FrameLayout(ViewGroup).measureChildWithMargins(View, int, int, int, int) line: 4825
FrameLayout.onMeasure(int, int) line: 310
FrameLayout(View).measure(int, int) line: 15518
LinearLayout.measureVertical(int, int) line: 847
LinearLayout.onMeasure(int, int) line: 588
LinearLayout(View).measure(int, int) line: 15518
PhoneWindow$DecorView(ViewGroup).measureChildWithMargins(View, int, int, int, int) line: 4825
PhoneWindow$DecorView(FrameLayout).onMeasure(int, int) line: 310
PhoneWindow$DecorView.onMeasure(int, int) line: 2176
PhoneWindow$DecorView(View).measure(int, int) line: 15518
ViewRootImpl.performMeasure(int, int) line: 1874
ViewRootImpl.measureHierarchy(View, WindowManager$LayoutParams, Resources, int, int) line: 1089
ViewRootImpl.performTraversals() line: 1265
ViewRootImpl.doTraversal() line: 989
ViewRootImpl$TraversalRunnable.run() line: 4351
Choreographer$CallbackRecord.run(long) line: 749
Choreographer.doCallbacks(int, long) line: 562
Choreographer.doFrame(long, int) line: 532
Choreographer$FrameDisplayEventReceiver.run() line: 735
Handler.handleCallback(Message) line: 725
Choreographer$FrameHandler(Handler).dispatchMessage(Message) line: 92
Looper.loop() line: 137
ActivityThread.main(String[]) line: 5041
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]
Method.invoke(Object, Object...) line: 511
ZygoteInit$MethodAndArgsCaller.run() line: 793
ZygoteInit.main(String[]) line: 560
NativeStart.main(String[]) line: not available [native method]
10倍很多!