【问题标题】:ListView inside ScrollView is not scrolling on AndroidScrollView 中的 ListView 在 Android 上不滚动
【发布时间】:2011-09-06 20:12:52
【问题描述】:

我在 ScrollView 内滚动 ListView 时遇到问题。我有一个 Activity,它在顶部有一些 EditText,然后是一个带有两个选项卡的选项卡主机,每个选项卡都有一个 ListView。当 EditText 视图聚焦时,软键盘出现,因为我有一个 ScrollView,所以内容是可滚动的。但是当 ListViews 中有更多项目(选项卡中的项目)时,问题就来了,即使有更多项目,我也无法滚动 ListView。

以下是布局 XML:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="?backgroundImage"
    android:orientation="vertical">

  <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="50dip"
        android:layout_alignParentBottom="true"
        android:layout_margin="10dip"
        android:id="@+id/buttons">
    <Button
            android:text="Save"
            android:layout_margin="2dip"
            android:textSize="20dip"
            android:id="@+id/btnSaveorUpdate"
            android:layout_height="wrap_content"
            android:layout_width="145dip"></Button>
    <Button
            android:text="Cancel"
            android:layout_margin="2dip"
            android:textSize="20dip"
            android:id="@+id/btnCancelorDelete"
            android:layout_height="wrap_content"
            android:layout_width="145dip"></Button>
  </LinearLayout>
  <ScrollView
        android:layout_above="@id/buttons"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:fillViewport="true"
        android:layout_margin="10dip">
    <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_margin="10dip">
      <TextView
                android:text="Bill details"
                android:textColor="?textColorDark"
                android:layout_alignParentTop="true"
                android:id="@+id/txtEnterDetails"
                android:textSize="25dip"
                android:textStyle="bold"
                android:layout_height="wrap_content"
                android:layout_width="fill_parent"
                android:layout_marginBottom="2dip"></TextView>
      <LinearLayout
                android:focusable="true"
                android:focusableInTouchMode="true"
                android:layout_width="0dip"
                android:layout_height="0dip" />
      <EditText
                android:layout_width="fill_parent"
                android:hint="Enter data"
                android:inputType="numberDecimal"
                android:id="@+id/txtSample"
                android:textSize="@dimen/editText"
                android:layout_height="@dimen/editTextHeight"
                android:text=""></EditText>
      <EditText
                android:layout_width="fill_parent"
                android:id="@+id/txtDescription"
                android:hint="Enter description"
                android:textSize="@dimen/editText"
                android:layout_height="@dimen/editTextHeight"
                android:inputType="text"
                android:text=""></EditText>
      <EditText
                android:layout_width="fill_parent"
                android:id="@+id/txtComment"
                android:hint="Enter comment (if any)"
                android:textSize="@dimen/editText"
                android:layout_height="@dimen/editTextHeight"
                android:inputType="text"
                android:text=""></EditText>
      <LinearLayout
                android:orientation="horizontal"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content">
        <TextView
                    android:id="@+id/txtDate"
                    android:layout_width="wrap_content"
                    android:text=""
                    android:textSize="20dip"
                    android:textColor="?textColorDark"
                    android:layout_marginLeft="10dip"
                    android:layout_height="@dimen/editTextHeight"
                    android:layout_gravity="center_vertical" />
        <Button
                    android:id="@+id/btnPickDate"
                    android:layout_width="wrap_content"
                    android:layout_height="@dimen/editTextHeight"
                    android:text="Select date"
                    android:layout_margin="2dip"
                    android:textSize="15dip"
                    android:layout_gravity="center_vertical" />
      </LinearLayout>
      <TabHost
                android:id="@+id/tabhost"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">
        <LinearLayout
                    android:id="@+id/linearLayout1"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:orientation="vertical">
          <TabWidget
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:id="@android:id/tabs"></TabWidget>
          <FrameLayout
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:id="@android:id/tabcontent">
            <ScrollView
                            android:layout_above="@id/buttons"
                            android:layout_width="fill_parent"
                            android:layout_height="fill_parent"
                            android:fillViewport="true"
                            android:id="@+id/tab1">
              <LinearLayout
                                android:layout_width="fill_parent"
                                android:layout_height="wrap_content"
                                android:orientation="vertical">
                <TableLayout
                                    android:layout_height="wrap_content"
                                    android:layout_width="fill_parent">
                  <TableRow
                                        android:id="@+id/tableRow1"
                                        android:layout_marginLeft="2dip"
                                        android:layout_marginRight="5dip"
                                        android:layout_width="fill_parent"
                                        android:layout_height="wrap_content">
                    <LinearLayout
                                            android:layout_width="fill_parent"
                                            android:layout_height="wrap_content"
                                            android:orientation="horizontal">
                      <ImageView
                                                android:src="@drawable/ic_menu_invite"
                                                android:layout_width="40dip"
                                                android:layout_height="40dip"
                                                android:layout_gravity="center_vertical"></ImageView>
                      <TextView
                                                android:text="Add friend"
                                                android:layout_height="wrap_content"
                                                android:layout_width="fill_parent"
                                                android:layout_centerVertical="true"
                                                android:textColor="?textColorDark"
                                                android:textSize="@dimen/editText"
                                                android:layout_gravity="center_vertical" />
                    </LinearLayout>
                  </TableRow>
                  <TableRow
                                        android:layout_width="fill_parent"
                                        android:layout_height="wrap_content"
                                        android:layout_marginLeft="5dip"
                                        android:layout_marginRight="5dip">
                    <TextView
                                            android:id="@+id/txtData1"
                                            android:layout_width="170dip"
                                            android:layout_height="wrap_content"
                                            android:text="Data"
                                            android:textSize="14dip"
                                            android:textStyle="bold"
                                            android:textColor="#000000">
                    </TextView>
                    <TextView
                                            android:id="@+id/txtData2"
                                            android:layout_width="wrap_content"
                                            android:layout_height="wrap_content"
                                            android:text="Sample"
                                            android:textSize="13dip"
                                            android:textColor="#000000"></TextView>

                  </TableRow>
                </TableLayout>
                <ListView
                                    android:cacheColorHint="#00000000"
                                    android:id="@+id/ListView01"
                                    android:layout_height="wrap_content"
                                    android:layout_width="fill_parent" />
              </LinearLayout>
            </ScrollView>
            <LinearLayout
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:gravity="center_horizontal"
                            android:id="@+id/tab2"
                            android:orientation="vertical">
              <TableLayout
                                android:layout_height="wrap_content"
                                android:layout_width="fill_parent">
                <TableRow
                                    android:id="@+id/tableRow2"
                                    android:layout_marginLeft="2dip"
                                    android:layout_marginRight="5dip"
                                    android:layout_width="fill_parent"
                                    android:layout_height="wrap_content">
                  <LinearLayout
                                        android:layout_width="fill_parent"
                                        android:layout_height="wrap_content"
                                        android:orientation="horizontal">
                    <ImageView
                                            android:src="@drawable/ic_menu_invite"
                                            android:layout_width="40dip"
                                            android:layout_height="40dip"
                                            android:layout_gravity="center_vertical"></ImageView>
                    <TextView
                                            android:text="Sample"
                                            android:layout_height="wrap_content"
                                            android:layout_width="fill_parent"
                                            android:layout_centerVertical="true"
                                            android:textColor="?textColorDark"
                                            android:textSize="@dimen/editText"
                                            android:layout_gravity="center_vertical" />
                  </LinearLayout>
                </TableRow>
                <TableRow
                                    android:layout_width="fill_parent"
                                    android:layout_height="wrap_content"
                                    android:layout_marginLeft="5dip"
                                    android:layout_marginRight="5dip">
                  <TextView
                                        android:id="@+id/txtUser1"
                                        android:layout_width="170dip"
                                        android:layout_height="wrap_content"
                                        android:text="User"
                                        android:textSize="14dip"
                                        android:textStyle="bold"
                                        android:textColor="#000000">
                  </TextView>
                  <TextView
                                        android:id="@+id/txtUserData"
                                        android:layout_width="wrap_content"
                                        android:layout_height="wrap_content"
                                        android:text="UserData"
                                        android:textSize="13dip"
                                        android:textColor="#000000"></TextView>
                </TableRow>
              </TableLayout>

              <ListView
                                android:cacheColorHint="#00000000"
                                android:id="@+id/ListView02"
                                android:layout_height="wrap_content"
                                android:layout_width="fill_parent" />

            </LinearLayout>


          </FrameLayout>
        </LinearLayout>
      </TabHost>
    </LinearLayout>
  </ScrollView>
