0

I try to put a SurfaceView on top of a RecyclerView using FrameLayout (wow that was the title :D). But even if I do not draw at all in the SurfaceView the screen gets black and I can not see the RecyclerView at all.

The three initializes in my MainActivity:

My Surface View:

surfaceView = new SurView(this, screenX, screenY);

My RecyclerView:

RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);

for (int i = 0; i < 100; i++) {
                input.add("Test" + i);
}

mAdapter = new RealFarmAdapter(input);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(mAdapter);

if (recyclerView != null) {
    ViewGroup parent = (ViewGroup) recyclerView.getParent();
    if (parent != null) {
        parent.removeView(recyclerView);
    }
}

My FrameLayout:

FrameLayout frameLayout = new FrameLayout(this);
frameLayout.addView(recyclerView, 0);
frameLayout.addView(surfaceView, 1);
  • 1
    What I really don't understand is why you initialize your RecyclerView with LayoutManager, Adapter and everything and then you remove it from the layout. – Bö macht Blau Feb 13 '18 at 19:32
  • well I dont know much about the recyclerview so far. i just wanna try a standardcode for that until it works and than i try to make it better. –  Feb 13 '18 at 19:35
  • OK, then let's try to make your RecyclerView visible: don't remove it from its parent and set the SurfaceView invisible until you've got the RecyclerView up and running. – Bö macht Blau Feb 13 '18 at 19:37
  • if I do this (dont remove the parent) I get an error when I try to put it into the frameLayout: "The specified child already has a parent. You must call removeView() on the child's parent first." –  Feb 13 '18 at 19:37
  • When I change the index of the both views (surface = 0 and recycler = 1) i see the recycler and it works fine (but this is not my intention) –  Feb 13 '18 at 19:38
  • I'm beginning to understand how your screen is set up. (But I still would stick to xml-only layouts if possible). Maybe [this post](https://stackoverflow.com/questions/5391089/how-to-make-surfaceview-transparent) is helpful? – Bö macht Blau Feb 13 '18 at 19:44
  • Well, I never really worked with layouts in android so far, which is the reason I did not even thought about a surfaceview in a layout file. You mean it would be a good idea to do change my framelayout to a layout.xml file? –  Feb 13 '18 at 19:53
  • I think it would be a very good idea to use a xml layout file if the Views in the layout do not change much at runtime and if the number of Views is limited - I never would use an xml layout with 64 Views for a game of chess. – Bö macht Blau Feb 13 '18 at 19:57
  • what do you mean of "change much at runtime"? When i draw on the surfaceview or when I add a new layout (whatever this actually means) and the post actually helped a bit. at least i can see the recyclerview now, but I do not like it very much. –  Feb 13 '18 at 20:02
  • "change much" like "number and type of Views you want to show vary much". This [link to documentation](https://developer.android.com/guide/topics/ui/declaring-layout.html) is meant as an entry point - there are tons of layout-related questions on Stack Overflow – Bö macht Blau Feb 13 '18 at 20:08
  • I will take look in it. Thank you for your help. I have one more question: Right now when i draw something on my surfaceview i will never be able to see the RecyclerView on that point again, can I solve this by doing the layouts in a file? or should I write my own class to draw the list in the surfaceview where i can manipulate everything as I want it? –  Feb 13 '18 at 20:28
  • 1
    Visibility of Views is not influenced by defining the layout in a xml file vs. setting things up programmatically, so the answer is "no". The second option sounds awfully complicated. And what would you gain? The main problem seems to be that the user can't look past opaque areas on the screen. So maybe it's not a good idea to have the SurfaceView cover the RecyclerView? Sorry, I don't really know your use case. – Bö macht Blau Feb 13 '18 at 20:35
  • 1
    Ok, thank you again for your help. I will figure something out i think. :D Have a good day. –  Feb 13 '18 at 20:40

0 Answers0