2

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 one list item at a time thanks help me.....

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/taskscrollviewid"
    android:layout_width="match_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:scrollbars="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:orientation="horizontal"
            android:weightSum="2" >

            <Button
                android:id="@+id/txtdate"
                android:layout_width="0dip"
                android:layout_height="30px"
                android:layout_weight="1"
                android:background="@drawable/taskbuttonselector" />

            <Button
                android:id="@+id/btnTaskTimeid"
                android:layout_width="0dip"
                android:layout_height="30px"
                android:layout_weight="1"
                android:background="@drawable/taskbuttonselector" />
        </LinearLayout>

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:orientation="horizontal" >

            <EditText
                android:id="@+id/txtItem"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/edittextselector"
                android:drawableEnd="@drawable/inbox"
                android:gravity="top|left"
                android:hint="Enter task"
                android:inputType="textMultiLine"
                android:maxLines="5"
                android:minLines="3"
                android:scrollbars="vertical"
                android:singleLine="false" />

            <ImageButton
                android:id="@+id/imagebuttonid"
                android:layout_width="31dp"
                android:layout_height="36dp"
                android:layout_gravity="right|bottom"
                android:background="@drawable/inbox" />
        </FrameLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:orientation="horizontal"
            android:weightSum="2" >

            <Button
                android:id="@+id/btnaddcategoryid"
                android:layout_width="0dip"
                android:layout_height="30px"
                android:layout_weight="1"
                android:background="@drawable/taskbuttonselector"
                android:text="AddCategory" />

            <Button
                android:id="@+id/btnaddseverityid"
                android:layout_width="0dip"
                android:layout_height="30px"
                android:layout_weight="1"
                android:background="@drawable/taskbuttonselector"
                android:text="AddSeverity" />
        </LinearLayout>

        <Button
            android:id="@+id/btnadddb"
            android:layout_width="wrap_content"
            android:layout_height="30px"
            android:layout_gravity="center|center_horizontal"
            android:layout_marginTop="10dp"
            android:background="@drawable/taskbuttonselector"
            android:text="Add This Task" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:orientation="horizontal"
            android:weightSum="2" >

            <Button
                android:id="@+id/fillbut"
                android:layout_width="0dip"
                android:layout_height="30px"
                android:layout_weight="1"
                android:background="@drawable/taskbuttonselector"
                android:text="Remain Task" />

            <Button
                android:id="@+id/remainbut"
                android:layout_width="0dip"
                android:layout_height="30px"
                android:layout_weight="1"
                android:background="@drawable/taskbuttonselector"
                android:text="Finish Task" />
        </LinearLayout>

        <ListView
            android:id="@+id/listid"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:clickable="true"
            android:orientation="vertical" >
        </ListView>
    </LinearLayout>

</ScrollView>
Satheesh
  • 1,722
  • 25
  • 35

3 Answers3

3

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.

npace
  • 4,218
  • 1
  • 25
  • 35
0

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.

Nirmal
  • 2,340
  • 21
  • 43
0
    //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;

    }


}
shailesh Rohit
  • 407
  • 5
  • 6