</RelativeLayout>

请谁能告诉我这里有什么问题?我在 ScrollView 问题中的 ListView 上有另一篇文章,但在我的情况下它们没有用。

【问题讨论】:

标签: android listview android-layout scrollview


【解决方案1】:

我找到了一个效果很好的解决方案,可以毫无问题地滚动ListView

ListView lv = (ListView)findViewById(R.id.myListView);  // your listview inside scrollview
lv.setOnTouchListener(new ListView.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            int action = event.getAction();
            switch (action) {
            case MotionEvent.ACTION_DOWN:
                // Disallow ScrollView to intercept touch events.
                v.getParent().requestDisallowInterceptTouchEvent(true);
                break;

            case MotionEvent.ACTION_UP:
                // Allow ScrollView to intercept touch events.
                v.getParent().requestDisallowInterceptTouchEvent(false);
                break;
            }

            // Handle ListView touch events.
            v.onTouchEvent(event);
            return true;
        }
    });

这样做是禁用ScrollView 上的TouchEvents 并使ListView 拦截它们。它很简单,而且一直有效。

【讨论】:

  • 这个有一个问题,它在没有 onitemclick 监听器的情况下运行良好,如果我也想要 OnItemClickListener 那么?
  • 这也适用于嵌套列表视图和可扩展列表视图。如果您有 SwipeRefreshLaout,则在一直向上滚动时会出现问题。一种简单的处理方法是通过父级获取 SwipeRefreshLayout 的实例,然后在向下禁用它,然后在向上重新启用它。
  • 您好@Moisés Olmedo 我正在尝试使用相同的方法,但获取此自定义视图 Horizo​​ntalListView 已调用 setOnTouchListener 但不会覆盖 performClick。有没有办法解决这个问题?
  • 这很棒——我用它来让 DragAndDropLinearLayout 在滚动视图中工作——我将这个监听器设置为拖动处理程序并禁用片段的滚动视图。唯一的区别是我将返回 false 而不是 true 以使拖放工作。
  • @MoisésOlmedo 我已经听从了你的回答。它在纵向模式下完美运行,但在横向模式下却不行。我在列表中有 11 项。它们都在纵向中可见,但在横向中只有 6 个。列表没有滚动。
【解决方案2】:

您不应该将ListView 放在ScrollView 中,因为ListView 类实现了它自己的滚动并且它只是不接收手势,因为它们都由父ScrollView 处理。我强烈建议您以某种方式简化布局。例如,您可以添加要滚动到 ListView 的视图作为页眉或页脚。

更新

从 API 级别 21 (Lollipop) 开始,Android SDK 正式支持嵌套滚动容器。在ViewViewGroup 类中有很多方法可以提供这个功能。要在 Lollipop 上进行嵌套滚动,您必须通过在其 XML 声明中添加 android:nestedScrollingEnabled="true" 或显式调用 setNestedScrollingEnabled(true) 来为子滚动视图启用它。

如果您想让嵌套滚动在棒棒糖之前的设备上工作,您可能会这样做,您必须使用支持库中的相应实用程序类。首先,您必须将 ScrollView 替换为 NestedScrollView。后者同时实现了NestedScrollingParentNestedScrollingChild,因此它可以用作父滚动容器或子滚动容器。

但是ListView不支持嵌套滚动,因此你需要继承它并实现NestedScrollingChild。幸运的是,Support 库提供了NestedScrollingChildHelper 类,因此您只需创建该类的实例并从视图类的相应方法中调用其方法即可。

【讨论】:

  • 感谢 Pixie,但您一定注意到我使用的 tabhost 有两个选项卡,每个选项卡都包含一个单独的列表视图。在这种情况下如何添加页眉或页脚?
  • 是的,将标签放入ListView 会很奇怪。但是你的布局非常非常复杂。我什至无法理解你想要它看起来像什么。无论如何,你最好让它更简单,因为 ListView 嵌套在 ScrollView 中,而 ScrollView 又嵌套在另一个 ScrollView 中,看起来很奇怪,而且不起作用。
  • 你好 Pixie,我删除了所有的滚动视图,现在列表视图滚动正在工作。但是,现在我面临另一个问题,当滚动列表视图时,它不会停止到该项目,我的意思是在列表视图上停止滚动后它会再次进入顶部。如何让列表视图显示滚动停止的项目?
  • 滚动视图内的自定义列表视图 stackoverflow.com/questions/18813296/….
  • 是的,可以嵌套滚动视图,但这也表明你的布局(和 UE 设计)有点复杂
