Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to setContentView in a fragment?

Now I've got this fragment which i want to use setContentView with but so far i cant find how. You can see my case in the code below, im not trying to inflate a layout, im trying to use it with the view called SampleView. So how can I do that? Thanks in advance

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;         } } } 
like image 319
lamp ard Avatar asked Aug 24 '12 11:08

lamp ard


People also ask

What is SetContentView () used for?

SetContentView is used to fill the window with the UI provided from layout file incase of setContentView(R. layout. somae_file). Here layoutfile is inflated to view and added to the Activity context(Window).

How do I start an intent from a fragment?

If you want to start a new instance of mFragmentFavorite , you can do so via an Intent . Intent intent = new Intent(this, mFragmentFavorite. class); startActivity(intent);

What is FrameLayout in fragment?

FrameLayout is designed to block out an area on the screen to display a single item. Generally, FrameLayout should be used to hold a single child view, because it can be difficult to organize child views in a way that's scalable to different screen sizes without the children overlapping each other.

What are the methods you can override in the fragment class?

The Fragment class has two callback methods, onAttach() and onDetach() , that you can override to perform work when either of these events occur.


2 Answers

You dont call setContentView in fragments, in fact you need to return a View from onCreateView.

Try replacing:

setContentView(new SampleView(this)); 

With this:

return new SampleView(this); 
like image 191
waqaslam Avatar answered Sep 20 '22 17:09

waqaslam


Return the view instance you want to use:

    @Override     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {         return inflater.inflate(R.layout.ads_tab, container, false);     } 
like image 42
bjorncs Avatar answered Sep 23 '22 17:09

bjorncs