1

I put the listView in a popupWindow, when it has many items, the popupWindow exceed the screen size, so I want to limit the max height(or max item, show scroll bar if exceed) of the listView, thanks all

Zijian
  • 207
  • 1
  • 9

3 Answers3

0

Try this it may be help to you

public static void setListViewHeightBasedOnChildren(ListView listView) {
     ListAdapter listAdapter = listView.getAdapter();
   if (listAdapter == null) {
   // pre-condition

         return;
   }


int totalHeight = listView.getPaddingTop() + listView.getPaddingBottom();
for (int i = 0; i < 5; i++) //here you can set 5 row at a time if row excceded the scroll automatically available
{
    View listItem = listAdapter.getView(i, null, listView);
    if (listItem instanceof ViewGroup) {
         listItem.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    }
      listItem.measure(0, 0);
      totalHeight += listItem.getMeasuredHeight();
}

  ViewGroup.LayoutParams params = listView.getLayoutParams();
  params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
  listView.setLayoutParams(params);

}

and you can use like this

     YourAdapter mYoursAdapter = new YourAdapter();
     mListview.setAdapter(mYoursAdapter);
     setListViewHeightBasedOnChildren(mListview);
Abhishek Patel
  • 4,280
  • 1
  • 24
  • 38
0

Create a class AppUtil in your project.Try like this:

public class AppUtil{
public static void setListViewHeightBasedOnChildren(ListView listView) {
        if (listView == null) {
            return;
        }
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            // pre-condition
            return;
        }

        int totalHeight = listView.getPaddingTop() + listView.getPaddingBottom();
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            if (listItem instanceof ViewGroup) {
                listItem.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            }
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }

        LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
    }

}

To call the above method in your activity or fragment you can proceed like this:

    public class MedicalReportsFragment extends Fragment {
@Bind(R.id.lv_investigative_report)
    ListView investigativeReportLV;

      private MedicalReportsAdapter mMedicalReportsAdapter;
    @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    mMedicalReportsAdapter = new MedicalReportsAdapter(getActivity(), mMedicalReportOverviews,true);
    investigativeReportLV.setAdapter(mMedicalReportsAdapter);
            AppUtil.setListViewHeightBasedOnChildren(investigativeReportLV);
          }
    }
Lips_coder
  • 686
  • 1
  • 5
  • 17
0

I think you can try this trick in your layout.xml:

<ListView
    android:id="@+id/list_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="40dp" />

<LinearLayout
    android:id="@+id/layout_bottom_button"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:layout_marginTop="-40dp" />

In fact the marginBottom will reserve 40dp for the layouts below ListView, so your ListView would not cover the whole screen like before.