【问题标题】:How to correctly replace a fragment in a view pager?如何正确替换视图寻呼机中的片段?
【发布时间】:2012-01-27 09:35:28
【问题描述】:

我的应用具有以下结构:

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

我正在使用ActionBarSherlock(我非常推荐),Activity 的结构基于https://github.com/JakeWharton/ActionBarSherlock/blob/master/samples/demos/src/com/actionbarsherlock/sample/demos/app/ActionBarTabsPager.java 的演示。因此,这两个片段显示为用户的两个选项卡。

当用户单击 ListFragment 的元素时,我想在 WebView 中加载与列表相同的位置的 url。也就是我想用新的WebView替换ListFragment(放到后栈)。

到目前为止,我已经尝试使用 Activity 中的FragmentTransaction.replace()。那种工作,除了两个问题:

  • 在我旋转设备(即重新创建活动)之前,ListFragment 不显示 WebView。
  • 另一个选项卡的内容消失(只是空白)

用另一个 Fragment 替换 ListFragment 的正确方法是什么?

【问题讨论】:

    标签: android android-fragments android-viewpager actionbarsherlock


    【解决方案1】:

    或者,您可以使用自定义 HorizontalScrollView 而不是 ViewPager 并覆盖其 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,并使用常规FragmentTransactionaddToBackStack() 来获得您想要的。

    注意:如果你想使用上面的代码,只需确保你的 Fragments 与整个屏幕的宽度相同,并记住你的结构应该是这样的

    MyHorizontalScrollView > LinearLayout > YourFragment1
                                          > YourFragment2
    

    【讨论】:

    • 我喜欢这个解决方案。它很容易实现,并且没有用变通方法弄乱代码。在这种特殊情况下,我在 WebView 中横向滚动触发链接点击时遇到了一些问题。
    【解决方案2】:

    作为一种解决方法,我已经完成了 AndroidTeam At Mail.RuconnoisseurReplace Fragment inside a ViewPager 上建议的答案。这当然是解决这个问题的一个非常糟糕的方法。我现在觉得有点脏。 :-S

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-27
      相关资源
      最近更新 更多