【问题标题】:ListView without scroll on Android在Android上没有滚动的ListView
【发布时间】:2013-09-04 07:48:23
【问题描述】:

我想设置列表视图以显示所有项目而不滚动。
下面是我的布局:

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

      <TextView
      android:id="@+id/tv1"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="Large Text"
      android:textColor="@android:color/black"
      android:textAppearance="?android:attr/textAppearanceLarge" />

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

    <TextView
         android:id="@+id/tv2"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:text="Large Text"
         android:textColor="@android:color/black"
         android:textAppearance="?android:attr/textAppearanceLarge" />

     </LinearLayout>

线性布局属于滚动视图。
所以我想设置列表视图的所有项目并按父级的滚动滚动。
我该怎么做?

【问题讨论】:

  • 滚动视图中的列表视图?
  • 这是不可能完成的任务。使用其他替代方法来显示固定数据。
  • @PankajKumar 这不是不可能完成的任务,但大多数情况下这是不好的做法,请看下面我的回答

标签: android listview


【解决方案1】:

这就是你应该如何在滚动视图中设置你的列表视图,但就像其他人回答的那样,你永远不应该将列表视图放在滚动视图中,特别是如果列表视图包含很多项目时

ListView lv = (ListView)findViewById(R.id.list);  // 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;
    }
});

【讨论】:

    【解决方案2】:

    如果这个要求的原因与我需要的相似,下面的课程可以提供帮助。它是ListView 的替代品,它观察适配器及其变化并做出相应反应,但实际上在引擎盖下使用手动填充的LinearLayout。我想我在网上的某个地方找到了这段代码,但我现在找不到它以链接到它。

    我的要求是利用ListView 的优点,即它的适配器功能,在一个页面上显示很少的项目(1 到 5),在单个屏幕上可能有多个这样的替换 ListView .显示后,元素本身将成为较大页面的一部分,因此它们不会在自己的 ListView 内单独滚动,而是与整个页面一起滚动。

    public class StaticListView extends LinearLayout {
      protected Adapter adapter;
      protected Observer observer = new Observer(this);
    
      public StaticListView(Context context) {
        super(context);
      }
    
      public StaticListView(Context context, AttributeSet attrs) {
        super(context, attrs);
      }
    
      public void setAdapter(Adapter adapter) {
        if (this.adapter != null)
          this.adapter.unregisterDataSetObserver(observer);
        this.adapter = adapter;
        adapter.registerDataSetObserver(observer);
        observer.onChanged();
      }
    
      private class Observer extends DataSetObserver {
        StaticListView context;
    
        public Observer(StaticListView context) {
          this.context = context;
        }
    
        @Override
        public void onChanged() {
          List<View> oldViews = new ArrayList<View>(context.getChildCount());
          for (int i = 0; i < context.getChildCount(); i++)
            oldViews.add(context.getChildAt(i));
    
          Iterator<View> iter = oldViews.iterator();
          context.removeAllViews();
          for (int i = 0; i < context.adapter.getCount(); i++) {
            View convertView = iter.hasNext() ? iter.next() : null;
            context.addView(context.adapter.getView(i, convertView, context));
          }
          super.onChanged();
        }
    
        @Override
        public void onInvalidated() {
          context.removeAllViews();
          super.onInvalidated();
        }
      }
    }
    

    【讨论】:

      【解决方案3】:

      不要将列表视图放在滚动视图中。

      http://developer.android.com/reference/android/widget/ScrollView.html

      引用自文档 永远不要将 ScrollView 与 ListView 一起使用,因为 ListView 负责自己的垂直滚动。最重要的是,这样做会破坏 ListView 中处理大型列表的所有重要优化,因为它有效地强制 ListView 显示其整个项目列表以填充 ScrollView 提供的无限容器。

      您可以将文本视图作为页眉和页脚添加到列表视图。

      http://developer.android.com/reference/android/widget/ListView.html

      检查上面链接中的页眉和页脚方法

      你可以有一个相对布局在顶部和底部添加文本视图。相对于textviews有textview之间的listview的

      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent" >
      
          <TextView
              android:id="@+id/textView1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignParentTop="true"
              android:layout_centerHorizontal="true"
              android:text="TextView1" />
      
          <TextView
              android:id="@+id/textView2"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignLeft="@+id/textView1"
              android:layout_alignParentBottom="true"
              android:text="TextView1" />
      
          <ListView
              android:id="@+id/listView1"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:layout_below="@+id/textView1"
              android:layout_above="@+id/textView2"
             >
      
          </ListView>
      
      </RelativeLayout>
      

      【讨论】:

      • 使用页眉/页脚适用于简单的布局,但如果您想在页眉/页脚中放置诸如edittext之类的有状态字段
      【解决方案4】:

      我认为这是不可能的。如果我们使用父布局滚动覆盖列表视图滚动行为,列表视图将无法正常工作。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-14
        • 2015-12-08
        • 2011-09-06
        • 2015-08-23
        • 1970-01-01
        相关资源
        最近更新 更多