我希望整个 activity 都可以滚动,但只有列表视图可以滚动。我怎么做?

I want the entire activity to be scrollable but only listview is scrollable. How do I do that?

我从服务器中提取数据到我的 listviewlistviewactivity 中。 activity 上面有按钮。

当接收到数据和滚动数据时,只有 listview 被滚动。 buttons 固定在顶部。

我希望 listview 获得所需的垂直高度,这样我就需要滚动 activity 而不是 listview

我的 XML 代码:

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

<RelativeLayout android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
            <TextView
                android:text="text1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/view_c_name"
                android:textSize="18sp"
                android:textStyle="bold"/>

            <TextView
                android:text="text4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/view_c_name"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:id="@+id/view_c_motto" />

            <TextView
                android:text="text5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/view_c_motto"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:id="@+id/view_c_details" />

            <TextView
                android:text="text2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/view_c_address"
                android:layout_below="@+id/view_c_details"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true" />

            <TextView
                android:text="text3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/view_c_act_text"
                android:layout_below="@+id/view_c_address"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_marginTop="71dp" />

            <Button
                android:text="Button 1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="16dp"
                android:layout_marginStart="16dp"
                android:id="@+id/view_button"
                android:layout_below="@+id/view_c_address"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true" />

            <Button
                android:text="button 2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignBottom="@+id/view_button"
                android:layout_toRightOf="@+id/view_button"
                android:layout_toEndOf="@+id/view_button"
                android:layout_marginLeft="16dp"
                android:layout_marginStart="16dp"
                android:id="@+id/view_button2" />

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/view_c_act_text"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:id="@+id/my_listview"
        android:scrollbars="none"
        />

</RelativeLayout>

将您现有的布局包裹在 ScrollView 中以滚动整个内容,而不仅仅是 ListView

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- your existing layout here, change your top container's height from match_parent to wrap_content -->

</ScrollView>

我遇到了类似的问题然后我创建了一个 class

public class ListViewExpanded extends ListView {
    public ListViewExpanded(Context context, AttributeSet attrs) {
        super(context, attrs);
        setDividerHeight(0);
    }

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST));
    }
}

然后我在 xml 中使用了它,尝试一下肯定会有帮助

<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
  <TextView
            android:id="@+id/text_goal_scored"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
          />
<example.ListViewExpanded
                android:id="@+id/list_home"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1">
</example.ListViewExpanded>
</android.support.v4.widget.NestedScrollView>

这就是您的方法 task.this 已禁用滚动列表视图,因此您可以轻松滚动 activity。

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

/**
 * Created by sudesh Regmi on 4/20/2017.
 */

public class ScrollDisabledListView extends ListView {
    private int mPosition;

public ScrollDisabledListView(Context context) {
    super(context);
}

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

public ScrollDisabledListView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    final int actionMasked = ev.getActionMasked() & MotionEvent.ACTION_MASK;

    if (actionMasked == MotionEvent.ACTION_DOWN) {
        // Record the position the list the touch landed on
        mPosition = pointToPosition((int) ev.getX(), (int) ev.getY());
        return super.dispatchTouchEvent(ev);
    }

    if (actionMasked == MotionEvent.ACTION_MOVE) {
        // Ignore move events
        return true;
    }

    if (actionMasked == MotionEvent.ACTION_UP) {
        // Check if we are still within the same view
        if (pointToPosition((int) ev.getX(), (int) ev.getY()) == mPosition) {
            super.dispatchTouchEvent(ev);
        } else {
            // Clear pressed state, cancel the action
            setPressed(false);
            invalidate();
            return true;
        }
    }

    return super.dispatchTouchEvent(ev);
}

@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
            MeasureSpec.AT_MOST);
    super.onMeasure(widthMeasureSpec, expandSpec);
}
}

试试这个

<ScrollView
    android:id="@+id/scrollViewMain"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
         android:id="@+id/linearLayoutMain"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:orientation="vertical">

         <Button
              android:text="Button"
              android:id="@+id/buttonMain"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content">

    </LinearLayout>
</ScrollView>

完美的代码应该是;

<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">

        <!--Inside the below layout your buttons will come. Modify it accordingly-->
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <!--Your buttons will come here-->

        </LinearLayout>

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

    </LinearLayout>

</android.support.v4.widget.NestedScrollView>

如上所示,应该使用 Nested Scroll View 而不是 Scroll View,因为 在另一个滚动视图中需要滚动视图(在本例中为列表视图)。系统无法决定滚动哪个视图,这就是嵌套滚动视图的用武之地。

  <RelativeLayout
      android:layout_width="match_parent">
      android:layout_height="match_parent">
      <ScrollView
      android:layout_width="match_parent"
      android:layout_height="match_parent">

       "your existing layout here, change your top container's height from match_parent or wrap_content "  


最好的情况是在相对布局内使用滚动视图,并在滚动视图内使用所有现有代码

Go through this Example. Make ScroolView as Parent. It Work Form me.

     <ScrollView android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fillViewport="true"
            xmlns:android="http://schemas.android.com/apk/res/android">
        <LinearLayout 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:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            tools:context=".MainActivity">

            <include
                android:id="@+id/maintool"
                layout="@layout/toolbar"></include>


            <RelativeLayout
                android:id="@+id/re1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:paddingTop="10dp">

                <TextView
                    android:id="@+id/text"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Email"
                    android:textSize="18sp" />

                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/text" />
            </RelativeLayout>

            <RelativeLayout
                android:id="@+id/re2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:paddingLeft="10dp"
                android:paddingRight="10dp">

                <TextView
                    android:id="@+id/text2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Select Email template"
                    android:textSize="18sp" />

                <Spinner
                    android:id="@+id/spinner"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/text2"
                    android:paddingTop="10dp" />
            </RelativeLayout>
</LinearLayout>
        </ScroolView>

setListViewHeightBasedOnChildren(your_listview);

将此行添加到您的 activity

public static void setListViewHeightBasedOnChildren(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null) {
        return;
    }
    int totalHeight = 0;
    int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.UNSPECIFIED);
    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();
}

在您的 xml

父级添加滚动视图
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout 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/view_c_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="text1"
            android:textSize="18sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/view_c_motto"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/view_c_name"
            android:text="text4" />

        <TextView
            android:id="@+id/view_c_details"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/view_c_motto"
            android:text="text5" />

        <TextView
            android:id="@+id/view_c_address"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/view_c_details"
            android:text="text2" />

        <TextView
            android:id="@+id/view_c_act_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/view_c_address"
            android:layout_marginTop="71dp"
            android:text="text3" />

        <Button
            android:id="@+id/view_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/view_c_address"
            android:layout_marginLeft="16dp"
            android:layout_marginStart="16dp"
            android:text="Button 1" />

        <Button
            android:id="@+id/view_button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/view_button"
            android:layout_marginLeft="16dp"
            android:layout_marginStart="16dp"
            android:layout_toEndOf="@+id/view_button"
            android:layout_toRightOf="@+id/view_button"
            android:text="button 2" />

        <ListView
            android:id="@+id/my_listview"
            android:layout_width="match_parent"
            android:layout_height="700dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/view_c_act_text"
            android:scrollbars="none" />

    </RelativeLayout>
</ScrollView>