I have a RecyclerView which displays dynamic content. I would to display this RecyclerView in between other Views. For example, one View displayed above and one View displayed below the RecyclerView:
View
RecyclerView
View
However, whatever I try doesn't seem to be working and the RecyclerView seems to take up the entire space. I believe the problem lies in the wrap_content issue, though I've tried some suggested solutions, such as this custom LinearLayoutManager, yet it doesn't seem to fix my problem.
Attempt:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- AppBar works fine; no issue here -->
<android.support.design.widget.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="@+id/app_bar_layout">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_height="?attr/actionBarSize"
android:layout_width="match_parent"
app:layout_collapseMode="pin"/>
</android.support.design.widget.AppBarLayout>
<!-- Not displaying; Tried different views -->
<com.chrynan.taginput.view.TextInputWrapper
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edit_text_container">
<AutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edit_text"/>
</com.chrynan.taginput.view.TextInputWrapper>
<!-- Takes up entire screen space -->
<android.support.v4.widget.SwipeRefreshLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/swipe_refresh">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/recycler_view"/>
</android.support.v4.widget.SwipeRefreshLayout>
<!-- Not displaying -->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/text_view"/>
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
Question: Is there a way to place and display a View above and a View below a RecyclerView? If so, how? Or is it better to use a ListView?
This viewType variable is internal to the Adapter class. It's used in the onCreateViewHolder() and onBindViewHolder to inflate and populate the mapped layouts. Before we jump into the implementation of the Adapter class, let's look at the types of layouts that are defined for each view type.
To be able to scroll through a vertical list of items that is longer than the screen, you need to add a vertical scrollbar. Inside RecyclerView , add an android:scrollbars attribute set to vertical .
RecyclerView provides these built-in layout managers: LinearLayoutManager shows items in a vertical or horizontal scrolling list. GridLayoutManager shows items in a grid. StaggeredGridLayoutManager shows items in a staggered grid.
RecyclerView is powerful when you need to customize your list or you want better animations. Those convenience methods in ListView caused a lot of trouble to people which is why RecyclerView provides a more flexible solution to them. The major change you need to make for migration is in your adapter.
What about the usage of weight
?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://..."
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<View
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<RecyclerView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
<View
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
This might not be your typical solution, but you can try it nonetheless.
How about using a RelativeLayout instead of LinearLayout, and simply setting the top and bottom padding of your SwipeRefreshLayout to the height of your views as follows.
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- your app bar -->
<!-- Not displaying; Tried different views -->
<com.chrynan.taginput.view.TextInputWrapper
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_alignParentTop="true"
android:id="@+id/edit_text_container"/>
<!-- Takes up entire screen space -->
<android.support.v4.widget.SwipeRefreshLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="150dp"
android:paddingBottom="250dp"
android:id="@+id/swipe_refresh">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/recycler_view"/>
</android.support.v4.widget.SwipeRefreshLayout>
<!-- bottom view -->
<TextView
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="250dp"
android:id="@+id/text_view"/>
</RelativeLayout>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With