-1

Here I'm new to android development. Don't mind if something isn't clear.

I did enough research on this issue, before marking this as duplicate please update the solution.

Here I'm adding an image with arrangement of layouts what I want to implement here.

Here the listView is being shown as only 1 item. If I scroll, all the items will be scrolled in the shown in this height.

sample image

Here my xml structure is

<LinearLayout>(parent)
     <ScrollView>
          <LinearLayout>
               <LinearLayout>
                weight: __
                -----------
               </LinearLayout>
               <ListView>
                   h:match_parent
                   w:wrap_content
                   weight:__
               </ListView>
               <LinearLayout>
                   weight:__
                --------------
               </LinearLayout>
          </LinearLayout>
     </ScrollView>
</LinearLayout>

I'm getting numbers of data sets from DB and have to show those sets in middle of the both LinearLayouts. For now I'm using ListView but seems impossible to use here as my expectation.

Please suggest whether I can I perform in ListView here or how can I use numbers of LinearLayout blocks for each dataset?

Update This is what I expect as shown in the pic.

sample_layout

Shambhu
  • 181
  • 6
  • 16
  • didn't understand all can u explain more – Goku Nov 30 '17 at 08:56
  • I've updated the the question, I want to show the data sets in middle of the 2 `linearLayout` which is I'm getting from DB. – Shambhu Nov 30 '17 at 09:34
  • check my below ans @snsingh i hope it helps – Goku Nov 30 '17 at 09:38
  • See if this helps : [link]https://stackoverflow.com/questions/18367522/android-list-view-inside-a-scroll-view and this should help [link]http://www.androidhub4you.com/2014/03/android-listview-into-scrollview-issue.html – kukroid Nov 30 '17 at 22:09

5 Answers5

1

Try this

<LinearLayout>(parent)
     <NestedScrollView>
          <LinearLayout>
               <LinearLayout>                    
                w:wrap_content
                h:wrap_content
               </LinearLayout>
               <ListView>
                   w:match_p__arent
                   h:match_p__arent
                   weight:1
                   NestedScroll:true
               </ListView>
               <LinearLayout>
                   w:match_p__arent
                   h:match_p__arent
                   weight:1 
               </LinearLayout>
          </LinearLayout>
     </NestedScrollView>
</LinearLayout>
Shambhu
  • 181
  • 6
  • 16
Goku
  • 9,102
  • 8
  • 50
  • 81
  • In this case, scrolling in `listView` works fine, but the `scrollView` isn't being scrolled. – Shambhu Nov 30 '17 at 10:10
  • @SNSingh use **NestedScrollView** instead of **ScrollView** – Goku Nov 30 '17 at 10:18
  • @SNSingh check this https://stackoverflow.com/questions/35634023/how-can-i-have-a-listview-inside-a-nestedscrollview – Goku Nov 30 '17 at 10:19
  • Thanks, now both are working fine, but now the `height` of `listView` is just as of 1 item. what if I want to increase height? – Shambhu Nov 30 '17 at 11:11
1

You need this method for your ListView

    public static void setListViewHeightBasedOnItems(ListView target_Listview, int limit) // LIMIT 0 FOR SHOWING ALLL CONTENTS
{
    if (limit == 0) {
        ListAdapter listAdapter = target_Listview.getAdapter();
        if (listAdapter != null) {

            int numberOfItems = listAdapter.getCount();

            // Get total height of all items.
            int totalItemsHeight = 0;
            for (int itemPos = 0; itemPos < numberOfItems; itemPos++) {
                //                if(itemPos < 4)
                //                {
                View item = listAdapter.getView(itemPos, null, target_Listview);
                item.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
                Log.e("" + itemPos, "" + item.getMeasuredHeight());
                totalItemsHeight += item.getMeasuredHeight();
                //                }
            }

            // Get total height of all item dividers.
            int totalDividersHeight = target_Listview.getDividerHeight() * (numberOfItems - 1);

            // Set list height.
            ViewGroup.LayoutParams params = target_Listview.getLayoutParams();
            params.height = totalItemsHeight + totalDividersHeight;
            target_Listview.setLayoutParams(params);
            target_Listview.requestLayout();

            //  return true;

        }
        else {

        }
    }
    else {
        ListAdapter listAdapter = target_Listview.getAdapter();
        if (listAdapter != null) {

            int numberOfItems = listAdapter.getCount();

            // Get total height of all items.
            int totalItemsHeight = 0;
            for (int itemPos = 0; itemPos < numberOfItems; itemPos++) {
                if (itemPos < limit) {
                    View item = listAdapter.getView(itemPos, null, target_Listview);
                    item.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
                    Log.e("" + itemPos, "" + item.getMeasuredHeight());
                    totalItemsHeight += item.getMeasuredHeight();
                }
            }

            // Get total height of all item dividers.
            int totalDividersHeight = target_Listview.getDividerHeight() * (numberOfItems - 1);

            // Set list height.
            ViewGroup.LayoutParams params = target_Listview.getLayoutParams();
            params.height = totalItemsHeight + totalDividersHeight;
            target_Listview.setLayoutParams(params);
            target_Listview.requestLayout();
        }


    }
}

After setting adapter on ListView Just call the method and Pass your ListView in the parameters with your list size as second parameter.

One Suggestion from my side : You should use RecyclerView Instead of listview . Just need to write one line for that after setting up everything(adapter etc)

recyclerView.HasFixedSize();

Hope my answer will help.

parambir singh
  • 224
  • 2
  • 13
0

Might need more clarification on what exactly it is you're trying to accomplish.

If the ListView is supposed to be longer and to scroll through more items on the list, I might recommend using the RecyclerView. Its a little bit more setup, but allows for a lot more complex layouts to be created based off the concept.

There are a lot of great references to learn the ways of RecyclerView if you want to search further:

Andrew Steinmetz
  • 1,010
  • 8
  • 16
  • thanks for the response, but I've to implement in `listView` – Shambhu Nov 30 '17 at 11:06
  • Is there a reason it must be a `ListView`? Just because the setup in your layout file right now has a scroll trap since the `ScrollView` and the `ListView` are both scrollable. A `RecyclerView` could help you get around that by combining everything into one big scrollable list. – Andrew Steinmetz Dec 01 '17 at 07:16
0

try this way

<LinearLayout>   
      <LinearLayout>
           <LinearLayout>
            weight: __
            -----------
           </LinearLayout>
           <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <ListView>
               h:match_parent
               w:wrap_content
               weight:__
            </ListView>
           </ScrollView>
           <LinearLayout>
               weight:__
            --------------
           </LinearLayout>
      </LinearLayout>

ajay dhadhal
  • 306
  • 4
  • 16
0

enter image description here
enter image description here

I have tried in my Android studio and mine is working i am able to scroll view .mine is little bit deffrent then you.

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

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorAccent">

        <LinearLayout
            android:id="@+id/first"
            android:layout_width="match_parent"
            android:layout_height="50dp"

            android:background="@color/colorAccent"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center_horizontal"
                android:text="@string/hello" />

        </LinearLayout>

        <ListView
            android:id="@+id/listview"
            android:layout_width="match_parent"
            android:layout_height="250dp"
            android:background="@color/black_overlay"
            android:layout_below="@+id/first" />

        <LinearLayout
            android:id="@+id/second"
            android:layout_width="match_parent"
            android:layout_height="550dp"

            android:background="@color/colorAccent"
            android:layout_below="@+id/listview"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center_horizontal"
                android:text="@string/hello" />

        </LinearLayout>


    </RelativeLayout>


</ScrollView>

enter image description here

Divyesh Kalotra
  • 204
  • 1
  • 2
  • 13