2

I have the following structure of my app:

v4 FragmentActivity --> v4 ViewPager --> v4 Fragment
                                     --> v4 ListFragment

I'm using ActionBarSherlock (which I would really recommend), and the structure of the Activity is based on the demo at https://github.com/JakeWharton/ActionBarSherlock/blob/master/samples/demos/src/com/actionbarsherlock/sample/demos/app/ActionBarTabsPager.java . So the two fragments are displayed as two tabs for the user.

When a user clicks an element of the ListFragment I want to load an url in a WebView in the same place as the list is. That is, I want to replace the ListFragment (put it on the back stack) with a new WebView.

So far I've tried using FragmentTransaction.replace() from the Activity. That kind of works, except two issues:

  • The ListFragment doesn't display the WebView before I rotate the device (i.e. the acitity is recreated).
  • The content of the other tab disappears (it's just blank)

What is the correct way to replace the ListFragment with another Fragment?

4

2 回答 2

1

或者,您可以使用自定义HorizontalScrollView而不是 aViewPager并覆盖其onTouchEvent方法以获得与 ViewPager 相同的捕捉效果,如下所示:

public class MyHorizontalScrollView extends HorizontalScrollView {

    public MyHorizontalScrollView(Context context) {
        super(context);
    }

    public MyHorizontalScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyHorizontalScrollView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }   

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
           case MotionEvent.ACTION_UP:
                    if (mScrollable) {
                        View child = getChildAt(0);
                        if (child != null) {
                            final int currentX = getScrollX();
                            final int windowWidth = getResources().getDisplayMetrics().widthPixels;
                            final int totalWidth = child.getWidth();
                            int showingElementNumber = 1;
                            int i = windowWidth;
                            while (i < currentX && i < totalWidth) {
                                i+=windowWidth;
                                showingElementNumber++;
                            }

                            int scrollTo = 0;
                            if (currentX < (windowWidth * (showingElementNumber - 1) + windowWidth/2)) { // Previouses widths + half the current
                                scrollTo = windowWidth * (showingElementNumber - 1);
                            } else {
                                scrollTo = windowWidth * showingElementNumber + marginSize; 
                            }
                            smoothScrollTo(scrollTo, 0);
                            return false;

                        }
                    }
                    return super.onTouchEvent(ev);
            default:
                return super.onTouchEvent(ev);
        }
    }
}

然后你可以在Fragments其中添加两个并使用常规FragmentTransactionwithaddToBackStack()来获得你想要的。

注意:如果您想使用上面的代码,只需确保您的片段与整个屏幕的宽度相同,并记住您的结构应该类似于

MyHorizontalScrollView > LinearLayout > YourFragment1
                                      > YourFragment2
于 2012-01-28T10:09:22.100 回答
0

As a workaround I've done what AndroidTeam At Mail.Ru and connoisseur suggest as answers on Replace Fragment inside a ViewPager. This is of course a pretty bad way solving this. I feel kinda dirty now. :-S

于 2012-01-27T20:52:52.230 回答