1

我知道“不要将 ListView 放入 ScrollView”是一种非常糟糕的做法,因为 ListView 有它自己的滚动条。但是,除了 ListView 之外,我还有几个其他项目(按钮和文本视图)。对于那些,我肯定需要使用 ScrollView。我找到了一些解决方案(http://nex-otaku-en.blogspot.com/2010/12/android-put-listview-in-scrollview.html)来防止列表视图折叠。但是,正如我所说,我需要一个 ScrollView 来封装我的 XML 表单上的所有项目。我已经添加了我的 xml 代码,请指导如何实现这一点。

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

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:choiceMode="singleChoice">
    </ListView>


    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
     <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button1" />
      </LinearLayout>

       <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/longtext" />

</LinearLayout>
</ScrollView>
4

3 回答 3

1

ListView 本身是可滚动的。不要将 ListView 放在滚动视图中。

将您的 ListView 移到 scorllview 之外。您还可以向列表视图添加页眉和页脚。

通过谷歌查看视频

https://www.youtube.com/watch?v=wDBM6wVEO70

于 2013-07-23T20:47:41.927 回答
0

您可以使用可扩展列表视图在滚动视图中使用列表视图,只要内容长度,它将扩展列表视图,创建一个名为 ExpandableHeightListView 的自定义类

包 com.knight.utils;

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

public class ExpandableHeightListView extends ListView
{

boolean expanded = false;

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

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

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

public boolean isExpanded()
{
    return expanded;
}

@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
    // HACK! TAKE THAT ANDROID!
    if (isExpanded())
    {
        // Calculate entire height by providing a very large height hint.
        // But do not use the highest 2 bits of this integer; those are
        // reserved for the MeasureSpec mode.
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);

        ViewGroup.LayoutParams params = getLayoutParams();
        params.height = getMeasuredHeight();
    }
    else
    {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}

public void setExpanded(boolean expanded)
{
    this.expanded = expanded;
}
} 

之后以编程方式将此行添加到您的代码中,它将在滚动视图中扩展列表视图,

Listview listview=(Listview)findviewbyid(R.id.listview1);
((ExpandableHeightListView)listview).setExpandable(true);
于 2014-05-08T13:18:16.417 回答
0

我从另一篇文章中找到了这个解决方案并发现它有效。

虽然当列表项具有相同的高度时所有其他解决方案都有效,但即使列表项具有可变的高度,此解决方案仍能提供完美的结果。

在 xml 中使用这个自定义的类,你很高兴。

public class NestedListView extends ListView implements OnTouchListener, OnScrollListener {

    private int listViewTouchAction;
    private static final int MAXIMUM_LIST_ITEMS_VIEWABLE = 99;

    public NestedListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        listViewTouchAction = -1;
        setOnScrollListener(this);
        setOnTouchListener(this);
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem,
            int visibleItemCount, int totalItemCount) {
        if (getAdapter() != null && getAdapter().getCount() > MAXIMUM_LIST_ITEMS_VIEWABLE) {
            if (listViewTouchAction == MotionEvent.ACTION_MOVE) {
                scrollBy(0, -1);
            }
        }
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int newHeight = 0;
        final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        if (heightMode != MeasureSpec.EXACTLY) {
            ListAdapter listAdapter = getAdapter();
            if (listAdapter != null && !listAdapter.isEmpty()) {
                int listPosition = 0;
                for (listPosition = 0; listPosition < listAdapter.getCount()
                        && listPosition < MAXIMUM_LIST_ITEMS_VIEWABLE; listPosition++) {
                    View listItem = listAdapter.getView(listPosition, null, this);
                    //now it will not throw a NPE if listItem is a ViewGroup instance
                    if (listItem instanceof ViewGroup) {
                        listItem.setLayoutParams(new LayoutParams(
                                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                    }
                    listItem.measure(widthMeasureSpec, heightMeasureSpec);
                    newHeight += listItem.getMeasuredHeight();
                }
                newHeight += getDividerHeight() * listPosition;
            }
            if ((heightMode == MeasureSpec.AT_MOST) && (newHeight > heightSize)) {
                if (newHeight > heightSize) {
                    newHeight = heightSize;
                }
            }
        } else {
            newHeight = getMeasuredHeight();
        }
        setMeasuredDimension(getMeasuredWidth(), newHeight);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (getAdapter() != null && getAdapter().getCount() > MAXIMUM_LIST_ITEMS_VIEWABLE) {
            if (listViewTouchAction == MotionEvent.ACTION_MOVE) {
                scrollBy(0, 1);
            }
        }
        return false;
    }
}
于 2014-04-28T06:51:30.660 回答