Rover
Rover

Reputation: 768

How to Put listview and recyclerview in linearlayout

android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">

<ListView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/list_items"></ListView>
<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/recycler">

</android.support.v7.widget.RecyclerView>

I need to add listview and recycler view in one activity but in this i am just getting listview items. Recycler view items are not showing.

new XML

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fillViewport="true"
        android:id="@+id/list_items">
    </ListView>
    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fillViewport="true"
        android:id="@+id/recycler">

    </android.support.v7.widget.RecyclerView>
</LinearLayout>
</ScrollView>

It shows listview in 1/4 of the screen and rest of the screen showing the recyclerview. i want all the items of the listview will show as listview end's recyclerview items start showing.

Upvotes: 0

Views: 259

Answers (2)

ssynhtn
ssynhtn

Reputation: 1377

The preferred way is to use a single RecyclerView, making its adapter handling both view types from your ListView and RecyclerView, but if you insist using both ListView and RecyclerView, you can use this ListView subclass

public class WrapListView extends ListView {
    public WrapListView(Context context) {
        super(context);
    }

    public WrapListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        heightMeasureSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 1, MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}

RecyclerView supports wrap_content out of the box, you just have to call recyclerView.setNestedScrollingEnabled(false); to make scrolling smooth

Doing this will void the recycling mechanism of both ListView and RecyclerView, since both view's height are stretched to full length, and no item view is reused/recycled.

Upvotes: 0

Happy Bhimani
Happy Bhimani

Reputation: 206

There is one good feature of android designing is Weight_sum properties in Linear layout.In your above code make changes as shown below,...

<LinearLayout 
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="2">
<ListView
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1" />
<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1" />

As per above code,can add weight sum in parent layout and add weight as per you wish to divide your layout. check by editing above code like,..

<LinearLayout 
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="3">
<ListView
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="2" />
<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1" />

You will get to know about how weight some works! May answer helps you!

Upvotes: 2

Related Questions