【问题标题】:How to setContentView in a fragment?如何在片段中设置内容视图?
【发布时间】:2012-08-24 11:17:09
【问题描述】:

现在我有这个片段,我想使用 setContentView,但到目前为止我找不到方法。您可以在下面的代码中看到我的案例,我没有尝试扩展布局,而是尝试将其与名为 SampleView 的视图一起使用。那么我该怎么做呢? 提前致谢

public class largeImageScroller extends SherlockFragment {

// Physical display width and height.
private static int displayWidth = 0;
private static int displayHeight = 0;

/** Called when the activity is first created. */
public View onCreateView(LayoutInflater inflater, ViewGroup group, Bundle saved) {

        getActivity();
        // displayWidth and displayHeight will change depending on screen
        // orientation. To get these dynamically, we should hook onSizeChanged().
        // This simple example uses only landscape mode, so it's ok to get them
        // once on startup and use those values throughout.

        Display display = ((WindowManager)
getActivity().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        displayWidth = display.getWidth();             
        displayHeight = display.getHeight();    

        // SampleView constructor must be constructed last as it needs the
        // displayWidth and displayHeight we just got.
        setContentView(new SampleView(this));
}

private static class SampleView extends View {
        private static Bitmap bmLargeImage; //bitmap large enough to be scrolled
        private static Rect displayRect = null; //rect we display to
        private Rect scrollRect = null; //rect we scroll over our bitmap with
        private int scrollRectX = 0; //current left location of scroll rect
        private int scrollRectY = 0; //current top location of scroll rect
        private float scrollByX = 0; //x amount to scroll by
        private float scrollByY = 0; //y amount to scroll by
        private float startX = 0; //track x from one ACTION_MOVE to the next
        private float startY = 0; //track y from one ACTION_MOVE to the next

        public SampleView(Context context) {
                super(context);

                // Destination rect for our main canvas draw. It never changes.
                displayRect = new Rect(0, 0, displayWidth, displayHeight);
                // Scroll rect: this will be used to 'scroll around' over the
                // bitmap in memory. Initialize as above.
                scrollRect = new Rect(0, 0, displayWidth, displayHeight);

                // Load a large bitmap into an offscreen area of memory.
                bmLargeImage = BitmapFactory.decodeResource(getResources(),
                        R.drawable.ground_floor_b);
        }

        @Override
        public boolean onTouchEvent(MotionEvent event) {

                switch (event.getAction()) {
                        case MotionEvent.ACTION_DOWN:
                                // Remember our initial down event location.
                                startX = event.getRawX();
                                startY = event.getRawY();
                                break;

                        case MotionEvent.ACTION_MOVE:
                                float x = event.getRawX();
                                float y = event.getRawY();
                                // Calculate move update. This will happen many times
                                // during the course of a single movement gesture.
                                scrollByX = x - startX; //move update x increment
                                scrollByY = y - startY; //move update y increment
                                startX = x; //reset initial values to latest
                                startY = y;
                                invalidate(); //force a redraw
                                break;
                }
                return true; //done with this event so consume it
        }

        @Override
        protected void onDraw(Canvas canvas) {

                // Our move updates are calculated in ACTION_MOVE in the opposite direction
                // from how we want to move the scroll rect. Think of this as dragging to
                // the left being the same as sliding the scroll rect to the right.
                int newScrollRectX = scrollRectX - (int)scrollByX;
                int newScrollRectY = scrollRectY - (int)scrollByY;

                // Don't scroll off the left or right edges of the bitmap.
                if (newScrollRectX < 0)
                        newScrollRectX = 0;
                else if (newScrollRectX > (bmLargeImage.getWidth() - displayWidth))
                        newScrollRectX = (bmLargeImage.getWidth() - displayWidth);

                // Don't scroll off the top or bottom edges of the bitmap.
                if (newScrollRectY < 0)
                        newScrollRectY = 0;
                else if (newScrollRectY > (bmLargeImage.getHeight() - displayHeight))
                        newScrollRectY = (bmLargeImage.getHeight() - displayHeight);

                // We have our updated scroll rect coordinates, set them and draw.
                scrollRect.set(newScrollRectX, newScrollRectY,
                        newScrollRectX + displayWidth, newScrollRectY + displayHeight);
                Paint paint = new Paint();
                canvas.drawBitmap(bmLargeImage, scrollRect, displayRect, paint);

                // Reset current scroll coordinates to reflect the latest updates,
                // so we can repeat this update process.
                scrollRectX = newScrollRectX;
                scrollRectY = newScrollRectY;
        }
}
}

【问题讨论】:

    标签: android android-fragments android-view


    【解决方案1】:

    你不要在片段中调用setContentView,实际上你需要从onCreateView返回一个View

    尝试替换:

    setContentView(new SampleView(this));
    

    有了这个:

    return new SampleView(this);
    

    【讨论】:

    • 对我来说,我不得不像return new SampleView(getActivity());
    【解决方案2】:

    返回你要使用的视图实例:

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            return inflater.inflate(R.layout.ads_tab, container, false);
        }
    

    【讨论】:

      【解决方案3】:

      此外,从onCreateView() 调用getActivity() 也不安全。 确保在onActivityCreated() 或之后调用它,因为此时您的FragmentActivity 完全关联。检查Fragment 的生命周期。

      Fragments

      【讨论】:

      • 文档确实说要小心并等待附加片段。但是,我没有看到它提到这意味着等待创建活动(即它的 onCreate 已重新调整)。 onAttach 恕我直言应该没问题(尽管我对 Android 开发相当陌生)并且它在 onCreateView 之前被调用。我错过了什么吗?
      • 另一方面,一些(官方)示例表明您是对的。我想这样会更好。
      【解决方案4】:

      如前所述,您需要在片段的情况下返回视图。 但是如果你想像setContentView()一样使用它,你可以通过以下方式来使用它。

      1.把这段代码sn-p放在你必须放setContentView()的地方

      View v = inflater.inflate(R.layout.activity_home, container, false);
      

      2.现在如果你想从 xml 文件中访问某些东西,你可以使用

      chart = v.findViewById(R.id.chart);
      

      3。在OnCreateView() 的末尾,你必须把

      return v;
      

      完整示例:

        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
      
      
          View v = inflater.inflate(R.layout.activity_home, container, false);
      
          chart = v.findViewById(R.id.chart);
      
          return v;
         }
      

      【讨论】:

        【解决方案5】:

        在活动中我们需要使用setContentView(R.layout.main)设置视图

        在片段中,我们需要覆盖 onCreateView() 以设置所需的视图。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-08-12
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多