0

I need to Implement the list view inside scroll view. i knw it's not good idea using listview inside scroll view but it's requirenment of app so i need to implement it. In app im using TabLayout with 3 type of tabs like categories 1,categories 2 and again categories 3 to filter the data.

in first tab categories 1 i need to add 2 list view one is fix items and second is dynamic. Fist list view is fixed so i can give fixed height to it. but second List view is dynamic so i need to give height according to the total number of values in list view.

im try many ways to give dynamic height but not getting the task done.

here is method i alredy try :-

-------------------first ------------------------------- first from question

Android: How to measure total height of ListView

public static void getTotalHeightofListView(ListView listView) {

    ListAdapter mAdapter = listView.getAdapter();

    int totalHeight = 0;

    for (int i = 0; i < mAdapter.getCount(); i++) {
        View mView = mAdapter.getView(i, null, listView);

        mView.measure(
                View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),

                View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));

        totalHeight += mView.getMeasuredHeight();
        Log.w("HEIGHT" + i, String.valueOf(totalHeight));

    }

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

}

-------------------second------------------------------- from question https://stackoverflow.com/questions/17693578

public static class ListUtils {
    public static void setDynamicHeight(ListView mListView) {
        ListAdapter mListAdapter = mListView.getAdapter();
        if (mListAdapter == null) {
            // when adapter is null
            return;
        }
        int height = 0;
        int desiredWidth = MeasureSpec.makeMeasureSpec(mListView.getWidth(), MeasureSpec.UNSPECIFIED);
        for (int i = 0; i < mListAdapter.getCount(); i++) {
            View listItem = mListAdapter.getView(i, null, mListView);
            listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
            height += listItem.getMeasuredHeight();
        }
        ViewGroup.LayoutParams params = mListView.getLayoutParams();
        params.height = height + (mListView.getDividerHeight() * (mListAdapter.getCount() - 1));
        mListView.setLayoutParams(params);
        mListView.requestLayout();
    }
}

-------------------error-------------------

Community
  • 1
  • 1
  • Like you said: don't do it. I Would recommend to use another approach or at least provide us with screenshots of your current state. – finki Jul 06 '16 at 11:34

2 Answers2

0

In your activity xml file, you can use LinearLayout inside the ScrollView and then can assign android:weight attribute to them.

You can assign weight to the single element and all other element ajust at their own available space.

Saini
  • 734
  • 3
  • 8
  • if you give fixed height to the first element, then remaining space will b automatically given to the second element. If second element do not require much space then there will remain some space below them. You don't have to allocate dyanmic space to the list view. It will be allocated automatically. You can use `android:layout_width=wrap_content`. – Saini Jul 06 '16 at 15:48
0

Better practice, it's using list that support dynamic changes of view. And for your question there is special list - LinkedListView

GensaGames
  • 5,538
  • 4
  • 24
  • 53