2016-06-24 5 views
1

Я поместил listView в popupWindow, когда у него много элементов, popupWindow превышает размер экрана, поэтому я хочу ограничить максимальную высоту (или max item, show scroll бар, если превышает) в ListView, спасибо всемКак установить максимальную высоту спискаView в android

+1

Отъезд этот ответ: http://stackoverflow.com/a/22002054/3363481 – earthw0rmjim

+0

Спасибо, ваше решение эффективно – Zijian

ответ

0

Попробуйте это может быть полезным для вас

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); 

} 

и вы можете использовать как этот

 YourAdapter mYoursAdapter = new YourAdapter(); 
    mListview.setAdapter(mYoursAdapter); 
    setListViewHeightBasedOnChildren(mListview); 
0

Создать класс AppUtil в проекте. Попробуйте вот так:

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); 
    } 

} 

Для вызова выше метод в вашей деятельности или фрагмента вы можете продолжить так:

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); 
      } 
    } 
Смежные вопросы