Inflating view above RecyclerView in FrameLayout
Asked Answered
S

4

5

On a button click, I am attempting to inflate an EditText above a RecyclerView. But instead of the EditText being added to the top of the layout, thus pushing down the RecyclerView, it is simply being placed on top of the RecyclerView, covering it like so:

enter image description here

You can see that "New Text" is covering the first item of the RecyclerView. How can I get it to inflate above the RecyclerView, and have the RecyclerView pushed down, accommodating the newly added EditText?

Here is the XML of my FrameLayout that I am adding the EditText to:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fab="http://schemas.android.com/apk/res-auto"
android:id="@+id/fragment_my_frame_layout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v7.widget.RecyclerView
    android:id="@+id/my_recycler_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:scrollbars="vertical" />

<com.melnykov.fab.FloatingActionButton
    android:id="@+id/button_floating_action"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|right"
    android:layout_margin="16dp"
    android:src="@drawable/ic_action_new"
    fab:fab_colorNormal="@color/primary_dark"
    fab:fab_colorPressed="@color/primary" />

Here is the onClick method of my FloatingActionButton where I wish to add the EditText above the RecyclerView:

    public void onClick(View v) {
    ViewGroup parent = (ViewGroup) rootView.findViewById(R.id.fragment_my_frame_layout);
    View view = LayoutInflater.from(getActivity()).inflate(R.layout.add_keyword_edittext, parent, true);
}

Here is the XML of the EditText I am adding:

<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/edit_text_above_listview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:background="#00000000"
    android:textCursorDrawable="@null"
    android:imeOptions="actionDone"
    android:singleLine="true"
    android:text="New Text" />
Sapro answered 28/10, 2014 at 23:2 Comment(0)
Z
6

in frame layout everything stack on one another, if you want it to be above the recycler view make your layout LinearLayout and call method LinearLayout.addView(child, index) which index is 0 in your case.

UPDATE

because you have floating button you should make your layout something like this:

<FrameLayout>
   <LinearLayout>
      <EditText/>//witch will be added later by add view
      <RecyclerView/>
   </LinearLayout>
   <Button/>
<FrameLayout>

also if you want the edit text to work as something like header you can use this: https://mcmap.net/q/49781/-is-there-an-addheaderview-equivalent-for-recyclerview

Zincate answered 29/10, 2014 at 6:10 Comment(1)
I ended up using the strategy you linked to, using the edit text as a header: #26531185 Thank you!Sapro
U
1

Is this TextView related to items in RecyclerView such that you want to treat it as an item? If so, add EditText as a new view type to your adapter (to index 0) and call notifyItemInserted(0). In response, RecyclerView will call onBind where you can return your EditText. This way, RecyclerView can nicely animate Views down while fading in EditText. If you can post your adapter, I'm happy to post an example comde.

If you don't want to add it inside the RecyclerView, you have two options: - Set top margin on the RecyclerView or top padding when you add the edit text. - Use a linear layout instead of a frame layout and use TransitionManager to animate.

I still think you should consider adding it as an item to your adapter so that LayoutManager can handle focus changes etc. (and it also moves w/ the rest of the list)

Unimposing answered 29/10, 2014 at 4:27 Comment(0)
S
0

This worked for me

<RelativeLayout>    
        <LinearLayout>
            <!-- A RecyclerView with some commonly used attributes -->
            <android.support.v7.widget.RecyclerView
                android:id="@+id/my_recycler_view"
                android:scrollbars="vertical"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:elevation="24dp"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"/>
        </LinearLayout>    
    <Button />    
</RelativeLayout>
Stanchion answered 25/3, 2015 at 18:12 Comment(0)
C
0

This is because in Frame Layout every view stacks on one after the other... FrameLayout was designed to block out an area in screen. You can add many views or ViewGroups to frameLayout . All view will be stacked on top of each other ...

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:fab="http://schemas.android.com/apk/res-auto"
    android:id="@+id/fragment_my_frame_layout"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
      />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/my_recycler_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scrollbars="vertical" />

    <com.melnykov.fab.FloatingActionButton
        "@+id/button_floating_action"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|right"
        android:layout_margin="16dp"
        android:src="@drawable/ic_action_new"
        fab:fab_colorNormal="@color/primary_dark"
        fab:fab_colorPressed="@color/primary" />

    </LinearLayout>
</FrameLayout>

And then when you want to inflate an EditText inflate it to the linearlayout and not to the frame layout

Coralline answered 9/6, 2017 at 19:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.