【解决方案3】:

我也有一个解决方案。我总是使用这种方法。试试这个

<ScrollView
    android:id="@+id/createdrill_scrollView"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:layout_marginTop="15dp" >

            <net.thepaksoft.fdtrainer.NestedListView
                android:id="@+id/crewList"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_marginBottom="2dp"
                android:layout_weight="1"
                android:background="@drawable/round_shape"
                android:cacheColorHint="#00000000" >
            </net.thepaksoft.fdtrainer.NestedListView>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:layout_marginTop="15dp" >

            <net.thepaksoft.fdtrainer.NestedListView
                android:id="@+id/benchmarksList"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_marginBottom="2dp"
                android:layout_weight="1"
                android:background="@drawable/round_shape"
                android:cacheColorHint="#00000000" >
            </net.thepaksoft.fdtrainer.NestedListView>
        </LinearLayout>

</ScrollView>

NestedListView.java 类:

public class NestedListView extends ListView implements OnTouchListener, OnScrollListener {

    private int listViewTouchAction;
    private static final int MAXIMUM_LIST_ITEMS_VIEWABLE = 99;

    public NestedListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        listViewTouchAction = -1;
        setOnScrollListener(this);
        setOnTouchListener(this);
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem,
            int visibleItemCount, int totalItemCount) {
        if (getAdapter() != null && getAdapter().getCount() > MAXIMUM_LIST_ITEMS_VIEWABLE) {
            if (listViewTouchAction == MotionEvent.ACTION_MOVE) {
                scrollBy(0, -1);
            }
        }
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int newHeight = 0;
        final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        if (heightMode != MeasureSpec.EXACTLY) {
            ListAdapter listAdapter = getAdapter();
            if (listAdapter != null && !listAdapter.isEmpty()) {
                int listPosition = 0;
                for (listPosition = 0; listPosition < listAdapter.getCount()
                        && listPosition < MAXIMUM_LIST_ITEMS_VIEWABLE; listPosition++) {
                    View listItem = listAdapter.getView(listPosition, null, this);
                    //now it will not throw a NPE if listItem is a ViewGroup instance
                    if (listItem instanceof ViewGroup) {
                        listItem.setLayoutParams(new LayoutParams(
                                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                    }
                    listItem.measure(widthMeasureSpec, heightMeasureSpec);
                    newHeight += listItem.getMeasuredHeight();
                }
                newHeight += getDividerHeight() * listPosition;
            }
            if ((heightMode == MeasureSpec.AT_MOST) && (newHeight > heightSize)) {
                if (newHeight > heightSize) {
                    newHeight = heightSize;
                }
            }
        } else {
            newHeight = getMeasuredHeight();
        }
        setMeasuredDimension(getMeasuredWidth(), newHeight);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (getAdapter() != null && getAdapter().getCount() > MAXIMUM_LIST_ITEMS_VIEWABLE) {
            if (listViewTouchAction == MotionEvent.ACTION_MOVE) {
                scrollBy(0, 1);
            }
        }
        return false;
    }
}

【讨论】:

  • 这应该是公认的答案。虽然所有其他解决方案仅在列表项大小相同时才有效,但当列表项具有可变高度时,此解决方案优于那些解决方案。花了几个小时终于找到了这个。
  • Scrollview 只能承载一个直接子级。这怎么可能。
  • @MuhammadAamirALi,你能告诉我如何实例化 NestedListView 类吗?我不太明白 AttributSet 值应该是什么。谢谢。
  • 我发现了这个解决方案的一个主要问题。它破坏了列表视图的全部目的。它本质上执行所有列表视图的加载(不仅仅是可见视图)并更改高度以将它们全部吞没(更像LinearLayout而不是ListView。这意味着如果您有大量列表项(或复杂的列表项)然后android设备将花费大量时间来打开活动(甚至在我的情况下尝试崩溃)。
  • 有史以来最糟糕的主意 ...You've just recreated a very expensive LinearLayout :) – Romain Guy(google engineer) ...此解决方案仅适用于不完全了解 ListView ... @reubenjohn 您绝对正确
【解决方案4】:

你只需要用这个Custom ScrollView 替换你的&lt;ScrollView &gt;&lt;/ScrollView&gt;,比如&lt;com.tmd.utils.VerticalScrollview &gt; &lt;/com.tmd.utils.VerticalScrollview &gt;

package com.tmd.utils;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.ScrollView;

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:
                    Log.i("VerticalScrollview", "onInterceptTouchEvent: DOWN super false" );
                    super.onTouchEvent(ev);
                    break;

            case MotionEvent.ACTION_MOVE:
                    return false; // redirect MotionEvents to ourself

            case MotionEvent.ACTION_CANCEL:
                    Log.i("VerticalScrollview", "onInterceptTouchEvent: CANCEL super false" );
                    super.onTouchEvent(ev);
                    break;

            case MotionEvent.ACTION_UP:
                    Log.i("VerticalScrollview", "onInterceptTouchEvent: UP super false" );
                    return false;

            default: Log.i("VerticalScrollview", "onInterceptTouchEvent: " + action ); break;
        }

        return false;
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        super.onTouchEvent(ev);
        Log.i("VerticalScrollview", "onTouchEvent. action: " + ev.getAction() );
         return true;
    }
}

【讨论】:

  • @Atul 在设备上运行良好:Samsung captiavte i897、HTC Sensation、HTC Incredible,但在 HTC Desire x 上无法运行,可能是什么问题?
  • @Atul 但有时会在使用 3 或 4 根手指时滚动,但有时会滚动。
  • @pepela 你是对的。有时它不像只有一个滚动视图那么流畅。但它满足了我们的需求。
  • 滚动大listview时可以拦截所有动作。至于我,我使用这个补丁:pristalovpavel.wordpress.com/2014/12/26/…
【解决方案5】:

我有同样的问题,在谷歌搜索时我发现了你的问题。是的,标记的答案也对我有用,但存在一些问题。
无论如何,我找到了另一个 solution。 无需做任何杂耍就可以完美运行。

【讨论】:

  • 请在 SO 上发布答案,而不仅仅是链接到外部网站。
【解决方案6】:
list.setOnTouchListener(new OnTouchListener() {
    // Setting on Touch Listener for handling the touch inside ScrollView
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // Disallow the touch request for parent scroll on touch of child view
        v.getParent().requestDisallowInterceptTouchEvent(true);
        return false;
    }
});

