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:

public class Utility {

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

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



    =============================
            Main Activity
    =============================

            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;

            public class MainActivity extends Activity {
                public ListView listview;
                ArrayList<String>data;
                Scroll_adapter adapter;

                @Override
                protected void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.activity_main);
                     listview=(ListView)findViewById(R.id.listView1);
                     data=new ArrayList<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=new Scroll_adapter(MainActivity.this,data);
                        listview.setAdapter(adapter);


                }


                @Override
                public boolean onCreateOptionsMenu(Menu menu) {
                    // Inflate the menu; this adds items to the action bar if it is present.
                    getMenuInflater().inflate(R.menu.main, menu);
                    return true;
                }


                public void setListfalse() {
                    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;

public class Scroll_adapter  extends ArrayAdapter<String>  
{
    private final Activity context;
    ArrayList<String>data;
    class ViewHolder 
    {
        public TextView tv_name;
        public ScrollView scroll;
        public LinearLayout l1;
    }


    public Scroll_adapter(Activity context, ArrayList<String> all_data) {
        super(context, R.layout.item_list, all_data);
        this.context = context;
        //data=all_data;
    }
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View rowView = convertView;
        ViewHolder viewHolder;
        if (rowView == null)
        {
            LayoutInflater inflater = context.getLayoutInflater();
            rowView = inflater.inflate(R.layout.item_list, null);

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


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

        // create dynamic Linear layout and add  textview or whatever u need just add into dynamic created layout

        for(int i=0;i<10;i++)
        {
            LinearLayout LL = new LinearLayout(context);

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

            final TextView t1=new TextView(context);

            //t1.setText("hello");

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

            LL.addView(t1);

            viewHolder.l1.addView(LL);

        }
        viewHolder.l1.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub

                // stop listview scrolling and enable scrollview inside listview item

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

                return false;
            }
        });

                return rowView;

    }


}

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