-3

I'm trying to make a view that contains "two" ListViews. The first one's height should be equal to a single list item height, and the second ListView's height should take up the remaining space.

Any ideas?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:baselineAligned="false"
android:divider="?android:attr/dividerHorizontal"
android:orientation="vertical" >

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/fragment_forecast"
    android:name=MyFragment"
    android:layout_width="fill_parent"
    android:layout_height="100dp"
    tools:layout="@android:layout/list_content" />

<FrameLayout
    android:id="@+id/fragment_wear"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

</LinearLayout>

Why people are -1 this!! If you don't know the answer then just simply move to another!

AMahdy
  • 123
  • 2
  • 10
  • 2
    Are list item heights variable? – ataulm Jan 03 '15 at 21:59
  • @djechelon updated with my code so far .. 100dp is my problem, I want it to be dynamic. – AMahdy Jan 03 '15 at 22:03
  • How do you populate the list? – usr-local-ΕΨΗΕΛΩΝ Jan 03 '15 at 22:05
  • Then just set the height `android:layout_height="@dimen/my_list_item_height"` – ataulm Jan 03 '15 at 22:08
  • @ataulm how to calculate 'my_list_item_height'? – AMahdy Jan 03 '15 at 22:14
  • 1
    People are downvoting this because you haven't stated what you've done to try to solve the issue yourself. The information you added doesn't seem to be relevant (there's no ListView in the XML you added) and it's difficult to answer your question. re: how to calculate `my_list_item_height`, I asked you if the heights are variable, and you said it's fixed; _this fixed height_ is what you should use as `my_list_item_height` – ataulm Jan 03 '15 at 22:17
  • @ataulm So I'm supposed to attach my full project source code here to satisfy them? The project is complicated and has many complicated layouts. What I posted is a fragment and under it a "place holder" that render a second fragment. Each fragment resolved to a ListView. Now after all this too much unrelated topic; the height is fixed but I have no idea what it is, it's composed of several view items that render based on screen resolution. – AMahdy Jan 03 '15 at 22:23
  • if you don't know the height, then it's not fixed - it's variable, depending on what's inside it. – ataulm Jan 03 '15 at 22:24
  • @ataulm ok I meant fixed in terms of fixed items inside each list item. Those items will never change even though, it's not a SimpleCursor. – AMahdy Jan 03 '15 at 22:27
  • So any thoughts how to solve this? – AMahdy Jan 03 '15 at 22:28
  • Can you add the layout for a list item? – ataulm Jan 03 '15 at 22:28
  • 1
    If the heights of all the items in the first `ListView` are all going to be the same, but it is not possible to determine this in advance because of different screen resolutions then I don't think it's possible to specify in the xml that the height of the ListView itself should equal the height of an item (I could be wrong about this). What you can do is call `layout()` and `getMeasuredHeight()` on an item at runtime and set the height of the top ListView at runtime. All the other information, such as the fact that the second ListView is below the first can be put in the xml. – Paul Boddington Jan 03 '15 at 22:29

2 Answers2

0

While I agree that the code snippet in the OP contributes nothing to the question, the question itself is very reasonable and I had to do exactly the same thing in my project.

The way this can be done (assuming all items in the list have the same height) is by measuring an item once and assigning its height to the listview. Here is what worked for me (adapter's code):

    private int cellHeight = -1;
    ...

    public override View getView(int position, View convertView, ViewGroup parent)
    {
        View view = context.LayoutInflater.Inflate(R.layout.list_item, null);
        ...

        if (cellHeight == -1)
        {
            // Measure the cell once and set this height on the list view. Won't work correctly if 
            // cells have different heights
            int UNBOUNDED = MeasureSpec.MakeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
            view.Measure(UNBOUNDED, UNBOUNDED);
            cellHeight = view.getMeasuredHeight();
            LayoutParameters para = listview.getLayoutParams();
            para.Height = cellHeight;
            listview.setLayoutParams(para);
        }

        return view;
    }
Dennis K
  • 1,828
  • 1
  • 16
  • 27
-1

The real question is, what benefit do you hope to achieve by having a ListView that only shows 1 item - why not just display that item in a standard View?


If you have a fixed height ListItem, you can set this as the android:layout_height attribute:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical">

  <ListView
    android:id="@+id/top_list_view"
    android:layout_width="match_parent"
    android:layout_height="?android:listPreferredItemHeight"
    android:background="@android:color/holo_green_dark" />

  <ListView
    android:id="@+id/bottom_list_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/holo_blue_bright" />
</LinearLayout>

two listviews, with the top listview having a fixed height

ataulm
  • 15,195
  • 7
  • 50
  • 92
  • Is there something similar to '?android:listPreferredItemHeight' that can actually get the calculated list item height? – AMahdy Jan 03 '15 at 22:34
  • Nope, that's a ref to a static value, just resolved at runtime depending on which theme you have active. – ataulm Jan 03 '15 at 22:36
  • To answer your question: The top ListView is a custom one that rotate horizontally instead of vertically. But it extends ListView because I need all its functionalities. So far I'm unable to make it wrap to one list item. – AMahdy Jan 03 '15 at 22:36
  • It sounds a lot like an XY problem, I would close this question and ask another stating what you _need_, rather than stating what you need to achieve what you _think_ you need - i.e. what functionality the ListView gives that you want to use. – ataulm Jan 03 '15 at 22:38
  • What I need is what's in the question subject: Android ListView height to wrap to the height of one single list item.. – AMahdy Jan 03 '15 at 22:41
  • We're only trying to help. What you need can be achieved by reading my comment under your question. Trust me. I've done exactly what you're asking for. – Paul Boddington Jan 03 '15 at 22:45