0

I'm trying to implement a Gallery that every cell is a fragment.

gallery_item.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" />

getView() of GalleryAdapter (extends 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;
}

It's working, but the problem is that the gallery called getView() in infinite loop.

It's look like a onMeasure issue, here is the stack:

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]  

10x a lot!

David
  • 37,109
  • 32
  • 120
  • 141
  • 1
    Fragments aren't designed to be used as children in views that recycle them. I advise you to rethink your current logic and avoid using fragments as children for that gallery(which you should also avoid as it's deprecated). – user May 06 '13 at 07:45
  • 10x @Luksprog .I know that Gallery is deprecated, but still can't find any alternative for the effect of Gallery that shows part of the items from left and right, do you have any suggestions? – David May 06 '13 at 09:19
  • Unfortunately I don't know a library to recommend as a gallery replacement. – user May 06 '13 at 12:49

1 Answers1

0

A single column GridView can be used as a Gallery substitute