【讨论】:

  • 这个的一个小问题是当列表视图到达末尾时我想继续滚动滚动视图,同时触摸它。
【解决方案7】:

我知道这个问题是很久以前提出的,但是现在遇到困难的人可以通过将此行添加到您的 ListView 来解决这个问题

android:nestedScrollingEnabled="true"

例如 -

                    <ListView
                    android:id="@+id/listView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:nestedScrollingEnabled="true" />

【讨论】:

    【解决方案8】:

    这是在 Scrollview 中使用 Listview 的确切方法。我们必须处理触摸事件。

          lstvNextTracks.setOnTouchListener(new OnTouchListener()
          {
              @Override
              public boolean onTouch(View v, MotionEvent event)
              {
             Log.e("Lisview *************", "focused");
             SCView.requestDisallowInterceptTouchEvent(true);
             return false;
              }
          });
    
    
    
    
        SCView.setOnTouchListener(new OnTouchListener()
        {
    
            @Override
            public boolean onTouch(View v, MotionEvent event)
            {
                int arr[] = new int[] { 1, 2 };
                lstvNextTracks.getLocationOnScreen(arr);
    
                /* Get bounds of child Listview*/
                int lstvTop = arr[0];
                int lstvBottom = arr[1] + lstvNextTracks.getHeight();
                int lstvLeft = arr[1];
                int lstvRight = arr[0] + lstvNextTracks.getWidth();
    
                float x = event.getRawX();
                float y = event.getRawY();
    
                if (event.getAction() == MotionEvent.ACTION_DOWN)
                {
                    /*check if child ListView bounds are touched*/
                    if (x > lstvTop && x < lstvBottom && y > lstvLeft && y < lstvRight)
                    {
                        SCView.clearFocus();
                        /*This statement tells the ScrollView to do not handle this touch event, so the child Listview will handle this touch event and will scroll */
                        SCView.requestDisallowInterceptTouchEvent(true);
                        /*The child Listview isFocusable attribute must be set to true otherwise it will not work*/
                        lstvNextTracks.requestFocus();
                        return true;
                    } else
                        return false;
                } else if (event.getAction() == MotionEvent.ACTION_MOVE)
                {
    
                    if (x > lstvTop && x < lstvBottom && y > lstvLeft && y < lstvRight)
                    {
                        SCView.clearFocus();
                        SCView.requestDisallowInterceptTouchEvent(true);
                        lstvNextTracks.requestFocus();
                        return true;
                    } else
                        return false;
                } else if (event.getAction() == MotionEvent.ACTION_UP)
                {
                    SCView.clearFocus();
                    SCView.requestDisallowInterceptTouchEvent(true);
                    lstvNextTracks.requestFocus();
                    return false;
                } else
                {
                    return false;
                }
            }
        });
    

    【讨论】:

    • 当滚动区域超出触摸区域时有一个bug
    【解决方案9】:

    使用下面的方法,尽情享受吧!

    private void setListViewScrollable(final ListView list) {
        list.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                listViewTouchAction = event.getAction();
                if (listViewTouchAction == MotionEvent.ACTION_MOVE) {
                    list.scrollBy(0, 1);
                }
                return false;
            }
        });
    
        list.setOnScrollListener(new OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
    
            }
    
            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                if (listViewTouchAction == MotionEvent.ACTION_MOVE) {
                    list.scrollBy(0, -1);
                }
            }
        });
    }
    

    listViewTouchAction 是一个全局整数值。 如果可以换行

    list.scrollBy(0, 1);
    

    还有其他的请与我们分享。

    享受吧!

    【讨论】:

    • 非常接近。多次还是不行,但至少有几次能行。
    • 它的工作,但问题在于滚动的速度。它非常滞后。
    【解决方案10】:

    将两个不同的滚动视图放在一起是一种不好的做法。 ListView 本身有自己的滚动功能,并且高度会根据您的行项目的适配器设置自动调整(请记住,我们没有在 Layout xml 中为 ListView 设置特定高度)。但是,在您的情况下,您可以将 ListView 替换为一个类,这将调整您的 ListView 的高度,请记住您的 ListView 在 ScrollView 内。

    这将对您有所帮助:

    public class ExpandableHeightListview extends ListView
    {
    
        boolean expanded = false;
    
        public ExpandableHeightListview(Context context)
        {
            super(context);
        }
    
        public ExpandableHeightListview(Context context, AttributeSet attrs)
        {
            super(context, attrs);
        }
    
        public ExpandableHeightListview(Context context, AttributeSet attrs,int defStyle)
        {
            super(context, attrs, defStyle);
        }
    
        public boolean isExpanded()
        {
            return expanded;
        }
    
        @Override
        public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
        {
            // HACK! TAKE THAT ANDROID!
            if (isExpanded())
            {
                // Calculate entire height by providing a very large height hint.
                // But do not use the highest 2 bits of this integer; those are
                // reserved for the MeasureSpec mode.
                int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
                super.onMeasure(widthMeasureSpec, expandSpec);
    
                ViewGroup.LayoutParams params = getLayoutParams();
                params.height = getMeasuredHeight();
            }
            else
            {
                super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            }
        }
    
        public void setExpanded(boolean expanded)
        {
            this.expanded = expanded;
        }
    }
    

    现在用这个类扩展你的 ListView,只需将你的 ListView 标签从你的资源 xml 更改为像这样,

    <yourpackagename.ExpandableHeightListview
                android:id="@+id/listView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/White"
                android:scrollbars="none"
                android:orientation="vertical"
                android:fadingEdge="none">
                </cyourpackagename.ExpandableHeightListview>
    

    这将解决您的 ListView 滚动问题,因为现在父滚动由 ScrollView 而不是 ListView 管理。

    【讨论】:

    • 这是否允许 ListView 在 NestedScrollView 内滚动??
    • 是的,它会允许的。通过此解决方案,listview 滚动属性将被禁用,父滚动视图将负责滚动 listview 内容。希望对您有所帮助。
    【解决方案11】:

    我有类似的问题,并通过使用 ListView 扩展创建自定义类来解决。

    ScrollableListView.java

    import android.content.Context;
    import android.util.AttributeSet;
    import android.widget.ListView;
    
    public class ScrollableListView extends ListView {
    
        public ScrollableListView (Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public ScrollableListView (Context context) {
            super(context);
        }
    
        public ScrollableListView (Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
    
        @Override
        public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
                    MeasureSpec.AT_MOST);
            super.onMeasure(widthMeasureSpec, expandSpec);
        }
    }
    

    用法:

    <com.my.package.ScrollableListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    

    【讨论】:

    • UIHaq 这是否允许 ListView 在 NestedScrollView 内滚动??
    • 它不适用于多级 ExpandableListview,如 3 级或 4 级。有什么帮助吗??
    【解决方案12】:

    添加: android:nestedScrollingEnabled="true"

    【讨论】:

    • 确认!只添加这个就足够了,要么列表在滚动视图内,要么滚动视图在列表视图内
    【解决方案13】:

    Demo_ListView_In_ScrollView

    ================================

                package com.app.custom_seekbar;
    
                import java.util.ArrayList;
    
                import android.app.Activity;
                import android.os.Bundle;
                import android.widget.ListView;
    
                public class Demo_ListView_In_ScrollView  extends Activity
                {
                    ListView listview;
                    ArrayList<String> data=null;
                    listview_adapter adapter=null;
    
                    @Override
                    protected void onCreate(Bundle savedInstanceState) 
                    {
                        // TODO Auto-generated method stub
                        super.onCreate(savedInstanceState);
                        super.setContentView(R.layout.demo_listview_in_scrollview_activity);
                        init();
                        set_data();
                        set_adapter();
    
    
                    }
                    public void init()
                    {
                        listview=(ListView)findViewById(R.id.listView1);
                        data=new ArrayList<String>();
    
                    }
                    public void set_data()
                    {
                        data.add("Meet");
                        data.add("prachi");
                        data.add("shailesh");
                        data.add("manoj");
                        data.add("sandip");
                        data.add("zala");
                        data.add("tushar");
                        data.add("Meet");
                        data.add("prachi");
                        data.add("shailesh");
                        data.add("manoj");
                        data.add("sandip");
                        data.add("zala");
                        data.add("tushar");
                        data.add("Meet");
                        data.add("prachi");
                        data.add("shailesh");
                        data.add("manoj");
                        data.add("sandip");
                        data.add("zala");
                        data.add("tushar");
                        data.add("Meet");
                        data.add("prachi");
                        data.add("shailesh");
                        data.add("manoj");
                        data.add("sandip");
                        data.add("zala");
                        data.add("tushar");
                        data.add("Meet");
                        data.add("prachi");
                        data.add("shailesh");
                        data.add("manoj");
                        data.add("sandip");
                        data.add("zala");
                        data.add("tushar");
                        data.add("Meet");
                        data.add("prachi");
                        data.add("shailesh");
                        data.add("manoj");
                        data.add("sandip");
                        data.add("zala");
                        data.add("tushar");
                        data.add("Meet");
                        data.add("prachi");
                        data.add("shailesh");
                        data.add("manoj");
                        data.add("sandip");
                        data.add("zala");
                        data.add("tushar");
                        data.add("Meet");
                        data.add("prachi");
                        data.add("shailesh");
                        data.add("manoj");
                        data.add("sandip");
                        data.add("zala");
                        data.add("tushar");
                        data.add("Meet");
                        data.add("prachi");
                        data.add("shailesh");
                        data.add("manoj");
                        data.add("sandip");
                        data.add("zala");
                        data.add("tushar");
                        data.add("Meet");
                        data.add("prachi");
                        data.add("shailesh");
                        data.add("manoj");
                        data.add("sandip");
                        data.add("zala");
                        data.add("tushar");
                        data.add("Meet");
                        data.add("prachi");
                        data.add("shailesh");
                        data.add("manoj");
                        data.add("sandip");
                        data.add("zala");
                        data.add("tushar");
                        data.add("Meet");
                        data.add("prachi");
                        data.add("shailesh");
                        data.add("manoj");
                        data.add("sandip");
                        data.add("zala");
                        data.add("tushar");
                        data.add("Meet");
                        data.add("prachi");
                        data.add("shailesh");
                        data.add("manoj");
                        data.add("sandip");
                        data.add("zala");
                        data.add("tushar");
    
    
    
                    }
    
                    public void set_adapter()
                    {
                        adapter=new listview_adapter(Demo_ListView_In_ScrollView.this,data);
                        listview.setAdapter(adapter);
                        Helper.getListViewSize(listview); // set height of listview according to Arraylist item  
                    }
    
                }
    

    ==========================

    listview_adapter

    ===========================

            package com.app.custom_seekbar;
    
            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 listview_adapter  extends ArrayAdapter<String>  
            {
                private final Activity context;
                ArrayList<String>data;
                class ViewHolder 
                {
                    public TextView tv_name;
                    public ScrollView scroll;
                    public LinearLayout l1;
                }
    
    
                public listview_adapter(Activity context, ArrayList<String> all_data) {
                    super(context, R.layout.item_list_xml, 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_xml, null);
    
                        viewHolder = new ViewHolder();
    
                        viewHolder.tv_name=(TextView)rowView.findViewById(R.id.textView1);
    
                        rowView.setTag(viewHolder);
                    }
                    else
                    viewHolder = (ViewHolder) rowView.getTag();
    
                    viewHolder.tv_name.setText(data.get(position).toString());
                    return rowView;
    
                }
    
    
            }
    

    ====================================

    助手类

    ======================================

        import android.util.Log;
        import android.view.View;
        import android.view.ViewGroup;
        import android.widget.ListAdapter;
        import android.widget.ListView;
    
        public class Helper {
            public static void getListViewSize(ListView myListView) {
                ListAdapter myListAdapter = myListView.getAdapter();
                if (myListAdapter == null) {
                    //do nothing return null
                    return;
                }
                //set listAdapter in loop for getting final size
                int totalHeight = 0;
                for (int size = 0; size < myListAdapter.getCount(); size++) {
                    View listItem = myListAdapter.getView(size, null, myListView);
                    listItem.measure(0, 0);
                    totalHeight += listItem.getMeasuredHeight();
                }
              //setting listview item in adapter
                ViewGroup.LayoutParams params = myListView.getLayoutParams();
                params.height = totalHeight + (myListView.getDividerHeight() *                   (myListAdapter.getCount() - 1));
                myListView.setLayoutParams(params);
                // print height of adapter on log
                Log.i("height of listItem:", String.valueOf(totalHeight));
            }
        }
    

    ==========================

    demo_listview_in_scrollview_activity.xml

    ==========================

            <?xml version="1.0" encoding="utf-8"?>
            <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical" >
    
                <ScrollView
                    android:id="@+id/scrollView1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" >
    
                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:orientation="vertical" >
    
                        <ListView
                            android:id="@+id/listView1"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content" >
                        </ListView>
                    </LinearLayout>
                </ScrollView>
    
            </LinearLayout>
    

    =================

    item_list_xml.xml

    ===================

            <?xml version="1.0" encoding="utf-8"?>
            <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical" >
    
                <TextView
                    android:id="@+id/textView1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:gravity="center"
                    android:text="TextView"
                    android:textSize="14sp" />
    
            </LinearLayout>
    

    【讨论】:

    • 你可能应该解释一下,一大堆代码还不如没有解释的答案。
    • 我习惯了这个解决方案。但是这个解决方案并不好。因为我的列表有 10 项,但它显示 9.5 或 9 项。
    【解决方案14】:

    requestDisallowInterceptTouchEvent (boolean disallowIntercept)

    当子级不希望此父级及其祖先使用 onInterceptTouchEvent(MotionEvent) 拦截触摸事件时调用。 该父级应将此调用传递给其父级。此父级必须在触摸期间遵守此请求(即,仅在此父级收到 up 或 cancel 后清除标志。

    试试这个答案,

        listview.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
    
    // Disallow the touch request for parent scroll on touch of child view
                scrollView.requestDisallowInterceptTouchEvent(true);
    
                int action = event.getActionMasked();
                switch (action) {
                    case MotionEvent.ACTION_UP:
                        scrollView.requestDisallowInterceptTouchEvent(false);
                        break;
                }
                return false;
            }
        });
    

    【讨论】:

      【解决方案15】:

      最好的解决方案是将 NestedScrollVew 与 RecyclerView 一起使用,或者如果您想与 Listview 一起使用,则可以在其中添加页眉和页脚视图。 例如:

      View footerView = ((LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.outofoffice_footer_view, null, false);
      infoListView.addFooterView(footerView);
      

      【讨论】:

        【解决方案16】:

        我已经尝试并测试了上面提到的几乎所有方法,相信我,在完全脱离 RecyclerView 之后,我用 RecyclerView 替换了我的 ListView,它运行良好。不需要任何用于 ExtendedHeightListView 的第 3 方库,只需简单明了的 RecyclerView。

        这是我在 recyclerView 之前的布局文件:

        <?xml version="1.0" encoding="utf-8"?>
        
        <ScrollView
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/scrollView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:elevation="5dp">
        
        <RelativeLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            xmlns:tools="http://schemas.android.com/tools"
            android:id="@+id/relativeLayoutre"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="2dp"
            tools:context="com.example.android.udamovappv3.activities.DetailedActivity">
        
            <android.support.v7.widget.Toolbar
                android:id="@+id/my_toolbar_detail"
                android:layout_width="match_parent"
                android:layout_height="56dp"
                android:layout_gravity="top"
                android:background="?attr/colorPrimary"
                android:elevation="4dp"
                android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
                app:layout_constraintHorizontal_bias="0.0"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
        
            <TextView
                android:id="@+id/title_name"
                android:layout_width="fill_parent"
                android:layout_height="128dp"
                android:layout_alignParentStart="true"
                android:layout_marginTop="59dp"
                android:background="#079ED9"
                android:gravity="left|center"
                android:padding="25dp"
                android:text="Name"
                android:textColor="#ffffff"
                android:textSize="30sp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintHorizontal_bias="1.0"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="@+id/my_toolbar_detail"
                app:layout_constraintVertical_bias="0.0"
                tools:layout_editor_absoluteX="0dp" />
        
        
            <ImageView
                android:id="@+id/iv_poster"
                android:layout_width="131dp"
                android:layout_height="163dp"
                android:layout_alignStart="@+id/my_toolbar_detail"
                android:layout_below="@+id/title_name"
                android:layout_marginBottom="15dp"
                android:layout_marginRight="8dp"
                android:layout_marginTop="15dp"
                app:layout_constraintHorizontal_bias="0.0"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:srcCompat="@mipmap/ic_launcher" />
        
            <Button
                android:id="@+id/bt_mark_as_fav"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignBottom="@+id/iv_poster"
                android:layout_alignParentEnd="true"
                android:layout_marginBottom="11dp"
                android:layout_marginEnd="50dp"
                android:background="#079ED9"
                android:text="Mark As \n Favorite"
                android:textSize="10dp" />
        
            <TextView
                android:id="@+id/tv_runTime"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignStart="@+id/tv_rating"
                android:layout_below="@+id/tv_releaseDate"
                android:layout_marginTop="11dp"
                android:text="TextView"
                android:textSize="20sp" />
        
            <TextView
                android:id="@+id/tv_releaseDate"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignStart="@+id/tv_runTime"
                android:layout_alignTop="@+id/iv_poster"
                android:text="TextView"
                android:textSize="25dp" />
        
            <TextView
                android:id="@+id/tv_rating"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignStart="@+id/bt_mark_as_fav"
                android:layout_below="@+id/tv_runTime"
                android:layout_marginTop="11dp"
                android:text="TextView" />
        
            <TextView
                android:id="@+id/tv_overview"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@+id/iv_poster"
                android:layout_centerHorizontal="true"
                android:layout_marginBottom="5dp"
                android:foregroundGravity="center"
                android:text="adasdasdfadfsasdfasdfasdfasdfnb agfjuanfalsbdfjbdfklbdnfkjasbnf;kasbdnf;kbdfas;kdjabnf;lbdnfo;aidsnfl';asdfj'plasdfj'pdaskjf'asfj'p[asdfk"
                android:textColor="#000000"
                 />
        
        
            <RelativeLayout
                android:id="@+id/foodItemActvity_linearLayout_fragments"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:layout_below="@+id/tv_overview">
                <TextView
                    android:id="@+id/fragment_dds_review_textView_label"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Reviews:"
        
                    android:textAppearance="?android:attr/textAppearanceMedium" />
                <com.github.paolorotolo.expandableheightlistview.ExpandableHeightListView
                    android:id="@+id/expandable_listview"
                    android:layout_width="fill_parent"
                    android:layout_height="match_parent"
                    android:layout_alignParentBottom="true"
                    android:layout_alignParentStart="true"
                    android:layout_below="@+id/fragment_dds_review_textView_label"
                    android:padding="8dp">
        
                </com.github.paolorotolo.expandableheightlistview.ExpandableHeightListView>
            </RelativeLayout>
        </RelativeLayout>
        </ScrollView>
        

        这是在用上面提到的众多解决方案之一替换我的列表视图之后。所以问题是由于android中的2个滚动视图错误(可能不是错误),列表视图行为不正确。

        我换了 使用回收站视图形成我的最终布局。

        这是我的回收站视图适配器:

        public class TrailerAdapter extends RecyclerView.Adapter<TrailerAdapter.TrailerAdapterViewHolder> {
        
        private ArrayList<String> Youtube_URLS;
        
        private Context Context;
        
        public TrailerAdapter(Context context, ArrayList<String> Youtube_URLS){
            this.Context = context;
            this.Youtube_URLS = Youtube_URLS;
        }
        @Override
        public TrailerAdapter.TrailerAdapterViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.trailer_layout, parent, false);
            return new TrailerAdapterViewHolder(view);
        }
        
        @Override
        public void onBindViewHolder(TrailerAdapter.TrailerAdapterViewHolder holder, int position) {
            Picasso.with(Context).load(R.drawable.ic_play_arrow_black_24dp).into(holder.iv_playbutton);
            holder.item_id.setText(Youtube_URLS.get(position));
        }
        
        @Override
        public int getItemCount() {
            if(Youtube_URLS.size()==0){
                return 0;
            }else{
                return Youtube_URLS.size();
            }
        }
        
        public class TrailerAdapterViewHolder extends RecyclerView.ViewHolder {
            ImageView iv_playbutton;
            TextView item_id;
        
            public TrailerAdapterViewHolder(View itemView) {
                super(itemView);
                iv_playbutton = (ImageView)itemView.findViewById(R.id.play_button);
                item_id = (TextView)itemView.findViewById(R.id.tv_trailer_sequence);
            }
        }
        }
        

        这是我的 RecyclerView 自定义布局:

        <RelativeLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="56dp"
            android:padding="6dip" >
        
            <ImageView
                android:id="@+id/play_button"
                android:layout_width="70dp"
                android:layout_height="wrap_content"
                android:layout_marginRight="6dip"
                android:layout_marginStart="12dp"
                android:src="@drawable/ic_play_arrow_black_24dp"
                android:layout_centerVertical="true"
                android:layout_alignParentStart="true" />
        
            <TextView
                android:id="@+id/tv_trailer_sequence"
                android:layout_width="wrap_content"
                android:layout_height="26dip"
                android:layout_centerVertical="true"
                android:layout_toEndOf="@+id/play_button"
                android:ellipsize="marquee"
                android:gravity="center"
                android:maxLines="1"
                android:text="Description"
                android:textSize="12sp" />
        
        </RelativeLayout>
        

        瞧,我在 scollview 中得到了 ListView(Now RecyclerView) 的预期效果。 Heres the Final Image of the UI

        最后一点,我相信替换 RecyclerView 对我来说是一个更好的选择,因为它提高了应用的整体稳定性,也帮助我更好地理解了 RecyclerView。如果我要提出解决方案,我会说用 RecyclerView 替换您的 ListView。

        【讨论】:

          【解决方案17】:

          我遇到了这个错误。我的解决方案如下:

          1.创建一个不可滚动的自定义列表视图

          public class NonScrollListView extends ListView {
          
          public NonScrollListView(Context context) {
              super(context);
          }
          public NonScrollListView(Context context, AttributeSet attrs) {
              super(context, attrs);
          }
          public NonScrollListView(Context context, AttributeSet attrs, int defStyle) {
              super(context, attrs, defStyle);
          }
          @Override
          public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
                  int heightMeasureSpec_custom = MeasureSpec.makeMeasureSpec(
                          Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
                  super.onMeasure(widthMeasureSpec, heightMeasureSpec_custom);
                  ViewGroup.LayoutParams params = getLayoutParams();
                  params.height = getMeasuredHeight();    
          }
          

          }

          2。将上述自定义类用于 xml 文件

            <com.Example.NonScrollListView
                  android:id="@+id/lv_nonscroll_list"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content" >
              </com.Example.NonScrollListView>
          

          希望对你最好。

          【讨论】:

          • 这是否允许 ListView 在 NestedScrollView 内滚动?我认为它会根据项目大小调整 Listview 的大小。
          • 它不适用于多级 ExpandableListview,如 3 级或 4 级。有什么帮助吗??
          【解决方案18】:

          @Shailesh Rohit 提供的上述解决方案效果很好。必须做一些技巧。

          1. 如果您将助手类放在同一个类(主类)中,则将助手类设置为静态,getListViewSize() 不是静态的。

          2. 最重要的是,写“Helper.getListViewSize(listView);”第一次设置适配器后的语句,如“listView.setAdapter(myAdapter);”以及您何时使用“myAdapter.notifyDataSetChanged();”

          3. 用法如下所示。

            listView = (ListView) findViewById(R.id.listView); myAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, listValues); listView .setAdapter(myAdapter); Helper.getListViewSizelistView(listView);

            myAdapter.notifyDataSetChanged(); Helper.getListViewSizelistView(listView);

          【讨论】:

            【解决方案19】:

            最佳代码

            <android.support.v4.widget.NestedScrollView
            android:id="@+id/scrollView1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@+id/btmlyt"
            android:layout_below="@+id/deshead_tv">
            
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
               android:orientation="vertical"
               >
            
            <TextView
                android:id="@+id/des_tv"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_above="@+id/btmlyt"
                android:background="@android:color/white"
                android:paddingLeft="3dp"
                android:paddingRight="3dp"
                android:scrollbars="vertical"
                android:paddingTop="3dp"
                android:text="description"
                android:textColor="@android:color/black"
                android:textSize="18sp" />
            </LinearLayout>
            
            </android.support.v4.widget.NestedScrollView>
            

            【讨论】:

              【解决方案20】:

              这是最好的例子

              添加 NestedScrollView 而不是 Scrollview

              在ListView中添加这一行android:nestedScrollingEnabled="true"

               <androidx.core.widget.NestedScrollView
                     android:layout_width="match_parent"
                     android:layout_height="match_parent"
                     >       
                      <ListView
                           android:layout_width="match_parent"
                           android:layout_height="wrap_content"
                           android:nestedScrollingEnabled="true"
                           />          
              </androidx.core.widget.NestedScrollView>
              

              【讨论】:

                【解决方案21】:

                根据我的说法,当您使用 setOnTouchListener om parent 或 child 时,当您触摸子项时停止滚动父项,否则当您触摸父项时停止滚动子项

                【讨论】:

                  【解决方案22】:

                  用 ScrollView 试试这个,而不是 ListView。

                  public class xScrollView extends ScrollView
                  {
                      ;
                      ;
                      @Override
                      public boolean onInterceptTouchEvent (MotionEvent ev)
                      {
                          return false;
                      }
                  }
                  

                  【讨论】:

                  • 你能格式化代码吗?答案有带分号的空行,没有 cmets
                  • 如果您对扩展的 ScrollView 做的不多,则不需要在带有分号的行中写一些东西。所以只需删除它们。并假设您的包名 'com.me',然后在布局 XML 中,使用 '
                  【解决方案23】:

                  在 ScrollView 中用 RecycleView 替换 ListView。无需额外的源代码即可顺利运行:

                   <ScrollView 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:paddingBottom="@dimen/activity_vertical_margin"
                      android:paddingLeft="@dimen/activity_horizontal_margin"
                      android:paddingRight="@dimen/activity_horizontal_margin"
                      android:paddingTop="@dimen/activity_vertical_margin"
                      tools:context="com.example.hung.recycleviewtest.MainActivityFragment"
                      >
                      <RelativeLayout
                          android:layout_width="match_parent"
                          android:layout_height="wrap_content">
                          <android.support.v7.widget.RecyclerView
                              android:id="@+id/recycle_view"
                              android:layout_width="match_parent"
                              android:layout_height="400dp"
                              android:background="@android:color/darker_gray"/>
                          <android.support.v7.widget.RecyclerView
                              android:id="@+id/recycle_view_a"
                              android:layout_marginTop="40dp"
                              android:layout_below="@id/recycle_view"
                              android:layout_width="match_parent"
                              android:layout_height="400dp"
                              android:background="@android:color/darker_gray"/>
                      </RelativeLayout>
                  
                  </ScrollView>
                  

                  【讨论】:

                    【解决方案24】:

                    对于 ScrollView 内的 ListView 使用 NestedScrollView 它可以处理这个 功能非常简单:

                    <android.support.v4.widget.NestedScrollView
                            android:layout_width="match_parent"
                            android:layout_height="match_parent">
                    
                            <LinearLayout
                                android:layout_width="match_parent"
                                android:layout_height="match_parent"
                                android:orientation="vertical">
                    
                                <android.support.v7.widget.RecyclerView
                                    android:id="@+id/recycler_view"
                                    android:layout_width="match_parent"
                                    android:layout_height="match_parent"
                                    android:padding="5dip"/>
                    
                            </LinearLayout>
                        </android.support.v4.widget.NestedScrollView>
                    

                    【讨论】:

                      【解决方案25】:

                      我找到了解决方案, 不使用scrollview,可以使用listview的addHeaderview和addFooterview

                      这是我的sn-p,

                      //  create separate layout and add it dynamically
                          scrollview=getLayoutInflater().inflate(R.layout.yourlayout,null);
                          lv=(ListView)listview.findViewById(R.id.listView2);
                          lv.addHeaderView(scrollview);
                          lv.setContentView(lv);
                      

                      现在,使用列表视图,您的布局也可以滚动。好好享受吧!!!

                      【讨论】:

                        【解决方案26】:

                        将 ScrollView 替换为 xml 中的 android.support.v4.widget.NestedScrollView。它运行

                        1) 在 XML::::android.support.v4.widget.NestedScrollView 中使用

                        而不是:::: ScrollView

                        2) 并在 cs 文件中使用 NonScrollListView 以这种方式用于列表视图:

                        public class NonScrollListView extends ListView {    
                        
                            public NonScrollListView(Context context) {    
                                super(context);
                            }    
                            public NonScrollListView(Context context, AttributeSet attrs) {
                                super(context, attrs);
                            }
                            public NonScrollListView(Context context, AttributeSet attrs, int defStyle) {
                                super(context, attrs, defStyle);
                            }
                            @Override
                            public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
                                int heightMeasureSpec_custom = MeasureSpec.makeMeasureSpec(
                                        Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
                                super.onMeasure(widthMeasureSpec, heightMeasureSpec_custom);
                                ViewGroup.LayoutParams params = getLayoutParams();
                                params.height = getMeasuredHeight();
                            }    
                        }    
                        

                        3) 最后用这段代码来识别cs文件中的scrollview:

                        NonScrollListView listView = (NonScrollListView) view.findViewById(R.id.listview);
                        

                        【讨论】:

                        • 这是否允许 ListView 在 NestedScrollView 内滚动??
                        • 它不适用于多级 ExpandableListview,如 3 级或 4 级。有什么帮助吗??
                        【解决方案27】:

                        使用此方法,您的列表视图将在滚动视图中滚动:-

                           ListView lstNewsOffer.setAdapter(new ViewOfferAdapter(
                                                    ViewNewsDetail.this, viewOfferList));
                                            getListViewSize(lstNewsOffer);
                        
                        void getListViewSize(ListView myListView) {
                            ListAdapter myListAdapter = myListView.getAdapter();
                            if (myListAdapter == null) {
                                // do nothing return null
                                return;
                            }
                            // set listAdapter in loop for getting final size
                            int totalHeight = 0;
                            for (int size = 0; size < myListAdapter.getCount(); size++) {
                                View listItem = myListAdapter.getView(size, null, myListView);
                                listItem.measure(0, 0);
                                totalHeight += listItem.getMeasuredHeight();
                            }
                            // setting listview item in adapter
                            ViewGroup.LayoutParams params = myListView.getLayoutParams();
                            params.height = totalHeight
                                    + (myListView.getDividerHeight() * (myListAdapter.getCount() - 1));
                            myListView.setLayoutParams(params);
                            // print height of adapter on log
                            Log.i("height of listItem:", String.valueOf(totalHeight));
                        }
                        

                        【讨论】:

                          【解决方案28】:

                          您不能在滚动视图中添加 ListView,因为列表视图也会 scolls,并且列表视图滚动和滚动视图 scoll 之间会存在同步问题。 您可以制作一个 CustomList 视图并将此方法添加到其中。

                          @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;
                          }
                          

                          【讨论】:

                            【解决方案29】:

                            不要给ListView滚动视图,因为它有默认设置

                            <ListView
                                android:cacheColorHint="#00000000"
                                android:id="@+id/ListView01"
                                android:layout_height="wrap_content"
                                android:layout_width="fill_parent" />
                            

                            【讨论】:

                              猜你喜欢
                              • 1970-01-01
                              • 1970-01-01
                              • 1970-01-01
                              • 2015-07-29
                              • 2013-10-01
                              • 1970-01-01
                              • 2016-09-03
                              • 1970-01-01
                              • 1970-01-01
                              相关资源
                              最近更新 更多