Skip to content Skip to sidebar Skip to footer

Scroll View Is Not Working In List View

I have multiple linear layout in this layout.The scroll view is working when i am fully scrolled the item, but not in list view.My list view have multiple list items it show only o

Solution 1:

NEVER put a list view inside a scroll view or vice versa! It says so right in the documentation:

You should never use a ScrollView with a ListView, because ListView takes care of its own vertical scrolling. Most importantly, doing this defeats all of the important optimizations in ListView for dealing with large lists, since it effectively forces the ListView to display its entire list of items to fill up the infinite container supplied by ScrollView.

Solution 2:

ListView by itself is scrollable.

You shouldn't put a ListView inside a ScrollView because the ListView class implements its own scrolling and it just doesn't receive gestures because they all are handled by the parent ScrollView.

http://developer.android.com/reference/android/widget/ScrollView.html

You either remove the scrollview or move listview outside scrollview.

You can check this link. The talk is on listview and they advice not put listview inside scrollview.

https://www.youtube.com/watch?v=wDBM6wVEO70

Solution 3:

Create Class Like this:

publicclassUtility {

publicstaticvoidsetListViewHeightBasedOnChildren(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null) {
        // pre-conditionreturn;
    }

    int totalHeight = 0;
    int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST);
    for (int i = 0; i < listAdapter.getCount(); i++) {
        View listItem = listAdapter.getView(i, null, listView);
        listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
        totalHeight += listItem.getMeasuredHeight();
    }

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

Call this method at where do you want Scroll your listview like this.

Utility.setListViewHeightBasedOnChildren(YourListView);

For your Reference.

Hope this will help you.

Solution 4:

//requestDisallowInterceptTouchEvent(true);// use above method to disable listview scroll and enabled scrollview scroll// to disable listview.requestDisallowInterceptTouchEvent(true);// to enabled scrollview scroll write as below// ex:  ((MainActivity) context).listview.requestDisallowInterceptTouchEvent(true);// i  have used above things on viewHolder.l1.setOnTouchListener(new View.OnTouchListener()



    =============================
            MainActivity=============================

            package com.example.scrollview_demo;

            import java.util.ArrayList;

            import com.example.scrollview_demo.Scroll_adapter.ViewHolder;

            import android.app.Activity;
            import android.os.Bundle;
            import android.view.LayoutInflater;
            import android.view.Menu;
            import android.view.MotionEvent;
            import android.view.View;
            import android.view.ViewGroup;
            import android.view.View.OnTouchListener;
            import android.widget.ArrayAdapter;
            import android.widget.LinearLayout;
            import android.widget.ListView;
            import android.widget.ScrollView;
            import android.widget.TextView;

            publicclassMainActivityextendsActivity {
                public ListView listview;
                ArrayList<String>data;
                Scroll_adapter adapter;

                @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.activity_main);
                     listview=(ListView)findViewById(R.id.listView1);
                     data=newArrayList<String>();
                     data.add("1");
                     data.add("1");
                     data.add("1");
                     data.add("1");
                     data.add("1");
                     data.add("1");
                     data.add("1");
                     data.add("1");
                     data.add("1");
                     data.add("1");
                     data.add("1");

                     adapter=newScroll_adapter(MainActivity.this,data);
                        listview.setAdapter(adapter);


                }


                @OverridepublicbooleanonCreateOptionsMenu(Menu menu) {
                    // Inflate the menu; this adds items to the action bar if it is present.
                    getMenuInflater().inflate(R.menu.main, menu);
                    returntrue;
                }


                publicvoidsetListfalse() {
                    listview.requestDisallowInterceptTouchEvent(false);
                }

            }

    Scroll Adapter 

         package com.example.scrollview_demo;

import java.util.ArrayList;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

publicclassScroll_adapterextendsArrayAdapter<String>  
{
    privatefinal Activity context;
    ArrayList<String>data;
    classViewHolder 
    {
        public TextView tv_name;
        public ScrollView scroll;
        public LinearLayout l1;
    }


    publicScroll_adapter(Activity context, ArrayList<String> all_data) {
        super(context, R.layout.item_list, all_data);
        this.context = context;
        //data=all_data;
    }
    @Overridepublic View getView(finalint position, View convertView, ViewGroup parent) {
        ViewrowView= convertView;
        ViewHolder viewHolder;
        if (rowView == null)
        {
            LayoutInflaterinflater= context.getLayoutInflater();
            rowView = inflater.inflate(R.layout.item_list, null);

            viewHolder = newViewHolder();
            viewHolder.scroll=(ScrollView)rowView.findViewById(R.id.scrollView1);
            viewHolder.l1=(LinearLayout)rowView.findViewById(R.id.layout);
            viewHolder.l1.setVerticalScrollBarEnabled(true);


            rowView.setTag(viewHolder);
        }
        elseviewHolder= (ViewHolder) rowView.getTag();
        viewHolder.l1.removeAllViews();

        // create dynamic Linear layout and add  textview or whatever u need just add into dynamic created layoutfor(int i=0;i<10;i++)
        {
            LinearLayoutLL=newLinearLayout(context);

            LL.setOrientation(LinearLayout.VERTICAL);
            LL.setPadding(0,5,0,0);        

            final TextView t1=newTextView(context);

            //t1.setText("hello");

            t1.setText(""+ (i+1));

            LL.addView(t1);

            viewHolder.l1.addView(LL);

        }
        viewHolder.l1.setOnTouchListener(newView.OnTouchListener() {

            @OverridepublicbooleanonTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub// stop listview scrolling and enable scrollview inside listview item

                ((MainActivity) context).listview.requestDisallowInterceptTouchEvent(true);

                returnfalse;
            }
        });

                return rowView;

    }


}

Post a Comment for "Scroll View Is Not Working In List View"