1
<com.mil.example.CustomListView
                    android:id="@+id/villa_lv"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_below="@+id/itemImageLay"
                    android:fadingEdge="none"
                    android:scrollbars="none"/>

And below is the CustomListview classs.

public class CustomListView extends ListView {
private float lastMotionY;

public CustomListView(Context context) {
    super(context);
}

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

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    int mode = MeasureSpec.getMode(widthMeasureSpec);
    if (mode == MeasureSpec.UNSPECIFIED) {
        int height = getLayoutParams().height;
        if (height > 0)
            setMeasuredDimension(getMeasuredWidth(), height);
    }
}

@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
    int action = event.getAction();
    float x = event.getX();
    float y = event.getY();
    float dy = y - lastMotionY;
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        lastMotionY = y;
        break;
    case MotionEvent.ACTION_MOVE:
        if (Util.canScroll(this, false, (int) dy, (int) x, (int) y)) {
            lastMotionY = y;
            return false;
        }
        break;
    }
    return super.onInterceptTouchEvent(event);
}

}

I want to set height as match_parent but then it's showing only the first item of the list. It shows all the items when I give the height, I am using scroll inside another scroll which is working fine.

Ashwini Shahapurkar
  • 6,586
  • 6
  • 26
  • 35
Neha
  • 135
  • 1
  • 7
  • 1
    Post the full xml code.. – Avishek Das Apr 07 '15 at 06:33
  • Are you using `ListView` inside `ScrollView` ?? – Anshul Tyagi Apr 07 '15 at 06:36
  • Yes, It's a custom scrollview and custom listview, which is actually working fine, but I have to give the height to a listview – Neha Apr 07 '15 at 06:47
  • A `ListView` never works inside a `ScrollView` with the `match_parent` height. – Anshul Tyagi Apr 07 '15 at 06:51
  • For more, you should look at http://stackoverflow.com/q/18367522/4404791 – Anshul Tyagi Apr 07 '15 at 06:53
  • getting null pointer exception in view.measure(desiredWidth, MeasureSpec.UNSPECIFIED); – Neha Apr 07 '15 at 08:50
  • Did you initialize it with null value or not? – Anshul Tyagi Apr 07 '15 at 08:57
  • View view = null; for (int i = 0; i < listAdapter.getCount(); i++) { view = listAdapter.getView(i, view, listView); if (i == 0) view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, RelativeLayout.LayoutParams.WRAP_CONTENT)); view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED); totalHeight += view.getMeasuredHeight(); } – Neha Apr 07 '15 at 09:02
  • View is intialized with null value – Neha Apr 07 '15 at 09:02
  • This line shouldn't give any error because we're initializing them too like `int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.UNSPECIFIED); int totalHeight = 0;` – Anshul Tyagi Apr 07 '15 at 09:09
  • Answer in this link helped me, Thanks :) http://stackoverflow.com/questions/21620764/android-listview-measure-height – Neha Apr 07 '15 at 09:13

2 Answers2

0

Try something like this below you set the custom adapter to your list view:

    listViewResult.setAdapter(customAdapter);

    listViewResult.setVisibility(View.VISIBLE);


     int totalHeight = 0;
     int adapterCount = customAdapter.getCount();
     for (int size = 0; size < adapterCount ; size++) {
         View listItem = customAdapter.getView(size, null, listViewResult);
         listItem.measure(0,0);
         totalHeight += listItem.getMeasuredHeight();
     }
     //Change Height of ListView 
     ViewGroup.LayoutParams params = listViewResult.getLayoutParams();
     params.height = totalHeight + (listViewResult.getDividerHeight() * (adapterCount - 1));
     listViewResult.setLayoutParams(params);
Krupa Patel
  • 3,309
  • 3
  • 23
  • 28
0

Try to use Weight like this

<com.mil.example.CustomListView
                    android:id="@+id/villa_lv"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1"
                    android:layout_below="@+id/itemImageLay"
                    android:fadingEdge="none"
                    android:scrollbars="none"/>
Divyang Panchal
  • 1,889
  • 1
  • 19
  • 27