Skip to content Skip to sidebar Skip to footer

Scrollview Only Works In Listview

I have the following layout defined in my app:

Solution 1:

I am going through your problem. You can not use list view inside the scroll-view directly. If you want to use it, then you have to give height pragmatically to list view and it work for me.

publicclasstest1extendsActivity {
...
protectedvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test1);
...

//here 1st set your list view than call  set_List_View_Height_Based_On_Children...

set_List_View_Height_Based_On_Children(your_Listview_name); 
...
}
///method set_List_View_Height_Based_On_Childrenpublicstaticvoidset_List_View_Height_Based_On_Children(ListView listView) {
    ListAdapterlistAdapter= listView.getAdapter();
    if (listAdapter == null)
        return;

    intdesiredWidth= View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.UNSPECIFIED);
    inttotalHeight=0;
    Viewview=null;
    for (inti=0; i < listAdapter.getCount(); i++) {
        view = listAdapter.getView(i, view, listView);
        if (i == 0)
            view.setLayoutParams(newViewGroup.LayoutParams(desiredWidth, ViewGroup.LayoutParams.WRAP_CONTENT));

        view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
        totalHeight += view.getMeasuredHeight();
    }
    ViewGroup.LayoutParamsparams= listView.getLayoutParams();
    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    listView.setLayoutParams(params);
    listView.requestLayout();
}
}

Solution 2:

ListView can't be placed inside a ScrollView unless you define that your ListView isn't scrollable. If you wanna do this you should create a custom ListView like the following

publicclassNonScrollListViewextendsListViewCompat {

//Define a public constructor@OverrideprotectedvoidonMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        intheightMeasureSpec_custom= View.MeasureSpec.makeMeasureSpec(
                Integer.MAX_VALUE >> 2, View.MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec_custom);
        ViewGroup.LayoutParamsparams= getLayoutParams();
        params.height = getMeasuredHeight();
    }
}

Solution 3:

You should not use a ListView in a ScroolView, it has it's own scroll and they interfere with each-other. I would recommend creating a list adapter that creates all of your different views from an array using a switch statement:

classMyAdapterextendsArrayAdapter{

    publicMyAdapter(Context context, int resource, List objects) {
        super(context, resource, objects);
    }

    @Overridepublic View getView(int position, View convertView, ViewGroup parent) {
         ViewItemviewType= getItem(position);

        switch(viewType.type){
            case TEXTVIEW: convertView = LayoutInflater.from(getContext()).inflate(R.layout.textView1, parent, false);

                break;
            case LISTITEM: convertView = LayoutInflater.from(getContext()).inflate(R.layout.listItem, parent, false);

                break;
        }


        return convertView;
    }


}

Post a Comment for "Scrollview Only Works In Listview"