scrolling ListView within ScrollView

前端 未结 4 1372
终归单人心
终归单人心 2021-01-16 02:50

I have a ScrollView. One of its children is a ListView. Both scrolling in the same direction. How do I get both of them to respond to scroll events? And then when the end of

相关标签:
4条回答
  • 2021-01-16 02:55

    you cannot add a ListView in a scroll View ,as list view also scolls and there would be a synchonization problem between listview scroll and scroll view scoll. You can make a CustomList View and add this method into it.

    @Override public boolean onInterceptTouchEvent(MotionEvent ev)
    {
        /*
         * Prevent parent controls from stealing our events once we've gotten a touch down
         */
        if (ev.getActionMasked() == MotionEvent.ACTION_DOWN) {
            ViewParent p = getParent();
            if (p != null) {
                p.requestDisallowInterceptTouchEvent(true);
            }
        }
        return false;
    }
    
    0 讨论(0)
  • 2021-01-16 03:05

    I have a ScrollView

    Get rid of it.

    One of its children is a ListView

    Put everything in the ListView, either using addHeaderView()/addFooterView(), my MergeAdapter, or something else along those lines.

    0 讨论(0)
  • 2021-01-16 03:12

    You cant scroll a listview same direction as the scrollview. But if you have a horizontal scrollview you can scroll vertical with a listview.

    0 讨论(0)
  • 2021-01-16 03:15

    You Should not have a listview within a ScrollView.

    But you can have a custom class and use that to accomplish A Listview with in a ScrollView.

    Try the following code

    first make a custom ScrollView Class.

    public class VerticalScrollview extends ScrollView{
    
    public VerticalScrollview(Context context) {
        super(context);
    }
    
     public VerticalScrollview(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public VerticalScrollview(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
    
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        final int action = ev.getAction();
        switch (action)
        {
            case MotionEvent.ACTION_DOWN:
                    super.onTouchEvent(ev);
                    break;
    
            case MotionEvent.ACTION_MOVE:
                    return false; // redirect MotionEvents to ourself
    
            case MotionEvent.ACTION_CANCEL:
                    super.onTouchEvent(ev);
                    break;
    
            case MotionEvent.ACTION_UP:
                    return false;
    
            default: break;
        }
    
        return false;
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        super.onTouchEvent(ev);
         return true;
    }
    }
    

    Then use this class for you layout

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:orientation="vertical" >
    
    <com.gui.today.VerticalScrollview
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#000000"
        tools:context=".MainActivity" >
    
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
         <FrameLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" >
    
                <ProgressBar
                    android:id="@+id/progressBar3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical|center_horizontal" />
    
                <TextView
                    android:id="@+id/empty_calender"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical|center_horizontal"
                    android:text="@string/empty_calender"
                    android:textColor="#ffffff"
                    android:visibility="gone" />
    
                <ListView
                    android:id="@+id/listView2"
                    android:layout_width="fill_parent"
                    android:layout_height="150dp" >
                </ListView>
            </FrameLayout>
        </LinearLayout>
      </com.gui.today.VerticalScrollview>
    
    0 讨论(0)
提交回复
热议问题