【问题标题】:LinearLayout not scrolling inside ScrollView [duplicate]LinearLayout不在ScrollView内滚动[重复]
【发布时间】:2020-09-04 22:06:32
【问题描述】:

我试图弄清楚为什么这个LinearLayout 没有滚动:

<ScrollView
    android:layout_width="match_parent"
    android:fillViewport="true"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent">

   <LinearLayout
    android:id="@+id/linearLayout"
    android:orientation="vertical"
    android:weightSum="2"
    android:gravity="center_horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="35dp"
        android:text="TITLE 1"
        android:textSize="30sp" />

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20sp" />

</LinearLayout>
</ScrollView>

我以编程方式将textView 和listView 添加到linearLayout,页面被切成两半。唯一滚动的是listViews。

我不需要 listViews 可滚动,我只希望整个 linearLayout 可滚动。

【问题讨论】:

  • 是的,如您所见,我解决了寻找该答案的问题,没有接受的答案,而是第二个。不过谢谢
  • 好的,我标记为重复,我的解决方案是另一个帖子的第二个答案。

标签: android xml android-layout android-linearlayout android-scrollview


【解决方案1】:

试试这个代码

<androidx.core.widget.NestedScrollView
    android:layout_width="match_parent"
    android:fillViewport="true"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent">

   <LinearLayout
    android:id="@+id/linearLayout"
    android:orientation="vertical"
    android:weightSum="2"
    android:gravity="center_horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="35dp"
        android:text="TITLE 1"
        android:textSize="30sp" />

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20sp" />

</LinearLayout>
</androidx.core.widget.NestedScrollView>

【讨论】:

  • 试过了但是没用..
  • 我用更多细节更新了我的问题
【解决方案2】:

列表视图不会滚动,因为它是嵌套的。 如果您在 Scrollview 中使用 listview,它们可能会互相打断。 (两个卷轴) 您应该自定义您的 Scrollview,因此当您触摸 listview 区域时,scrollview 将停止滚动。

1) 创建新类:CustomScrollView 并从 ScrollView 扩展它

2) 将您的 xml &lt;ScrollView&gt; 更改为您的自定义滚动视图:&lt;com.your_package_name.CustomScrollView&gt;

CustomScrollView.java:


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

public class CustomScrollView extends ScrollView {
    public CustomScrollView(Context context) {
        super(context);
    }

    public CustomScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

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

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

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

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

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

        return false;
    }

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

【讨论】:

  • 我试过了,但它没有滚动...我复制了你的课程并更改了 xml
  • 我用更多细节更新了我的问题
  • 我理解你。但是你只有2个视图,如果在listview之后什么都没有,你怎么能滚动它?您可以通过一种方式:禁用 listView -> stackoverflow.com/questions/7611085/…
  • 正如我在帖子中所写:“我正在以编程方式将 textView 和 listView 添加到 linearLayout”......我终于找到了解决方案,我将其发布为答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多