【问题标题】:How to place listview inside a scroll view如何将列表视图放置在滚动视图中
【发布时间】:2012-11-23 09:26:35
【问题描述】:

我有一个RelativeLayout,下面我有一个ListView。我必须将这些放在ScrollView 中。任何帮助将不胜感激。

【问题讨论】:

  • RelativeLayout 视图作为列表的标题视图。
  • 您不应该将 listView 放在 scrollView 中。 listView 实现了自己的 scrollListener ,它不会响应 scrollView 上的事件,

标签: android


【解决方案1】:

您不应该将 ListView 放在 ScrollView 中,因为 ListView 类实现了自己的滚动,并且它只是不接收手势,因为它们都由父 ScrollView 处理

【讨论】:

    【解决方案2】:

    ScrollView 只能为单个Layout 设置。所以将你想要滚动的widgets 放在一个layout 中,如下所示:

    <ScrollView
        android:id="@+id/scrView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
         >
    
        <RelativeLayout
            android:id="@+id/rltvLyt"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >
    
            <ListView
                android:id="@+id/lstView"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:textSize="20sp" />
        </RelativeLayout>
    </ScrollView>
    

    【讨论】:

    • 我已将该特定布局添加为我的列表视图的标题,我的问题得到了解决...
    【解决方案3】:

    不要这样做。你到底想达到什么目的?这里(以及其他任何地方)都提到过,这不是一个好主意,因为滚动机制会因为它们都想滚动而变得混乱。但是,如果您有一个庞大的列表视图,那么您就错过了使用列表视图的主要性能提升,并且可能会使用表格布局并添加行。

    【讨论】:

    • 你的答案和 Ram kiran 一样。如果您发现这是重复的,请标记问题。
    • 我们同时发帖。也可能是他想要使用表格布局,所以我试图弄清楚,因为他可能只是问了错误的问题,在这种情况下我们可以编辑它。我完全赞成结束问题,但有时这个社区非常乐意这样做,因为他们提出了错误的问题并且看起来像重复的问题。
    【解决方案4】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-17
      • 2019-10-05
      • 2015-03-18
      • 1970-01-01
      • 2014-09-04
      • 1970-01-01
      • 2015-04-16
      • 1970-01-01
      相关资源
      最近更新 更多