【问题标题】:I want the entire activity to be scrollable but only listview is scrollable. How do I do that?我希望整个活动都是可滚动的,但只有列表视图是可滚动的。我怎么做?
【发布时间】:2019-01-06 06:55:30
【问题描述】:

我从服务器将数据提取到我的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>

【问题讨论】:

  • 在此处发布您的 xml
  • 我已经添加了 XML 文件。
  • 请试试我的回答
  • 您只是想用ListViewTextView 滚动Buttons 吗?
  • 就像 facebook 应用主页一样。将热门故事视为“按钮”和“文本视图”,以及从“列表视图”收到的帖子

标签: android listview android-activity android-scrollview


【解决方案1】:

完美的代码应该是;

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

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

【讨论】:

  • 你好。它不起作用。它只显示列表中的一项,仅此而已。没有更多的项目。不再可滚动。
【解决方案2】:

将现有布局包裹在 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>

【讨论】:

    【解决方案3】:

    试试这个

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

    【讨论】:

    • 还是一样:'(
    【解决方案4】:
      <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 "  
    


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

    【讨论】:

    • 列表视图的高度等于列表视图项的高度,尽管我尝试了 match_parent 和 wrap_content 并且列表视图仍然可以滚动,但活动不是。
    【解决方案5】:

    我有类似的问题然后我创建了一个类

    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>
    

    【讨论】:

    • 不过,只有列表视图是可滚动的。活动不是:(
    • 你能发布你的xml吗?
    • @SidDharthaNeupane 它肯定会检查编辑后的代码
    • 我做不到。我无法弄清楚如何处理您的代码。我到底应该做什么?
    • 你应该创建一个名为 ListViewExpanded 的类,然后你可以像 listview 一样在 xml 中使用它。@SidDharthaNeupane
    【解决方案6】:

    浏览这个例子。将 ScroolView 设为父级。它对我有用。

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

    【讨论】:

      【解决方案7】:

      这就是您执行任务的方式。这是禁用滚动的列表视图,因此您可以轻松滚动您的活动。

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

      【讨论】:

      • 不过,只有列表视图是可滚动的。活动不是;(
      • 使用 ScrollDiabled ListView 而不是 listview。我为你附上了一个课程
      • 如何使用 ScrollDiabled ListView 而不是 listview?
      • 复制代码并制作课程。并转到 xml 文件并使用与列表视图相同的方法。
      【解决方案8】:

      setListViewHeightBasedOnChildren(your_listview);

      在您的活动中添加这一行

      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>
      

      【讨论】:

        猜你喜欢
        • 2016-07-25
        • 2020-12-11
        • 2013-12-15
        • 2021-04-25
        • 2016-06-26
        • 1970-01-01
        • 1970-01-01
        • 2018-04-07
        • 1970-01-01
        相关资源
        最近更新 更多